Skip to content

Commit

Permalink
Merge pull request #4 from hellowearemito/1.0.0-dev
Browse files Browse the repository at this point in the history
update to release 1.0.0
  • Loading branch information
albertborsos authored Mar 21, 2017
2 parents f56ca97 + 88326bd commit 84578fc
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 337 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ before_script:
fi
script:
- php ./vendor/bin/phpcs --standard=vendor/mito/yii2-coding-standards/Application src
- php ./vendor/bin/phpcs --standard=vendor/mito/yii2-coding-standards/Application -s --exclude=PSR1.Files.SideEffects,PSR1.Classes.ClassDeclaration --extensions=php tests
- sh ./phpcs.sh
- php ./vendor/bin/codecept run unit -d $PHPUNIT_FLAGS

after_success:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
- renamed `\mito\sentry\SentryComponent` to `\mito\sentry\Component` and `\mito\sentry\SentryTarget` to `\mito\sentry\Target`
- catch array type exceptions and show it nicely in Sentry (2017-03-21 aborsos)
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```
php composer.phar require --prefer-dist mito/yii2-sentry "*"
php composer.phar require --prefer-dist mito/yii2-sentry "~1.0.0"
```

or add the following line to the require section of your `composer.json` file:

```
"mito/yii2-sentry": "*"
"mito/yii2-sentry": "~1.0.0"
```

## Requirements
Expand All @@ -39,13 +39,13 @@ Once the extension is installed, set your configuration in common config file:

```php
'components' => [

'sentry' => [
'class' => 'mito\sentry\SentryComponent',
'dsn' => '', // private DSN
'environment' => YII_CONFIG_ENVIRONMENT, // if not set, the default is `development`
'jsNotifier' => true, // to collect JS errors
'clientOptions' => [ // raven-js config parameter
'class' => 'mito\sentry\Component',
'dsn' => 'YOUR-PRIVATE-DSN', // private DSN
'environment' => 'staging', // if not set, the default is `production`
'jsNotifier' => true, // to collect JS errors. Default value is `false`
'jsOptions' => [ // raven-js config parameter
'whitelistUrls' => [ // collect JS errors from these urls
'http://staging.my-product.com',
'https://my-product.com',
Expand All @@ -55,14 +55,15 @@ Once the extension is installed, set your configuration in common config file:
'log' => [
'targets' => [
[
'class' => 'mito\sentry\SentryTarget',
'class' => 'mito\sentry\Target',
'levels' => ['error', 'warning'],
'except' => [
'yii\web\HttpException:404',
],
]
],
],
],

],
```

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
],
"require": {
"yiisoft/yii2": "*",
"sentry/sentry": "1.5.*",
"bower-asset/raven-js": "3.7.*"
"sentry/sentry": "1.6.*",
"bower-asset/raven-js": "3.12.*"
},
"require-dev": {
"yiisoft/yii2-codeception": "~2.0",
Expand Down
10 changes: 10 additions & 0 deletions phpcs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

php ./vendor/bin/phpcs --standard=vendor/mito/yii2-coding-standards/Application src
SRC=$?
php ./vendor/bin/phpcs --standard=vendor/mito/yii2-coding-standards/Application -s --exclude=PSR1.Files.SideEffects,PSR1.Classes.ClassDeclaration --extensions=php tests
TESTS=$?

if [ $SRC -ne 0 ] || [ $TESTS -ne 0 ]; then
exit 1
fi
116 changes: 43 additions & 73 deletions src/SentryComponent.php → src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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;

class SentryComponent extends Component
class Component extends \yii\base\Component
{

/**
Expand All @@ -36,14 +36,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 +53,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 +139,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);
}
}
}
16 changes: 7 additions & 9 deletions src/SentryTarget.php → src/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace mito\sentry;

use Raven_Stacktrace;
use yii\base\ErrorException;
use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\helpers\VarDumper;
use yii\log\Logger;
use yii\log\Target;

class SentryTarget extends Target
class Target extends \yii\log\Target
{
/**
* @var string|SentryComponent
* @var string|Component
*/
public $sentry = 'sentry';

Expand All @@ -25,7 +22,7 @@ public function init()
{
parent::init();

$this->sentry = Instance::ensure($this->sentry, SentryComponent::className());
$this->sentry = Instance::ensure($this->sentry, Component::className());

if (!$this->sentry->enabled) {
$this->enabled = false;
Expand Down Expand Up @@ -66,9 +63,10 @@ public function export()
$data['message'] = $context['msg'];
$extra = $context;
unset($extra['msg']);
$data['extra'] = $extra;
$data['extra'] = VarDumper::export($extra);
} else {
$data['message'] = $context;
$data['message'] = is_array($context) ? VarDumper::export($context) : $context;
$data['extra'] = VarDumper::export($context);
}

$this->sentry->capture($data, $traces);
Expand Down
2 changes: 0 additions & 2 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
// autoload Dummy_Raven_Client
require_once dirname(__FILE__) . '/../vendor/sentry/sentry/test/Raven/Tests/ClientTest.php';
Raven_Autoloader::register();

$_SERVER['SCRIPT_FILENAME'] = '/' . dirname(__DIR__) . '/web';
Expand Down
Loading

0 comments on commit 84578fc

Please sign in to comment.