Skip to content

Commit 9f0f019

Browse files
author
Andrii Afanasiev
committed
add 'checkIgnore' support
1 parent a2537ef commit 9f0f019

File tree

12 files changed

+233
-30
lines changed

12 files changed

+233
-30
lines changed

Provider/InterfaceCheckIgnore.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Provider;
4+
5+
/**
6+
* Interface InterfaceCheckIgnore
7+
*
8+
* @package SymfonyRollbarBundle\Provider
9+
*/
10+
interface InterfaceCheckIgnore
11+
{
12+
/**
13+
* @param boolean $isUncaught
14+
* @param \Rollbar\ErrorWrapper|\Exception|string $toLog
15+
* @param \Rollbar\Payload\Payload $payload
16+
*
17+
* @return boolean
18+
*/
19+
public function checkIgnore($isUncaught, $toLog, $payload);
20+
}

Provider/RollbarHandler.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class RollbarHandler extends AbstractProcessingHandler
3939
*/
4040
protected $rbConfig;
4141

42+
/**
43+
* List of configuration options where we have to try to inject services
44+
* @var array
45+
*/
46+
protected $injectServices = [
47+
// config option, method
48+
'person_fn' => 'getPerson',
49+
'checkIgnore' => 'checkIgnore',
50+
];
51+
4252
/**
4353
* Monolog vs Rollbar
4454
* @var array
@@ -95,26 +105,42 @@ protected function initialize()
95105
$rConfig[$key] = $value;
96106
}
97107

98-
// DI for 'person_fn'
99-
if (!empty($rConfig['person_fn'])) {
100-
if ($this->container->has($rConfig['person_fn'])) {
101-
$service = $this->container->get($rConfig['person_fn']);
102-
} elseif (class_exists($rConfig['person_fn'])) {
103-
$service = new $rConfig['person_fn']($this->container);
108+
foreach ($this->injectServices as $option => $method) {
109+
if (empty($rConfig[$option])) {
110+
continue;
104111
}
105112

106-
if (!empty($service) && $service instanceof AbstractPersonProvider) {
107-
$rConfig['person_fn'] = [$service, 'getPerson'];
108-
} else {
109-
$rConfig['person_fn'] = is_callable($rConfig['person_fn']) ? $rConfig['person_fn'] : null;
110-
}
113+
$rConfig[$option] = $this->injectService($rConfig[$option], $method);
111114
}
112115

113116
$this->exclude = empty($config['exclude']) ? [] : $config['exclude'];
114117

115118
return $rConfig;
116119
}
117120

121+
/**
122+
* Inject service into configuration
123+
*
124+
* @param string $name
125+
* @param string $method
126+
*
127+
* @return array|callable|null
128+
*/
129+
protected function injectService($name, $method)
130+
{
131+
if ($this->container->has($name)) {
132+
$service = $this->container->get($name);
133+
} elseif (class_exists($name)) {
134+
$service = new $name($this->container);
135+
}
136+
137+
if (!empty($service) && method_exists($service, $method)) {
138+
return [$service, $method];
139+
} else {
140+
return is_callable($name) ? $name : null;
141+
}
142+
}
143+
118144
/**
119145
* @return \Symfony\Component\DependencyInjection\ContainerInterface
120146
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace SymfonyRollbarBundle\Tests\Fixtures;
3+
4+
use SymfonyRollbarBundle\Provider\InterfaceCheckIgnore;
5+
6+
class CheckIgnoreProvider implements InterfaceCheckIgnore
7+
{
8+
/**
9+
* @var bool
10+
*/
11+
protected $ignore = false;
12+
13+
/**
14+
* @param boolean $isUncaught
15+
* @param \Rollbar\ErrorWrapper|\Exception|string $toLog
16+
* @param \Rollbar\Payload\Payload $payload
17+
*
18+
* @return boolean
19+
*/
20+
public function checkIgnore($isUncaught, $toLog, $payload)
21+
{
22+
return $this->ignore;
23+
}
24+
25+
/**
26+
* @param bool $ignore
27+
*
28+
* @return $this
29+
*/
30+
public function setIgnore($ignore = false)
31+
{
32+
$this->ignore = $ignore;
33+
34+
return $this;
35+
}
36+
}

Tests/Fixtures/app/config/config_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ symfony_rollbar:
1313
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1414
environment: '%kernel.environment%'
1515
person_fn: \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider
16+
checkIgnore: \SymfonyRollbarBundle\Tests\Fixtures\CheckIgnoreProvider
1617

1718
rollbar_js:
1819
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'

Tests/Fixtures/app/config/config_test_pf.yml renamed to Tests/Fixtures/app/config/config_test_if.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ symfony_rollbar:
1212
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1313
environment: '%kernel.environment%'
1414
person_fn: "get_awesome_person"
15+
checkIgnore: "should_ignore"
1516

1617
rollbar_js:
1718
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'

Tests/Fixtures/app/config/config_test_ps.yml renamed to Tests/Fixtures/app/config/config_test_is.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ symfony_rollbar:
1313
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1414
environment: '%kernel.environment%'
1515
person_fn: "symfony_rollbar.fixture.person_provider"
16+
checkIgnore: "symfony_rollbar.fixture.check_ignore_provider"
1617

1718
rollbar_js:
1819
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
parameters:
22
symfony_rollbar.fixture.person_provider.class: \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider
3+
symfony_rollbar.fixture.check_ignore_provider.class: \SymfonyRollbarBundle\Tests\Fixtures\CheckIgnoreProvider
34

45
services:
56
symfony_rollbar.fixture.person_provider:
67
class: "%symfony_rollbar.fixture.person_provider.class%"
78
public: true
89
arguments: ["@service_container"]
10+
11+
symfony_rollbar.fixture.check_ignore_provider:
12+
class: "%symfony_rollbar.fixture.check_ignore_provider.class%"
13+
public: true

Tests/Fixtures/global_fn.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* @return array
5+
*/
6+
function get_awesome_person()
7+
{
8+
return [
9+
'id' => 'global_id',
10+
'username' => 'global_username',
11+
'email' => 'global_email',
12+
];
13+
}
14+
15+
/**
16+
* @param boolean $isUncaught
17+
* @param \Rollbar\ErrorWrapper|\Exception|string $toLog
18+
* @param \Rollbar\Payload\Payload $payload
19+
*
20+
* @return boolean
21+
*/
22+
function should_ignore($isUncaught, $toLog, $payload)
23+
{
24+
return false;
25+
}

Tests/Fixtures/global_person_fn.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

Tests/SymfonyRollbarBundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testParameters()
3535
'base_api_url' => 'https://api.rollbar.com/api/1/',
3636
'branch' => Configuration::BRANCH,
3737
'capture_error_stacktraces' => true,
38-
'checkIgnore' => null,
38+
'checkIgnore' => '\SymfonyRollbarBundle\Tests\Fixtures\CheckIgnoreProvider',
3939
'code_version' => '',
4040
'enable_utf8_sanitization' => true,
4141
'environment' => static::$kernel->getEnvironment(),

0 commit comments

Comments
 (0)