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

Skip to content

Commit e658f9f

Browse files
committed
[HttpKernel] Collect data from every event dispatcher
1 parent 5fbf9be commit e658f9f

File tree

4 files changed

+111
-87
lines changed

4 files changed

+111
-87
lines changed

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/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
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Add `#[WithLogLevel]` for defining log levels for exceptions
1212
* Add `skip_response_headers` to the `HttpCache` options
1313
* Introduce targeted value resolvers with `#[ValueResolver]` and `#[AsTargetedValueResolver]`
14+
* Collect data from every event dispatcher
1415

1516
6.2
1617
---

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

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,94 +28,108 @@
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+
$dispatchers = $dispatchers ? ['event_dispatcher' => $dispatchers] : [];
43+
}
44+
$this->dispatchers = $dispatchers;
3845
$this->requestStack = $requestStack;
3946
}
4047

4148
public function collect(Request $request, Response $response, \Throwable $exception = null): void
4249
{
4350
$this->currentRequest = $this->requestStack && $this->requestStack->getMainRequest() !== $request ? $request : null;
44-
$this->data = [
45-
'called_listeners' => [],
46-
'not_called_listeners' => [],
47-
'orphaned_events' => [],
48-
];
51+
$this->data = [];
4952
}
5053

5154
public function reset(): void
5255
{
5356
$this->data = [];
5457

55-
if ($this->dispatcher instanceof ResetInterface) {
56-
$this->dispatcher->reset();
58+
foreach ($this->dispatchers as $dispatcher) {
59+
if ($dispatcher instanceof ResetInterface) {
60+
$dispatcher->reset();
61+
}
5762
}
5863
}
5964

6065
public function lateCollect(): void
6166
{
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));
67+
foreach ($this->dispatchers as $name => $dispatcher) {
68+
if (!$dispatcher instanceof TraceableEventDispatcher) {
69+
continue;
70+
}
71+
72+
$this->setCalledListeners($dispatcher->getCalledListeners($this->currentRequest), $name);
73+
$this->setNotCalledListeners($dispatcher->getNotCalledListeners($this->currentRequest), $name);
74+
$this->setOrphanedEvents($dispatcher->getOrphanedEvents($this->currentRequest), $name);
6675
}
6776

6877
$this->data = $this->cloneVar($this->data);
6978
}
7079

80+
public function getData(): array|Data
81+
{
82+
return $this->data;
83+
}
84+
7185
/**
7286
* @see TraceableEventDispatcher
7387
*/
74-
public function setCalledListeners(array $listeners): void
88+
public function setCalledListeners(array $listeners, string $dispatcher = 'event_dispatcher'): void
7589
{
76-
$this->data['called_listeners'] = $listeners;
90+
$this->data[$dispatcher]['called_listeners'] = $listeners;
7791
}
7892

7993
/**
8094
* @see TraceableEventDispatcher
8195
*/
82-
public function getCalledListeners(): array|Data
96+
public function getCalledListeners(string $dispatcher = 'event_dispatcher'): array|Data
8397
{
84-
return $this->data['called_listeners'];
98+
return $this->data[$dispatcher]['called_listeners'] ?? [];
8599
}
86100

87101
/**
88102
* @see TraceableEventDispatcher
89103
*/
90-
public function setNotCalledListeners(array $listeners): void
104+
public function setNotCalledListeners(array $listeners, string $dispatcher = 'event_dispatcher'): void
91105
{
92-
$this->data['not_called_listeners'] = $listeners;
106+
$this->data[$dispatcher]['not_called_listeners'] = $listeners;
93107
}
94108

95109
/**
96110
* @see TraceableEventDispatcher
97111
*/
98-
public function getNotCalledListeners(): array|Data
112+
public function getNotCalledListeners(string $dispatcher = 'event_dispatcher'): array|Data
99113
{
100-
return $this->data['not_called_listeners'];
114+
return $this->data[$dispatcher]['not_called_listeners'] ?? [];
101115
}
102116

103117
/**
104118
* @param array $events An array of orphaned events
105119
*
106120
* @see TraceableEventDispatcher
107121
*/
108-
public function setOrphanedEvents(array $events): void
122+
public function setOrphanedEvents(array $events, string $dispatcher = 'event_dispatcher'): void
109123
{
110-
$this->data['orphaned_events'] = $events;
124+
$this->data[$dispatcher]['orphaned_events'] = $events;
111125
}
112126

113127
/**
114128
* @see TraceableEventDispatcher
115129
*/
116-
public function getOrphanedEvents(): array|Data
130+
public function getOrphanedEvents(string $dispatcher = 'event_dispatcher'): array|Data
117131
{
118-
return $this->data['orphaned_events'];
132+
return $this->data[$dispatcher]['orphaned_events'] ?? [];
119133
}
120134

121135
public function getName(): string

0 commit comments

Comments
 (0)