Skip to content

Commit b613586

Browse files
authored
Merge pull request #38 from UFOMelkor/feature/data-collector
Automatically register DataCollectorPlugin
2 parents d89d681 + b516bca commit b613586

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/DependencyInjection/ProophServiceBusExtension.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,26 @@ private function busLoad(
8383
foreach (array_keys($config) as $name) {
8484
$serviceBuses[$name] = 'prooph_service_bus.' . $name;
8585
}
86-
$container->setParameter('prooph_service_bus.' . $type . '_buses', $serviceBuses);
86+
$container->setParameter("prooph_service_bus.{$type}_buses", $serviceBuses);
8787

8888
foreach ($config as $name => $options) {
8989
$this->loadBus($type, $name, $options, $container);
9090
}
91+
92+
// Add DataCollector
93+
if ($type !== 'query' && $container->getParameter('kernel.debug')) {
94+
$container
95+
->setDefinition(
96+
"prooph_service_bus.plugin.data_collector.${type}_bus",
97+
new ChildDefinition('prooph_service_bus.plugin.data_collector')
98+
)
99+
->addArgument($type)
100+
->addTag("prooph_service_bus.{$type}_bus.plugin")
101+
->addTag('data_collector', [
102+
'id' => "prooph.{$type}_bus",
103+
'template' => '@ProophServiceBus/Collector/debug_view.html.twig',
104+
]);
105+
}
91106
}
92107

93108
/**

src/Plugin/DataCollectorPlugin.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,41 @@ class DataCollectorPlugin extends DataCollector implements Plugin
2323
*/
2424
protected $listenerHandlers = [];
2525

26+
/**
27+
* @var array
28+
*/
29+
private $buses = [];
30+
2631
/**
2732
* @var Stopwatch
2833
*/
2934
private $stopwatch;
3035

36+
/**
37+
* @var ContainerInterface
38+
*/
39+
private $container;
40+
3141
public function __construct(ContainerInterface $container, string $busType)
3242
{
3343
$this->stopwatch = new Stopwatch();
34-
$this->data['bus_type'] = $busType;
3544
$this->container = $container;
45+
$this->data['bus_type'] = $busType;
3646
$this->data['messages'] = [];
3747
$this->data['duration'] = [];
38-
$this->buses = [];
3948
}
4049

41-
/**
42-
* {@inheritdoc}
43-
*/
4450
public function collect(Request $request, Response $response, \Exception $exception = null)
4551
{
4652
foreach ($this->buses as $bus) {
4753
$busName = $bus->busName();
4854

49-
$reflClass = new \ReflectionClass($bus);
50-
$reflProperty = $reflClass->getProperty('events');
51-
$reflProperty->setAccessible(true);
52-
// todo maybe put as default value in config tree builder to also make it configurable?
53-
// $this->data['config'][$busName]['action_event_emitter'] = get_class($reflProperty->getValue($bus));
54-
$this->data['config'][$busName] = $this->container->getParameter(sprintf('prooph_service_bus.%s.configuration', $busName));
55+
$this->data['config'][$busName] = $this->container->getParameter(
56+
sprintf('prooph_service_bus.%s.configuration', $busName)
57+
);
5558
}
5659
}
5760

58-
/**
59-
* {@inheritdoc}
60-
*/
6161
public function getName(): string
6262
{
6363
return sprintf('prooph.%s_bus', $this->data['bus_type']);
@@ -105,9 +105,14 @@ public function attachToMessageBus(MessageBus $messageBus): void
105105
if ($messageBus instanceof QueryBus) {
106106
return;
107107
}
108+
108109
$this->buses[] = $messageBus;
109110
if (! $messageBus instanceof NamedMessageBus) {
110-
throw new RuntimeException(sprinf('To use the Symfony Datacollector, the Bus "%s" needs to implement "%s"', $messageBus, NamedMessageBus::class));
111+
throw new RuntimeException(sprinf(
112+
'To use the Symfony Datacollector, the Bus "%s" needs to implement "%s"',
113+
$messageBus,
114+
NamedMessageBus::class
115+
));
111116
}
112117

113118
$this->listenerHandlers[] = $messageBus->attach(MessageBus::EVENT_DISPATCH, function (ActionEvent $actionEvent) {
@@ -142,11 +147,11 @@ public function attachToMessageBus(MessageBus $messageBus): void
142147
$this->data['message_callstack'][$actionEvent->getTarget()->busName()][] = [
143148
'id' => $actionEvent->getParam('message')->uuid(),
144149
'message' => $actionEvent->getParam('message-name'),
145-
'handler' => \is_object($actionEvent->getParam('message-handler')) ? get_class($actionEvent->getParam('message-handler')) : (string) $actionEvent->getParam('message-handler'),
150+
'handler' => is_object($actionEvent->getParam('message-handler'))
151+
? get_class($actionEvent->getParam('message-handler'))
152+
: (string) $actionEvent->getParam('message-handler'),
146153
];
147154
}
148-
149-
$context = $this->createContextFromActionEvent($actionEvent);
150155
}, MessageBus::PRIORITY_ROUTE - 50000);
151156
}
152157

@@ -166,7 +171,9 @@ protected function createContextFromActionEvent(ActionEvent $event): array
166171
'message-data' => $event->getParam('message')->toArray(),
167172
'message-name' => $event->getParam('message-name'),
168173
'message-handled' => $event->getParam('message-handled'),
169-
'message-handler' => \is_object($event->getParam('message-handler')) ? get_class($event->getParam('message-handler')) : $event->getParam('message-handler'),
174+
'message-handler' => is_object($event->getParam('message-handler'))
175+
? get_class($event->getParam('message-handler'))
176+
: $event->getParam('message-handler'),
170177
];
171178
}
172179

src/Resources/config/service_bus.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
</service>
1717

1818
<service id="prooph_service_bus.plugin.psr_logger" class="Prooph\Bundle\ServiceBus\Plugin\PsrLoggerPlugin" abstract="true" public="false"/>
19+
<service id="prooph_service_bus.plugin.data_collector" class="Prooph\Bundle\ServiceBus\Plugin\DataCollectorPlugin" abstract="true" public="false">
20+
<argument type="service" id="service_container"/>
21+
</service>
1922

2023
<service id="prooph_service_bus.async_switch_message_router" class="Prooph\ServiceBus\Plugin\Router\AsyncSwitchMessageRouter" />
2124
</services>

0 commit comments

Comments
 (0)