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

Skip to content

Commit 9770a9c

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

File tree

6 files changed

+100
-123
lines changed

6 files changed

+100
-123
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', 'name'),
5151
service('request_stack')->ignoreOnInvalid(),
5252
])
5353
->tag('data_collector', ['template' => '@WebProfiler/Collector/events.html.twig', 'id' => 'events', 'priority' => 290])

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"symfony/error-handler": "^6.1",
2727
"symfony/event-dispatcher": "^5.4|^6.0",
2828
"symfony/http-foundation": "^6.2",
29-
"symfony/http-kernel": "^6.2.1",
29+
"symfony/http-kernel": "^6.3",
3030
"symfony/polyfill-mbstring": "~1.0",
3131
"symfony/filesystem": "^5.4|^6.0",
3232
"symfony/finder": "^5.4|^6.0",

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/events.html.twig

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,85 @@
88
{% endblock %}
99

1010
{% block panel %}
11-
<h2>Event Dispatcher</h2>
11+
<h2>Dispatched Events</h2>
1212

13-
{% if collector.calledlisteners is empty %}
14-
<div class="empty empty-panel">
15-
<p>No events have been recorded. Check that debugging is enabled in the kernel.</p>
16-
</div>
17-
{% else %}
18-
<div class="sf-tabs">
13+
<div class="sf-tabs">
14+
{% for dispatcherName, dispatcherData in collector.data %}
1915
<div class="tab">
20-
<h3 class="tab-title">Called Listeners <span class="badge">{{ collector.calledlisteners|length }}</span></h3>
21-
22-
<div class="tab-content">
23-
{{ _self.render_table(collector.calledlisteners) }}
24-
</div>
25-
</div>
26-
27-
<div class="tab">
28-
<h3 class="tab-title">Not Called Listeners <span class="badge">{{ collector.notcalledlisteners|length }}</span></h3>
16+
<h3 class="tab-title">{{ dispatcherName }}</h3>
2917
<div class="tab-content">
30-
{% if collector.notcalledlisteners is empty %}
31-
<div class="empty">
32-
<p>
33-
<strong>There are no uncalled listeners</strong>.
34-
</p>
35-
<p>
36-
All listeners were called for this request or an error occurred
37-
when trying to collect uncalled listeners (in which case check the
38-
logs to get more information).
39-
</p>
18+
{% if dispatcherData.called_listeners is empty %}
19+
<div class="empty empty-panel">
20+
<p>No events have been recorded. Check that debugging is enabled in the kernel.</p>
4021
</div>
4122
{% else %}
42-
{{ _self.render_table(collector.notcalledlisteners) }}
43-
{% endif %}
44-
</div>
45-
</div>
23+
<div class="sf-tabs">
24+
<div class="tab">
25+
<h3 class="tab-title">Called Listeners <span class="badge">{{ dispatcherData.called_listeners|length }}</span></h3>
4626

47-
<div class="tab">
48-
<h3 class="tab-title">Orphaned Events <span class="badge">{{ collector.orphanedEvents|length }}</span></h3>
49-
<div class="tab-content">
50-
{% if collector.orphanedEvents is empty %}
51-
<div class="empty">
52-
<p>
53-
<strong>There are no orphaned events</strong>.
54-
</p>
55-
<p>
56-
All dispatched events were handled or an error occurred
57-
when trying to collect orphaned events (in which case check the
58-
logs to get more information).
59-
</p>
27+
<div class="tab-content">
28+
{{ _self.render_table(dispatcherData.called_listeners) }}
29+
</div>
30+
</div>
31+
32+
<div class="tab">
33+
<h3 class="tab-title">Not Called Listeners <span class="badge">{{ dispatcherData.not_called_listeners|length }}</span></h3>
34+
<div class="tab-content">
35+
{% if dispatcherData.not_called_listeners is empty %}
36+
<div class="empty">
37+
<p>
38+
<strong>There are no uncalled listeners</strong>.
39+
</p>
40+
<p>
41+
All listeners were called for this request or an error occurred
42+
when trying to collect uncalled listeners (in which case check the
43+
logs to get more information).
44+
</p>
45+
</div>
46+
{% else %}
47+
{{ _self.render_table(dispatcherData.not_called_listeners) }}
48+
{% endif %}
49+
</div>
50+
</div>
51+
52+
<div class="tab">
53+
<h3 class="tab-title">Orphaned Events <span class="badge">{{ dispatcherData.orphaned_events|length }}</span></h3>
54+
<div class="tab-content">
55+
{% if dispatcherData.orphaned_events is empty %}
56+
<div class="empty">
57+
<p>
58+
<strong>There are no orphaned events</strong>.
59+
</p>
60+
<p>
61+
All dispatched events were handled or an error occurred
62+
when trying to collect orphaned events (in which case check the
63+
logs to get more information).
64+
</p>
65+
</div>
66+
{% else %}
67+
<table>
68+
<thead>
69+
<tr>
70+
<th>Event</th>
71+
</tr>
72+
</thead>
73+
<tbody>
74+
{% for event in dispatcherData.orphaned_events %}
75+
<tr>
76+
<td class="font-normal">{{ event }}</td>
77+
</tr>
78+
{% endfor %}
79+
</tbody>
80+
</table>
81+
{% endif %}
82+
</div>
83+
</div>
6084
</div>
61-
{% else %}
62-
<table>
63-
<thead>
64-
<tr>
65-
<th>Event</th>
66-
</tr>
67-
</thead>
68-
<tbody>
69-
{% for event in collector.orphanedEvents %}
70-
<tr>
71-
<td class="font-normal">{{ event }}</td>
72-
</tr>
73-
{% endfor %}
74-
</tbody>
75-
</table>
7685
{% endif %}
7786
</div>
7887
</div>
79-
</div>
80-
{% endif %}
88+
{% endfor %}
89+
</div>
8190
{% endblock %}
8291

