Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Display orphaned events in profiler #24392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 UPGRADE-4.1.md
Original file line number Diff line number Diff line change
@@ -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 5 UPGRADE-5.0.md
Original file line number Diff line number Diff line change
@@ -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 5 src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* added information about orphaned events

4.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
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
6 changes: 6 additions & 0 deletions 6 src/Symfony/Component/EventDispatcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

4.1.0
-----

* The `TraceableEventDispatcher::getOrphanedEvents()` method has been added.
* The `TraceableEventDispatcherInterface` has been deprecated and will be removed in 5.0.

4.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 1 src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.