8000 [HttpKernel] fixed wrong reference in TraceableEventDispatcher by fabpot · Pull Request #10191 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] fixed wrong reference in TraceableEventDispatcher #10191

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

Merged
merged 1 commit into from
Feb 4, 2014
Merged
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
66 changes: 35 additions & 31 deletions src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
private $dispatcher;
private $wrappedListeners;
private $firstCalledEvent;
private $id;
private $lastEventId = 0;

/**
Expand Down Expand Up @@ -125,9 +124,9 @@ public function dispatch($eventName, Event $event = null)
$event = new Event();
}

$this->id = $eventId = ++$this->lastEventId;
$eventId = ++$this->lastEventId;

$this->preDispatch($eventName, $event);
$this->preDispatch($eventName, $eventId, $event);

$e = $this->stopwatch->start($eventName, 'section');

Expand All @@ -139,14 +138,11 @@ public function dispatch($eventName, Event $event = null)

$this->dispatcher->dispatch($eventName, $event);

// reset the id as another event might have been dispatched during the dispatching of this event
$this->id = $eventId;

unset($this->firstCalledEvent[$eventName]);

$e->stop();

$this->postDispatch($eventName, $event);
$this->postDispatch($eventName, $eventId, $event);

return $event;
}
Expand All @@ -168,7 +164,7 @@ public function getNotCalledListeners()

foreach ($this->getListeners() as $name => $listeners) {
foreach ($listeners as $listener) {
$info = $this->getListenerInfo($listener, $name);
$info = $this->getListenerInfo($listener, null, $name);
if (!isset($this->called[$name.'.'.$info['pretty']])) {
$notCalled[$name.'.'.$info['pretty']] = $info;
}
Expand Down Expand Up @@ -198,24 +194,24 @@ public function __call($method, $arguments)
* Whenever Symfony will require PHP 5.4, this could be changed
* to a proper private method.
*/
public function logSkippedListeners($eventName, Event $event, $listener)
public function logSkippedListeners($eventName, $eventId, Event $event, $listener)
{
if (null === $this->logger) {
return;
}

$info = $this->getListenerInfo($listener, $eventName);
$info = $this->getListenerInfo($listener, $eventId, $eventName);

$this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));

$skippedListeners = $this->getListeners($eventName);
$skipped = false;

foreach ($skippedListeners as $skippedListener) {
$skippedListener = $this->unwrapListener($skippedListener);
$skippedListener = $this->unwrapListener($skippedListener, $eventId);

if ($skipped) {
$info = $this->getListenerInfo($skippedListener, $eventName);
$info = $this->getListenerInfo($skippedListener, $eventId, $eventName);
$this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
}

Expand All @@ -232,7 +228,7 @@ public function logSkippedListeners($eventName, Event $event, $listener)
* Whenever Symfony will require PHP 5.4, this could be changed
* to a proper private method.
*/
public function preListenerCall($eventName, $listener)
public function preListenerCall($eventName, $eventId, $listener)
{
// is it the first called listener?
if (isset($this->firstCalledEvent[$eventName])) {
Expand All @@ -241,7 +237,7 @@ public function preListenerCall($eventName, $listener)
unset($this->firstCalledEvent[$eventName]);
}

$info = $this->getListenerInfo($listener, $eventName);
$info = $this->getListenerInfo($listener, $eventId, $eventName);

if (null !== $this->logger) {
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
Expand All @@ -260,9 +256,9 @@ public function preListenerCall($eventName, $listener)
*
* @return array Information about the listener
*/
private function getListenerInfo($listener, $eventName)
private function getListenerInfo($listener, $eventId, $eventName)
{
$listener = $this->unwrapListener($listener);
$listener = $this->unwrapListener($listener, $eventId);

$info = array(
'event' => $eventName,
Expand Down Expand Up @@ -365,17 +361,17 @@ private function saveInfoInProfile(Profile $profile, $updateChildren)
}
}

private function preDispatch($eventName, Event $event)
private function preDispatch($eventName, $eventId, Event $event)
{
// wrap all listeners before they are called
$this->wrappedListeners[$this->id] = new \SplObjectStorage();
$this->wrappedListeners[$eventId] = new \SplObjectStorage();

$listeners = $this->dispatcher->getListeners($eventName);

foreach ($listeners as $listener) {
$this->dispatcher->removeListener($eventName, $listener);
$wrapped = $this->wrapListener($eventName, $listener);
$this->wrappedListeners[$this->id][$wrapped] = $listener;
$wrapped = $this->wrapListener($eventName, $eventId, $listener);
$this->wrappedListeners[$eventId][$wrapped] = $listener;
$this->dispatcher->addListener($eventName, $wrapped);
}

Expand Down Expand Up @@ -404,7 +400,7 @@ private function preDispatch($eventName, Event $event)
}
}

private function postDispatch($eventName, Event $event)
private function postDispatch($eventName, $eventId, Event $event)
{
switch ($eventName) {
case KernelEvents::CONTROLLER:
Expand Down Expand Up @@ -433,36 +429,44 @@ private function postDispatch($eventName, Event $event)
break;
}

foreach ($this->wrappedListeners[$this->id] as $wrapped) {
foreach ($this->wrappedListeners[$eventId] as $wrapped) {
$this->dispatcher->removeListener($eventName, $wrapped);
$this->dispatcher->addListener($eventName, $this->wrappedListeners[$this->id][$wrapped]);
$this->dispatcher->addListener($eventName, $this->wrappedListeners[$eventId][$wrapped]);
}

unset($this->wrappedListeners[$this->id]);
unset($this->wrappedListeners[$eventId]);
}

private function wrapListener($eventName, $listener)
private function wrapListener($eventName, $eventId, $listener)
{
$self = $this;

return function (Event $event) use ($self, $eventName, $listener) {
$e = $self->preListenerCall($eventName, $listener);
return function (Event $event) use ($self, $eventName, $eventId, $listener) {
$e = $self->preListenerCall($eventName, $eventId, $listener);

call_user_func($listener, $event);

$e->stop();

if ($event->isPropagationStopped()) {
$self->logSkippedListeners($eventName, $event, $listener);
$self->logSkippedListeners($eventName, $eventId, $event, $listener);
}
};
}

private function unwrapListener($listener)
private function unwrapListener($listener, $eventId)
{
// get the original listener
if (is_object($listener) && isset($this->wrappedListeners[$this->id][$listener])) {
return $this->wrappedListeners[$this->id][$listener];
if (is_object($listener)) {
if (null === $eventId) {
foreach (array_keys($this->wrappedListeners) as $eventId) {
if (isset($this->wrappedListeners[$eventId][$listener])) {
return $this->wrappedListeners[$eventId][$listener];
}
}
} elseif (isset($this->wrappedListeners[$eventId][$listener])) {
return $this->wrappedListeners[$eventId][$listener];
}
}

return $listener;
Expand Down
0