Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e832b7

Browse files
committed
[HttpKernel] Collect data from every event dispatcher
1 parent f06554b commit 2e832b7

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

UPGRADE-6.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ HttpKernel
4747
----------
4848

4949
* Deprecate parameters `container.dumper.inline_factories` and `container.dumper.inline_class_loader`, use `.container.dumper.inline_factories` and `.container.dumper.inline_class_loader` instead
50+
* Deprecate passing `null` or an instance of `Symfony\Contracts\EventDispatcher\EventDispatcherInterface` to `Symfony\Component\HttpKernel\DataCollector\EventDataCollector::__construct`, pass an iterable of `Symfony\Contracts\EventDispatcher\EventDispatcherInterface` instead
5051

5152
Messenger
5253
---------

src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
->set('data_collector.events', EventDataCollector::class)
4949
->args([
50-
service('debug.event_dispatcher')->ignoreOnInvalid(),
50+
tagged_iterator('event_dispatcher.dispatcher'),
5151
service('request_stack')->ignoreOnInvalid(),
5252
])
5353
->tag('data_collector', ['template' => '@WebProfiler/Collector/events.html.twig', 'id' => 'events', 'priority' => 290])

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add `#[WithHttpStatus]` for defining status codes for exceptions
1010
* Use an instance of `Psr\Clock\ClockInterface` to generate the current date time in `DateTimeValueResolver`
1111
* Add `#[WithLogLevel]` for defining log levels for exceptions
12+
* Collect data from every event dispatcher
1213

1314
6.2
1415
---

src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@
2828
*/
2929
class EventDataCollector extends DataCollector implements LateDataCollectorInterface
3030
{
31-
private ?EventDispatcherInterface $dispatcher;
31+
private EventDispatcherInterface|iterable|null $dispatchers;
3232
private ?RequestStack $requestStack;
3333
private ?Request $currentRequest = null;
3434

35-
public function __construct(EventDispatcherInterface $dispatcher = null, RequestStack $requestStack = null)
35+
/**
36+
* @param EventDispatcherInterface|iterable<EventDispatcherInterface>|null $dispatchers
37+
*/
38+
public function __construct(EventDispatcherInterface|iterable $dispatchers = null, RequestStack $requestStack = null)
3639
{
37-
$this->dispatcher = $dispatcher;
40+
if (!is_iterable($dispatchers)) {
41+
trigger_deprecation('symfony/http-kernel', '6.3', 'Passing "null" or a single "%s" to "%s()" is deprecated, pass an "%1$s" iterable instead.', EventDispatcherInterface::class, __METHOD__);
42+
43+
$dispatchers = $dispatchers ? [$dispatchers] : [];
44+
}
45+
$this->dispatchers = $dispatchers;
3846
$this->requestStack = $requestStack;
3947
}
4048

@@ -52,19 +60,26 @@ public function reset()
5260
{
5361
$this->data = [];
5462

55-
if ($this->dispatcher instanceof ResetInterface) {
56-
$this->dispatcher->reset();
63+
foreach ($this->dispatchers as $dispatcher) {
64+
if ($dispatcher instanceof ResetInterface) {
65+
$dispatcher->reset();
66+
}
5767
}
5868
}
5969

6070
public function lateCollect()
6171
{
62-
if ($this->dispatcher instanceof TraceableEventDispatcher) {
63-
$this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest));
64-
$this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest));
65-
$this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest));
72+
foreach ($this->dispatchers as $dispatcher) {
73+
if ($dispatcher instanceof TraceableEventDispatcher) {
74+
array_push($this->data['called_listeners'], ...$dispatcher->getCalledListeners($this->currentRequest));
75+
array_push($this->data['not_called_listeners'], ...$dispatcher->getNotCalledListeners($this->currentRequest));
76+
array_push($this->data['orphaned_events'], ...$dispatcher->getOrphanedEvents($this->currentRequest));
77+
}
6678
}
6779

80+
usort($this->data['called_listeners'], $this->sortListeners(...));
81+
usort($this->data['not_called_listeners'], $this->sortListeners(...));
82+
6883
$this->data = $this->cloneVar($this->data);
6984
}
7085

@@ -122,4 +137,29 @@ public function getName(): string
122137
{
123138
return 'events';
124139
}
140+
141+
private function sortListeners(array $a, array $b): int
142+
{
143+
if (0 !== $cmp = strcmp($a['event'], $b['event'])) {
144+
return $cmp;
145+
}
146+
147+
if (\is_int($a['priority']) && !\is_int($b['priority'])) {
148+
return 1;
149+
}
150+
151+
if (!\is_int($a['priority']) && \is_int($b['priority'])) {
152+
return -1;
153+
}
154+
155+
if ($a['priority'] === $b['priority']) {
156+
return 0;
157+
}
158+
159+
if ($a['priority'] > $b['priority']) {
160+
return -1;
161+
}
162+
163+
return 1;
164+
}
125165
}

0 commit comments

Comments
 (0)