Skip to content

Commit

Permalink
feature #24392 Display orphaned events in profiler (kejwmen)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jan 19, 2018
2 parents d6c05cc + 509f9a9 commit 0a56a37
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 11 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-4.1.md
@@ -1,6 +1,11 @@
UPGRADE FROM 4.0 to 4.1
=======================

EventDispatcher
---------------

* The `TraceableEventDispatcherInterface` has been deprecated and will be removed in 5.0.

HttpFoundation
--------------

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-5.0.md
@@ -1,6 +1,11 @@
UPGRADE FROM 4.x to 5.0
=======================

EventDispatcher
---------------

* The `TraceableEventDispatcherInterface` has been removed.

HttpFoundation
--------------

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* added information about orphaned events

4.0.0
-----

Expand Down
Expand Up @@ -45,6 +45,26 @@
{% endif %}
</div>
</div>

<div class="tab">
<h3 class="tab-title">Orphaned events <span class="badge">{{ collector.orphanedEvents|length }}</span></h3>
<div class="tab-content">
{% if collector.orphanedEvents is empty %}
<div class="empty">
<p>
<strong>There are no orphaned events</strong>.
</p>
<p>
All dispatched events were handled or an error occurred
when trying to collect orphaned events (in which case check the
logs to get more information).
</p>
</div>
{% else %}
{{ helper.render_table(collector.orphanedEvents) }}
{% endif %}
</div>
</div>
</div>
{% endif %}
{% endblock %}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/EventDispatcher/CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ CHANGELOG
-----

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

4.0.0
-----
Expand Down
Expand Up @@ -32,6 +32,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
private $called;
private $dispatcher;
private $wrappedListeners;
private $orphanedEvents;

public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
{
Expand All @@ -40,6 +41,7 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
$this->logger = $logger;
$this->called = array();
$this->wrappedListeners = array();
$this->orphanedEvents = array();
}

/**
Expand Down Expand Up @@ -207,6 +209,16 @@ public function getNotCalledListeners()
return $notCalled;
}

/**
* Gets the orphaned events.
*
* @return array An array of orphaned events
*/
public function getOrphanedEvents()
{
return $this->orphanedEvents;
}

public function reset()
{
$this->called = array();
Expand Down Expand Up @@ -247,6 +259,12 @@ protected function postDispatch($eventName, Event $event)

private function preProcess($eventName)
{
if (!$this->dispatcher->hasListeners($eventName)) {
$this->orphanedEvents[] = $eventName;

return;
}

foreach ($this->dispatcher->getListeners($eventName) as $listener) {
$priority = $this->getListenerPriority($eventName, $listener);
$wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
Expand Down
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @deprecated since version 4.1, will be removed in 5.0.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface TraceableEventDispatcherInterface extends EventDispatcherInterface
Expand Down
Expand Up @@ -153,6 +153,40 @@ public function testGetCalledListenersNested()
$this->assertCount(2, $dispatcher->getCalledListeners());
}

public function testItReturnsNoOrphanedEventsWhenCreated()
{
// GIVEN
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
// WHEN
$events = $tdispatcher->getOrphanedEvents();
// THEN
$this->assertEmpty($events);
}

public function testItReturnsOrphanedEventsAfterDispatch()
{
// GIVEN
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$tdispatcher->dispatch('foo');
// WHEN
$events = $tdispatcher->getOrphanedEvents();
// THEN
$this->assertCount(1, $events);
$this->assertEquals(array('foo'), $events);
}

public function testItDoesNotReturnHandledEvents()
{
// GIVEN
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$tdispatcher->addListener('foo', function () {});
$tdispatcher->dispatch('foo');
// WHEN
$events = $tdispatcher->getOrphanedEvents();
// THEN
$this->assertEmpty($events);
}

public function testLogger()
{
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.1.0
-----

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

4.0.0
Expand Down
Expand Up @@ -11,10 +11,11 @@

namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;

/**
* EventDataCollector.
Expand All @@ -38,6 +39,7 @@ public function collect(Request $request, Response $response, \Exception $except
$this->data = array(
'called_listeners' => array(),
'not_called_listeners' => array(),
'orphaned_events' => array(),
);
}

Expand All @@ -56,6 +58,11 @@ public function lateCollect()
$this->setCalledListeners($this->dispatcher->getCalledListeners());
$this->setNotCalledListeners($this->dispatcher->getNotCalledListeners());
}

if ($this->dispatcher instanceof TraceableEventDispatcher) {
$this->setOrphanedEvents($this->dispatcher->getOrphanedEvents());
}

$this->data = $this->cloneVar($this->data);
}

Expand All @@ -64,7 +71,7 @@ public function lateCollect()
*
* @param array $listeners An array of called listeners
*
* @see TraceableEventDispatcherInterface
* @see TraceableEventDispatcher
*/
public function setCalledListeners(array $listeners)
{
Expand All @@ -76,7 +83,7 @@ public function setCalledListeners(array $listeners)
*
* @return array An array of called listeners
*
* @see TraceableEventDispatcherInterface
* @see TraceableEventDispatcher
*/
public function getCalledListeners()
{
Expand All @@ -86,9 +93,9 @@ public function getCalledListeners()
/**
* Sets the not called listeners.
*
* @param array $listeners An array of not called listeners
* @param array $listeners
*
* @see TraceableEventDispatcherInterface
* @see TraceableEventDispatcher
*/
public function setNotCalledListeners(array $listeners)
{
Expand All @@ -98,15 +105,39 @@ public function setNotCalledListeners(array $listeners)
/**
* Gets the not called listeners.
*
* @return array An array of not called listeners
* @return array
*
* @see TraceableEventDispatcherInterface
* @see TraceableEventDispatcher
*/
public function getNotCalledListeners()
{
return $this->data['not_called_listeners'];
}

/**
* Sets the orphaned events.
*
* @param array $events An array of orphaned events
*
* @see TraceableEventDispatcher
*/
public function setOrphanedEvents(array $events)
{
$this->data['orphaned_events'] = $events;
}

/**
* Gets the orphaned events.
*
* @return array An array of orphaned events
*
* @see TraceableEventDispatcher
*/
public function getOrphanedEvents()
{
return $this->data['orphaned_events'];
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -11,10 +11,9 @@

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;

class TestEventDispatcher extends EventDispatcher implements TraceableEventDispatcherInterface
class TestEventDispatcher extends TraceableEventDispatcher
{
public function getCalledListeners()
{
Expand All @@ -29,4 +28,9 @@ public function getNotCalledListeners()
public function reset()
{
}

public function getOrphanedEvents()
{
return array();
}
}

0 comments on commit 0a56a37

Please sign in to comment.