Skip to content

Commit 2b4bd77

Browse files
author
Sam Stevens
committed
setup custom callbacks
1 parent 8a2fc2e commit 2b4bd77

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

BugsnagBundle.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bugsnag\BugsnagBundle;
44

5+
use Bugsnag\BugsnagBundle\DependencyInjection\Compiler\CallbackRegisteringPass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
57
use Symfony\Component\HttpKernel\Bundle\Bundle;
68

79
class BugsnagBundle extends Bundle
@@ -12,4 +14,13 @@ class BugsnagBundle extends Bundle
1214
* @return string
1315
*/
1416
const VERSION = '1.0.0';
17+
18+
/**
19+
* @inheritdoc
20+
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
21+
*/
22+
public function build(ContainerBuilder $container)
23+
{
24+
$container->addCompilerPass(new CallbackRegisteringPass());
25+
}
1526
}

DependencyInjection/ClientFactory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ class ClientFactory
139139
*/
140140
protected $filters;
141141

142+
/**
143+
* Custom callbacks to register on the client.
144+
*
145+
* @var callable[]
146+
*/
147+
protected $customCallbacks = [];
148+
142149
/**
143150
* Create a new client factory instance.
144151
*
@@ -203,6 +210,26 @@ public function __construct(
203210
$this->filters = $filters;
204211
}
205212

213+
/**
214+
* Add a custom callback to be registered on the client.
215+
*
216+
* @param callable|array $callable
217+
*/
218+
public function addCallback($callable)
219+
{
220+
// If the parameter is not a callable (if the method name does not exist on the class for example),
221+
// then throw an exception informing the user
222+
if (! is_callable($callable)) {
223+
throw new \RuntimeException(sprintf(
224+
"Could not add Bugsnag callback. Class %s does not contain the \"%s\" method.",
225+
get_class($callable[0]),
226+
$callable[1]
227+
));
228+
}
229+
230+
$this->customCallbacks[] = $callable;
231+
}
232+
206233
/**
207234
* Make a new client instance.
208235
*
@@ -248,6 +275,10 @@ public function make()
248275
$client->setFilters($this->filters);
249276
}
250277

278+
foreach ($this->customCallbacks as $callable) {
279+
$client->registerCallback($callable);
280+
}
281+
251282
return $client;
252283
}
253284

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bugsnag\BugsnagBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
class CallbackRegisteringPass implements CompilerPassInterface
10+
{
11+
const FACTORY_SERVICE_NAME = 'bugsnag.factory';
12+
const TAG_NAME = 'bugsnag.callback';
13+
14+
/**
15+
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
16+
*/
17+
public function process(ContainerBuilder $container)
18+
{
19+
if (! $container->has(self::FACTORY_SERVICE_NAME)) {
20+
return;
21+
}
22+
23+
// Get the Bugsnag factory service
24+
$bugsnagFactory = $container->findDefinition(self::FACTORY_SERVICE_NAME);
25+
26+
// Get all services tagged as a callback
27+
$callbackServices = $container->findTaggedServiceIds(self::TAG_NAME);
28+
29+
// Add each callback to the factory service via an addCallback call
30+
foreach ($callbackServices as $id => $tags) {
31+
foreach ($tags as $attributes) {
32+
// Get the method name to call on the service from the tag definition,
33+
// defaulting to registerCallback
34+
$method = isset($attributes['method']) ? $attributes['method'] : 'registerCallback';
35+
$bugsnagFactory->addMethodCall('addCallback', [[new Reference($id), $method]]);
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)