8000 feature #24392 Display orphaned events in profiler (kejwmen) · symfony/symfony@0a56a37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a56a37

Browse files
committed
feature #24392 Display orphaned events in profiler (kejwmen)
This PR was squashed before being merged into the 4.1-dev branch (closes #24392). Discussion ---------- Display orphaned events in profiler | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #24347 | License | MIT Implements #24347. Deprecating `TraceableEventDispatcherInterface`. Commits ------- 509f9a9 Display orphaned events in profiler
2 parents d6c05cc + 509f9a9 commit 0a56a37

File tree

11 files changed

+138
-11
lines changed

11 files changed

+138
-11
lines changed

UPGRADE-4.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.0 to 4.1
22
=======================
33

4+
EventDispatcher
5+
---------------
6+
7+
* The `TraceableEventDispatcherInterface` has been deprecated and will be removed in 5.0.
8+
49
HttpFoundation
510
--------------
611

UPGRADE-5.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.x to 5.0
22
=======================
33

4+
EventDispatcher
5+
---------------
6+
7+
* The `TraceableEventDispatcherInterface` has been removed.
8+
49
HttpFoundation
510
--------------
611

src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added information about orphaned events
8+
49
4.0.0
510
-----
611

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@
4545
{% endif %}
4646
</div>
4747
</div>
48+
49+
<div class="tab">
50+
<h3 class="tab-title">Orphaned events <span class="badge">{{ collector.orphanedEvents|length }}</span></h3>
51+
<div class="tab-content">
52+
{% if collector.orphanedEvents is empty %}
53+
<div class="empty">
54+
<p>
55+
<strong>There are no orphaned events</strong>.
56+
</p>
57+
<p>
58+
All dispatched events were handled or an error occurred
59+
when trying to collect orphaned events (in which case check the
60+
logs to get more information).
61+
</p>
62+
</div>
63+
{% else %}
64+
{{ helper.render_table(collector.orphanedEvents) }}
65+
{% endif %}
66+
</div>
67+
</div>
4868
</div>
4969
{% endif %}
5070
{% endblock %}

src/Symfony/Component/EventDispatcher/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* added support for invokable event listeners tagged with `kernel.event_listener` by default
8+
* The `TraceableEventDispatcher::getOrphanedEvents()` method has been added.
9+
* The `TraceableEventDispatcherInterface` has been deprecated and will be removed in 5.0.
810

911
4.0.0
1012
-----

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
3232
private $called;
3333
private $dispatcher;
3434
private $wrappedListeners;
35+
private $orphanedEvents;
3536

3637
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
3738
{
@@ -40,6 +41,7 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
4041
$this->logger = $logger;
4142
$this->called = array();
4243
$this->wrappedListeners = array();
44+
$this->orphanedEvents = array();
4345
}
4446

4547
/**
@@ -207,6 +209,16 @@ public function getNotCalledListeners()
207209
return $notCalled;
208210
}
209211

212+
/**
213+
* Gets the orphaned events.
214+
*
215+
* @return array An array of orphaned events
216+
*/
217+
public function getOrphanedEvents()
218+
{
219+
return $this->orphanedEvents;
220+
}
221+
210222
public function reset()
211223
{
212224
$this->called = array();
@@ -247,6 +259,12 @@ protected function postDispatch($eventName, Event $event)
247259

248260
private function preProcess($eventName)
249261
{
262+
if (!$this->dispatcher->hasListeners($eventName)) {
263+
$this->orphanedEvents[] = $eventName;
264+
265+
return;
266+
}
267+
250268
foreach ($this->dispatcher->getListeners($eventName) as $listener) {
251269
$priority = $this->getListenerPriority($eventName, $listener);
252270
$wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcherInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1515

1616
/**
17+
* @deprecated since version 4.1, will be removed in 5.0.
18+
*
1719
* @author Fabien Potencier <fabien@symfony.com>
1820
*/
1921
interface TraceableEventDispatcherInterface extends EventDispatcherInterface

src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,40 @@ public function testGetCalledListenersNested()
153153
$this->assertCount(2, $dispatcher->getCalledListeners());
154154
}
155155

156+
public function testItReturnsNoOrphanedEventsWhenCreated()
157+
{
158+
// GIVEN
159+
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
160+
// WHEN
161+
$events = $tdispatcher->getOrphanedEvents();
162+
// THEN
163+
$this->assertEmpty($events);
164+
}
165+
166+
public function testItReturnsOrphanedEventsAfterDispatch()
167+
{
168+
// GIVEN
169+
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
170+
$tdispatcher->dispatch('foo');
171+
// WHEN
172+
$events = $tdispatcher->getOrphanedEvents();
173+
// THEN
174+
$this->assertCount(1, $events);
175+
$this->assertEquals(array('foo'), $events);
176+
}
177+
178+
public function testItDoesNotReturnHandledEvents()
179+
{
180+
// GIVEN
181+
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
182+
$tdispatcher->addListener('foo', function () {});
183+
$tdispatcher->dispatch('foo');
184+
// WHEN
185+
$events = $tdispatcher->getOrphanedEvents();
186+
// THEN
187+
$this->assertEmpty($events);
188+
}
189+
156190
public function testLogger()
157191
{
158192
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.1.0
55
-----
66

7+
* added orphaned events support to `EventDataCollector`
78
* `ExceptionListener` now logs and collects exceptions at priority `2048` (previously logged at `-128` and collected at `0`)
89

910
4.0.0

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace Symfony\Component\HttpKernel\DataCollector;
1313

14+
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
15+
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
16+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1417
use Symfony\Component\HttpFoundation\Request;
1518
use Symfony\Component\HttpFoundation\Response;
16-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17-
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
1819

1920
/**
2021
* EventDataCollector.
@@ -38,6 +39,7 @@ public function collect(Request $request, Response $response, \Exception $except
3839
$this->data = array(
3940
'called_listeners' => array(),
4041
'not_called_listeners' => array(),
42+
'orphaned_events' => array(),
4143
);
4244
}
4345

@@ -56,6 +58,11 @@ public function lateCollect()
5658
$this->setCalledListeners($this->dispatcher->getCalledListeners());
5759
$this->setNotCalledListeners($this->dispatcher->getNotCalledListeners());
5860
}
61+
62+
if ($this->dispatcher instanceof TraceableEventDispatcher) {
63+
$this->setOrphanedEvents($this->dispatcher->getOrphanedEvents());
64+
}
65+
5966
$this->data = $this->cloneVar($this->data);
6067
}
6168

@@ -64,7 +71,7 @@ public function lateCollect()
6471
*
6572
* @param array $listeners An array of called listeners
6673
*
67-
* @see TraceableEventDispatcherInterface
74+
* @see TraceableEventDispatcher
6875
*/
6976
public function setCalledListeners(array $listeners)
7077
{
@@ -76,7 +83,7 @@ public function setCalledListeners(array $listeners)
7683
*
7784
* @return array An array of called listeners
7885
*
79-
* @see TraceableEventDispatcherInterface
86+
* @see TraceableEventDispatcher
8087
*/
8188
public function getCalledListeners()
8289
{
@@ -86,9 +93,9 @@ public function getCalledListeners()
8693
/**
8794
* Sets the not called listeners.
8895
*
89-
* @param array $listeners An array of not called listeners
96+
* @param array $listeners
9097
*
91-
* @see TraceableEventDispatcherInterface
98+
* @see TraceableEventDispatcher
9299
*/
93100
public function setNotCalledListeners(array $listeners)
94101
{
@@ -98,15 +105,39 @@ public function setNotCalledListeners(array $listeners)
98105
/**
99106
* Gets the not called listeners.
100107
*
101-
* @return array An array of not called listeners
108+
* @return array
102109
*
103-
* @see TraceableEventDispatcherInterface
110+
* @see TraceableEventDispatcher
104111
*/
105112
public function getNotCalledListeners()
106113
{
107114
return $this->data['not_called_listeners'];
108115
}
109116

117+
/**
118+
* Sets the orphaned events.
119+
*
120+
* @param array $events An array of orphaned events
121+
*
122+
* @see TraceableEventDispatcher
123+
*/
124+
public function setOrphanedEvents(array $events)
125+
{
126+
$this->data['orphaned_events'] = $events;
127+
}
128+
129+
/**
130+
* Gets the orphaned events.
131+
*
132+
* @return array An array of orphaned events
133+
*
134+
* @see TraceableEventDispatcher
135+
*/
136+
public function getOrphanedEvents()
137+
{
138+
return $this->data['orphaned_events'];
139+
}
140+
110141
/**
111142
* {@inheritdoc}
112143
*/

src/Symfony/Component/HttpKernel/Tests/Fixtures/TestEventDispatcher.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
1313

14-
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
15-
use Symfony\Component\EventDispatcher\EventDispatcher;
14+
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
1615

17-
class TestEventDispatcher extends EventDispatcher implements TraceableEventDispatcherInterface
16+
class TestEventDispatcher extends TraceableEventDispatcher
1817
{
1918
public function getCalledListeners()
2019
{
@@ -29,4 +28,9 @@ public function getNotCalledListeners()
2928
public function reset()
3029
{
3130
}
31+
32+
public function getOrphanedEvents()
33+
{
34+
return array();
35+
}
3236
}

0 commit comments

Comments
 (0)
0