8392
{% macro render_table(listeners) %}

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: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,94 +28,60 @@
2828
*/
2929
class EventDataCollector extends DataCollector implements LateDataCollectorInterface
3030
{
31-
private ?EventDispatcherInterface $dispatcher;
31+
/** @var iterable<EventDispatcherInterface> */
32+
private iterable $dispatchers;
3233
private ?RequestStack $requestStack;
3334
private ?Request $currentRequest = null;
3435

35-
public function __construct(EventDispatcherInterface $dispatcher = null, RequestStack $requestStack = null)
36+
/**
37+
* @param iterable<EventDispatcherInterface>|EventDispatcherInterface|null $dispatchers
38+
*/
39+
public function __construct(iterable|EventDispatcherInterface|null $dispatchers = [], RequestStack $requestStack = null)
3640
{
37-
$this->dispatcher = $dispatcher;
41+
if (!is_iterable($dispatchers)) {
42+
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__);
43+
44+
$dispatchers = $dispatchers ? ['event_dispatcher' => $dispatchers] : [];
45+
}
46+
$this->dispatchers = $dispatchers;
3847
$this->requestStack = $requestStack;
3948
}
4049

4150
public function collect(Request $request, Response $response, \Throwable $exception = null)
4251
{
4352
$this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
44-
$this->data = [
45-
'called_listeners' => [],
46-
'not_called_listeners' => [],
47-
'orphaned_events' => [],
48-
];
53+
$this->data = [];
4954
}
5055

5156
public function reset()
5257
{
5358
$this->data = [];
5459

55-
if ($this->dispatcher instanceof ResetInterface) {
56-
$this->dispatcher->reset();
60+
foreach ($this->dispatchers as $dispatcher) {
61+
if ($dispatcher instanceof ResetInterface) {
62+
$dispatcher->reset();
63+
}
5764
}
5865
}
5966

6067
public function lateCollect()
6168
{
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));
69+
foreach ($this->dispatchers as $name => $dispatcher) {
70+
if ($dispatcher instanceof TraceableEventDispatcher) {
71+
$this->data[$name] = [
72+
'called_listeners' => $dispatcher->getCalledListeners($this->currentRequest),
73+
'not_called_listeners' => $dispatcher->getNotCalledListeners($this->currentRequest),
74+
'orphaned_events' => $dispatcher->getOrphanedEvents($this->currentRequest),
75+
];
76+
}
6677
}
6778

6879
$this->data = $this->cloneVar($this->data);
6980
}
7081

71-
/**
72-
* @see TraceableEventDispatcher
73-
*/
74-
public function setCalledListeners(array $listeners)
75-
{
76-
$this->data['called_listeners'] = $listeners;
77-
}
78-
79-
/**
80-
* @see TraceableEventDispatcher
81-
*/
82-
public function getCalledListeners(): array|Data
83-
{
84-
return $this->data['called_listeners'];
85-
}
86-
87-
/**
88-
* @see TraceableEventDispatcher
89-
*/
90-
public function setNotCalledListeners(array $listeners)
91-
{
92-
$this->data['not_called_listeners'] = $listeners;
93-
}
94-
95-
/**
96-
* @see TraceableEventDispatcher
97-
*/
98-
public function getNotCalledListeners(): array|Data
99-
{
100-
return $this->data['not_called_listeners'];
101-
}
102-
103-
/**
104-
* @param array $events An array of orphaned events
105-
*
106-
* @see TraceableEventDispatcher
107-
*/
108-
public function setOrphanedEvents(array $events)
109-
{
110-
$this->data['orphaned_events'] = $events;
111-
}
112-
113-
/**
114-
* @see TraceableEventDispatcher
115-
*/
116-
public function getOrphanedEvents(): array|Data
82+
public function getData(): array|Data
11783
{
118-
return $this->data['orphaned_events'];
84+
return $this->data;
11985
}
12086

12187
public function getName(): string

0 commit comments

Comments
 (0)