summaryrefslogtreecommitdiff
path: root/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php')
-rw-r--r--vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php135
1 files changed, 59 insertions, 76 deletions
diff --git a/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php b/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php
index 12e2b1c6..9b5c689a 100644
--- a/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php
+++ b/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php
@@ -33,13 +33,6 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
private $dispatcher;
private $wrappedListeners;
- /**
- * Constructor.
- *
- * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
- * @param Stopwatch $stopwatch A Stopwatch instance
- * @param LoggerInterface $logger A LoggerInterface instance
- */
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
{
$this->dispatcher = $dispatcher;
@@ -102,6 +95,24 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
/**
* {@inheritdoc}
*/
+ public function getListenerPriority($eventName, $listener)
+ {
+ // we might have wrapped listeners for the event (if called while dispatching)
+ // in that case get the priority by wrapper
+ if (isset($this->wrappedListeners[$eventName])) {
+ foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
+ if ($wrappedListener->getWrappedListener() === $listener) {
+ return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
+ }
+ }
+ }
+
+ return $this->dispatcher->getListenerPriority($eventName, $listener);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function hasListeners($eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
@@ -145,8 +156,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
$called = array();
foreach ($this->called as $eventName => $listeners) {
foreach ($listeners as $listener) {
- $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
- $called[$eventName.'.'.$info['pretty']] = $info;
+ $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
}
}
@@ -184,15 +194,24 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
}
if (!$called) {
- $info = $this->getListenerInfo($listener, $eventName);
- $notCalled[$eventName.'.'.$info['pretty']] = $info;
+ if (!$listener instanceof WrappedListener) {
+ $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
+ }
+ $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
}
}
}
+ uasort($notCalled, array($this, 'sortListenersByPriority'));
+
return $notCalled;
}
+ public function reset()
+ {
+ $this->called = array();
+ }
+
/**
* Proxies all method calls to the original event dispatcher.
*
@@ -229,12 +248,11 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
private function preProcess($eventName)
{
foreach ($this->dispatcher->getListeners($eventName) as $listener) {
- $this->dispatcher->removeListener($eventName, $listener);
- $info = $this->getListenerInfo($listener, $eventName);
- $name = isset($info['class']) ? $info['class'] : $info['type'];
- $wrappedListener = new WrappedListener($listener, $name, $this->stopwatch, $this);
+ $priority = $this->getListenerPriority($eventName, $listener);
+ $wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
$this->wrappedListeners[$eventName][] = $wrappedListener;
- $this->dispatcher->addListener($eventName, $wrappedListener);
+ $this->dispatcher->removeListener($eventName, $listener);
+ $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
}
}
@@ -247,13 +265,17 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
continue;
}
// Unwrap listener
+ $priority = $this->getListenerPriority($eventName, $listener);
$this->dispatcher->removeListener($eventName, $listener);
- $this->dispatcher->addListener($eventName, $listener->getWrappedListener());
+ $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
+
+ if (null !== $this->logger) {
+ $context = array('event' => $eventName, 'listener' => $listener->getPretty());
+ }
- $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
if ($listener->wasCalled()) {
if (null !== $this->logger) {
- $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
+ $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
}
if (!isset($this->called[$eventName])) {
@@ -264,12 +286,12 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
}
if (null !== $this->logger && $skipped) {
- $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
+ $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
}
if ($listener->stoppedPropagation()) {
if (null !== $this->logger) {
- $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
+ $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
}
$skipped = true;
@@ -277,63 +299,24 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
}
}
- /**
- * Returns information about the listener.
- *
- * @param object $listener The listener
- * @param string $eventName The event name
- *
- * @return array Information about the listener
- */
- private function getListenerInfo($listener, $eventName)
+ private function sortListenersByPriority($a, $b)
{
- $info = array(
- 'event' => $eventName,
- );
- if ($listener instanceof \Closure) {
- $info += array(
- 'type' => 'Closure',
- 'pretty' => 'closure',
- );
- } elseif (is_string($listener)) {
- try {
- $r = new \ReflectionFunction($listener);
- $file = $r->getFileName();
- $line = $r->getStartLine();
- } catch (\ReflectionException $e) {
- $file = null;
- $line = null;
- }
- $info += array(
- 'type' => 'Function',
- 'function' => $listener,
- 'file' => $file,
- 'line' => $line,
- 'pretty' => $listener,
- );
- } elseif (is_array($listener) || (is_object($listener) && is_callable($listener))) {
- if (!is_array($listener)) {
- $listener = array($listener, '__invoke');
- }
- $class = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
- try {
- $r = new \ReflectionMethod($class, $listener[1]);
- $file = $r->getFileName();
- $line = $r->getStartLine();
- } catch (\ReflectionException $e) {
- $file = null;
- $line = null;
- }
- $info += array(
- 'type' => 'Method',
- 'class' => $class,
- 'method' => $listener[1],
- 'file' => $file,
- 'line' => $line,
- 'pretty' => $class.'::'.$listener[1],
- );
+ if (is_int($a['priority']) && !is_int($b['priority'])) {
+ return 1;
+ }
+
+ if (!is_int($a['priority']) && is_int($b['priority'])) {
+ return -1;
+ }
+
+ if ($a['priority'] === $b['priority']) {
+ return 0;
+ }
+
+ if ($a['priority'] > $b['priority']) {
+ return -1;
}
- return $info;
+ return 1;
}
}