Skip to content

Commit

Permalink
Added multiple environments support.
Browse files Browse the repository at this point in the history
  • Loading branch information
simodima committed May 6, 2013
1 parent 64f9ba0 commit 7a50754
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vendor/
!vendor/.gitkeep
.idea/

composer.phar
composer.lock
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('kunstmaan_sentry');
$rootNode
->children()
->arrayNode('environments')
->cannotBeEmpty()
->defaultValue(array('prod'))
->prototype('scalar')
->end()
->end();

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/KunstmaanSentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$container->setParameter($this->getAlias(). '.environments', $config['environments']);

}
}
12 changes: 9 additions & 3 deletions EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ class ExceptionListener
*/
protected $client;

/**
* @var array $environments
*/
protected $environments;

/**
* @param Raven $client
*/
public function __construct(Raven $client)
public function __construct(Raven $client, $environments)
{
$this->client = $client;
$this->environments = $environments;
}

/**
Expand All @@ -39,11 +45,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
if ($event->getRequest()->attributes->has("_controller")) {
$culprit = $event->getRequest()->attributes->get("_controller");
}
if ($this->client->getEnvironment() != 'prod') {
if (!in_array($this->client->getEnvironment(), $this->environments)) {
return array($exception, $culprit, $this->client->getEnvironment());
} else {
$event_id = $this->client->getIdent($this->client->captureException($exception, $culprit, $this->client->getEnvironment()));
error_log("[$event_id] " . $exception->getMessage() . ' in: ' . $exception->getFile() . ':' . $exception->getLine());
return error_log("[$event_id] " . $exception->getMessage() . ' in: ' . $exception->getFile() . ':' . $exception->getLine());
}
}
}
12 changes: 9 additions & 3 deletions EventListener/ShutdownListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ class ShutdownListener
*/
protected $client;

/**
* @var array $environments
*/
protected $environments;

/**
* @param Raven $client
*/
public function __construct(Raven $client)
public function __construct(Raven $client, $environments)
{
$this->client = $client;
$this->environments = $environments;
}

/**
Expand Down Expand Up @@ -52,11 +58,11 @@ public function onShutdown()
return;
}

$message = '[Shutdown Error]: %s';
$message = '%s -> [Shutdown Error]';
$message = sprintf($message, $error['message']);
$exception = new RuntimeException($message.' in: '.$error['file'].':'.$error['line']);
$culprit = $error['file'];
if ($this->client->getEnvironment() != 'prod') {
if ( !in_array($this->client->getEnvironment(), $this->environments) ) {
return array($exception, $culprit, $this->client->getEnvironment());
} else {
$event_id = $this->client->getIdent($this->client->captureException($exception, $culprit, $this->client->getEnvironment()));
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
<service id="sentry.exception_listener" class="%sentry.exception_listener.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
<argument type="service" id="sentry.client" />
<argument>%kunstmaan_sentry.environments%</argument>
</service>

<service id="sentry.shutdown_listener" class="%sentry.shutdown_listener.class%">
<tag name="kernel.event_listener" event="kernel.controller" method="register" />
<argument type="service" id="sentry.client" />
<argument>%kunstmaan_sentry.environments%</argument>
</service>
</services>
</container>
40 changes: 33 additions & 7 deletions Tests/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,46 @@
*/
class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers \Kunstmaan\SentryBundle\EventListener\ExceptionListener
*/
public function testExceptionListener()

private function getListnerResult($environments, $currentEnv = 'test')
{
$kernel = new \TestKernel('test', false);
$kernel = new \TestKernel($currentEnv , false);
$raven = new Raven('http://public:[email protected]/1', $kernel->getEnvironment());
$listener = new ExceptionListener($raven);
$listener = new ExceptionListener($raven, $environments);
$request = new Request();
$event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new Exception("Test"));
$result = $listener->onKernelException($event);
return $result = $listener->onKernelException($event);
}

/**
* @dataProvider unhandledEnvironmentsProvider
* @covers \Kunstmaan\SentryBundle\EventListener\ExceptionListener
*/
public function testExceptionListenerWithUnhandledEnv($environments)
{
$result = $this->getListnerResult($environments, 'test');
$this->assertTrue($result[0] instanceof Exception);
$this->assertEmpty($result[1]);
$this->assertEquals($result[2], 'test');
}

/**
* @dataProvider unhandledEnvironmentsProvider
* @covers \Kunstmaan\SentryBundle\EventListener\ExceptionListener
*/
public function testExceptionListenerWithHandledEnv($environments)
{
ini_set('error_log','/dev/null');
$result = $this->getListnerResult($environments, 'prod');
$this->assertTrue($result);
}

public static function unhandledEnvironmentsProvider()
{
return array(
array(array('prod', 'dev')),
array(array('prod', 'dev', 'stage')),
array(array('prod', 'dev', 'my_env'))
);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": "2.1.*",
"symfony/framework-bundle": ">=2.1",
"raven/raven": "dev-master"
},
"autoload": {
Expand Down

0 comments on commit 7a50754

Please sign in to comment.