Skip to content

Commit

Permalink
Merge 193b968 into e5dd65e
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Nov 24, 2016
2 parents e5dd65e + 193b968 commit aa78da9
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 119 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -27,6 +27,7 @@ before_script:
fi
script:
- php ./vendor/bin/phpcs --standard=vendor/mito/yii2-coding-standards/Application src
- php ./vendor/bin/codecept run unit -d $PHPUNIT_FLAGS

after_success:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,6 @@
2016-11-23 (aborsos) - 1.0.0.
- validate sentry DSN even if the component is disabled
- DSN is required even if the component is disabled
- default environment tag is `production`
- remove deprecated methods and properties
- separate init method's content to individual methods
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -21,7 +21,8 @@
"codeception/codeception": "2.1.*",
"codeception/specify": " *",
"codeception/verify": " *",
"codeception/mockery-module": "^0.2.2"
"codeception/mockery-module": "^0.2.2",
"mito/yii2-coding-standards": "~2.0.0@beta"
},
"autoload": {
"psr-4": {
Expand Down
157 changes: 154 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 42 additions & 71 deletions src/SentryComponent.php
Expand Up @@ -2,11 +2,12 @@

namespace mito\sentry;

use Closure;
use mito\sentry\assets\RavenAsset;
use Yii;
use yii\base\Component;
use yii\base\ErrorException;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\View;

Expand Down Expand Up @@ -36,14 +37,7 @@ class SentryComponent extends Component
* @var string environment name
* @note this is ignored if [[client]] is a Raven client instance.
*/
public $environment = 'development';

/**
* @var array Options of the Raven client.
* @see \Raven_Client::__construct for more details
* @deprecated use [[client]] instead
*/
public $options = [];
public $environment = 'production';

/**
* collect JavaScript errors
Expand All @@ -60,86 +54,74 @@ class SentryComponent extends Component
*/
public $jsOptions;

/**
* Raven-JS configuration array
*
* @var array
* @see https://docs.getsentry.com/hosted/clients/javascript/config/
* @deprecated use [[jsOptions]] instead
*/
public $clientOptions = [];

/**
* @var string Raven client class
* @deprecated use [[client]] instead
*/
public $ravenClass = '\Raven_Client';

/**
* @var \Raven_Client|array Raven client or configuration array used to instantiate one
*/
public $client;
public $client = [];

public function init()
{
$this->validateDsn();

if (!$this->enabled) {
return;
}

if ($this->jsOptions === null) {
$this->jsOptions = $this->clientOptions;
}

$this->setRavenClient();
$this->setEnvironmentOptions();
$this->generatePublicDsn();
$this->registerAssets();
}

// for backwards compatibility
$this->clientOptions = $this->jsOptions;

private function validateDsn()
{
if (empty($this->dsn)) {
throw new InvalidConfigException('Private or public DSN must be set!');
throw new InvalidConfigException('Private DSN must be set!');
}

if ($this->publicDsn === null && $this->jsNotifier === true) {
$this->publicDsn = preg_replace('/^(https:\/\/|http:\/\/)([a-z0-9]*):([a-z0-9]*)@(.*)/', '$1$2@$4', $this->dsn);
}
if (!empty($this->publicDsn)) {
$this->jsNotifier = true;
}

if (is_array($this->client)) {
$ravenClass = $this->client['class'];
$options = $this->client;
unset($options['class']);
$this->client = new $ravenClass($this->dsn, $options);
} elseif (empty($this->client)) {
// deprecated codepath
$this->client = new $this->ravenClass($this->dsn, $this->options);
}

$this->registerAssets();
// throws \InvalidArgumentException if dsn is invalid
\Raven_Client::parseDSN($this->dsn);
}

/**
* Adds a tag to filter events by environment
*/
private function setEnvironmentOptions()
{
if (empty($this->environment)) {
return;
}

if (is_array($this->client)) {
$this->client['tags']['environment'] = $this->environment;
if (is_object($this->client) && property_exists($this->client, 'tags')) {
$this->client->tags = ArrayHelper::merge($this->client->tags, ['environment' => $this->environment]);
}
$this->options['tags']['environment'] = $this->environment;
$this->jsOptions['tags']['environment'] = $this->environment;
}

private function setRavenClient()
{
if (is_array($this->client)) {
$ravenClass = ArrayHelper::remove($this->client, 'class', '\Raven_Client');
$options = $this->client;
$this->client = new $ravenClass($this->dsn, $options);
} elseif (!is_object($this->client) || $this->client instanceof Closure) {
$this->client = Yii::createObject($this->client);
}

if (!is_object($this->client)) {
throw new InvalidConfigException(get_class($this) . '::' . 'client must be an object');
}
}

/**
* Registers RavenJS if publicDsn exists
*/
private function registerAssets()
{
if ($this->jsNotifier && Yii::$app instanceof \yii\web\Application) {
RavenAsset::register(Yii::$app->getView());
Yii::$app->getView()->registerJs('Raven.config(' . Json::encode($this->publicDsn) . ', ' . Json::encode($this->jsOptions) . ').install();', View::POS_HEAD);
$view = Yii::$app->getView();
RavenAsset::register($view);
$view->registerJs('Raven.config(' . Json::encode($this->publicDsn) . ', ' . Json::encode($this->jsOptions) . ').install();', View::POS_HEAD);
}
}

Expand All @@ -158,21 +140,10 @@ public function capture($data, $stack = null, $vars = null)
return $this->client->capture($data, $stack, $vars);
}

/**
* @return \Raven_Client
* @deprecated use [[$client]]
*/
public function getClient()
private function generatePublicDsn()
{
return $this->client;
}

/**
* @return string public dsn
* @deprecated use [[$publicDsn]]
*/
public function getPublicDsn()
{
return $this->publicDsn;
if ($this->publicDsn === null && $this->jsNotifier === true) {
$this->publicDsn = preg_replace('/^(https:\/\/|http:\/\/)([a-z0-9]*):([a-z0-9]*)@(.*)/', '$1$2@$4', $this->dsn);
}
}
}
4 changes: 2 additions & 2 deletions src/SentryTarget.php
Expand Up @@ -55,8 +55,8 @@ public function export()
'level' => static::getLevelName($level),
'timestamp' => $timestamp,
'tags' => [
'category' => $category
]
'category' => $category,
],
];

if ($context instanceof \Throwable || $context instanceof \Exception) {
Expand Down

0 comments on commit aa78da9

Please sign in to comment.