Augmentation vers version 3.3.0
This commit is contained in:
@@ -29,7 +29,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
protected $logger;
|
||||
protected $stopwatch;
|
||||
|
||||
private $called;
|
||||
private $callStack;
|
||||
private $dispatcher;
|
||||
private $wrappedListeners;
|
||||
|
||||
@@ -38,8 +38,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->stopwatch = $stopwatch;
|
||||
$this->logger = $logger;
|
||||
$this->called = array();
|
||||
$this->wrappedListeners = array();
|
||||
$this->wrappedListeners = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,8 +96,14 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
*/
|
||||
public function getListenerPriority($eventName, $listener)
|
||||
{
|
||||
if (!method_exists($this->dispatcher, 'getListenerPriority')) {
|
||||
return 0;
|
||||
// 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);
|
||||
@@ -117,6 +122,10 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
*/
|
||||
public function dispatch($eventName, Event $event = null)
|
||||
{
|
||||
if (null === $this->callStack) {
|
||||
$this->callStack = new \SplObjectStorage();
|
||||
}
|
||||
|
||||
if (null === $event) {
|
||||
$event = new Event();
|
||||
}
|
||||
@@ -126,19 +135,24 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
}
|
||||
|
||||
$this->preProcess($eventName);
|
||||
$this->preDispatch($eventName, $event);
|
||||
|
||||
$e = $this->stopwatch->start($eventName, 'section');
|
||||
|
||||
$this->dispatcher->dispatch($eventName, $event);
|
||||
|
||||
if ($e->isStarted()) {
|
||||
$e->stop();
|
||||
try {
|
||||
$this->preDispatch($eventName, $event);
|
||||
try {
|
||||
$e = $this->stopwatch->start($eventName, 'section');
|
||||
try {
|
||||
$this->dispatcher->dispatch($eventName, $event);
|
||||
} finally {
|
||||
if ($e->isStarted()) {
|
||||
$e->stop();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
$this->postDispatch($eventName, $event);
|
||||
}
|
||||
} finally {
|
||||
$this->postProcess($eventName);
|
||||
}
|
||||
|
||||
$this->postDispatch($eventName, $event);
|
||||
$this->postProcess($eventName);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
@@ -147,12 +161,15 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
*/
|
||||
public function getCalledListeners()
|
||||
{
|
||||
$called = array();
|
||||
foreach ($this->called as $eventName => $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
|
||||
$called[$eventName.'.'.$info['pretty']] = $info;
|
||||
}
|
||||
if (null === $this->callStack) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$called = [];
|
||||
foreach ($this->callStack as $listener) {
|
||||
list($eventName) = $this->callStack->getInfo();
|
||||
|
||||
$called[] = $listener->getInfo($eventName);
|
||||
}
|
||||
|
||||
return $called;
|
||||
@@ -167,39 +184,43 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
$allListeners = $this->getListeners();
|
||||
} catch (\Exception $e) {
|
||||
if (null !== $this->logger) {
|
||||
$this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
|
||||
$this->logger->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]);
|
||||
}
|
||||
|
||||
// unable to retrieve the uncalled listeners
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
|
||||
$notCalled = array();
|
||||
$calledListeners = [];
|
||||
|
||||
if (null !== $this->callStack) {
|
||||
foreach ($this->callStack as $calledListener) {
|
||||
$calledListeners[] = $calledListener->getWrappedListener();
|
||||
}
|
||||
}
|
||||
|
||||
$notCalled = [];
|
||||
foreach ($allListeners as $eventName => $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$called = false;
|
||||
if (isset($this->called[$eventName])) {
|
||||
foreach ($this->called[$eventName] as $l) {
|
||||
if ($l->getWrappedListener() === $listener) {
|
||||
$called = true;
|
||||
|
||||
break;
|
||||
}
|
||||
if (!\in_array($listener, $calledListeners, true)) {
|
||||
if (!$listener instanceof WrappedListener) {
|
||||
$listener = new WrappedListener($listener, null, $this->stopwatch, $this);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$called) {
|
||||
$info = $this->getListenerInfo($listener, $eventName);
|
||||
$notCalled[$eventName.'.'.$info['pretty']] = $info;
|
||||
$notCalled[] = $listener->getInfo($eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uasort($notCalled, array($this, 'sortListenersByPriority'));
|
||||
uasort($notCalled, [$this, 'sortNotCalledListeners']);
|
||||
|
||||
return $notCalled;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->callStack = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxies all method calls to the original event dispatcher.
|
||||
*
|
||||
@@ -210,7 +231,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
*/
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
return \call_user_func_array(array($this->dispatcher, $method), $arguments);
|
||||
return \call_user_func_array([$this->dispatcher, $method], $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,12 +257,12 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
private function preProcess($eventName)
|
||||
{
|
||||
foreach ($this->dispatcher->getListeners($eventName) as $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 instanceof WrappedListener ? $listener->getWrappedListener() : $listener, null, $this->stopwatch, $this);
|
||||
$this->wrappedListeners[$eventName][] = $wrappedListener;
|
||||
$this->dispatcher->removeListener($eventName, $listener);
|
||||
$this->dispatcher->addListener($eventName, $wrappedListener, $info['priority']);
|
||||
$this->dispatcher->addListener($eventName, $wrappedListener, $priority);
|
||||
$this->callStack->attach($wrappedListener, [$eventName]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,26 +279,25 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
|
||||
$this->dispatcher->removeListener($eventName, $listener);
|
||||
$this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
|
||||
|
||||
$info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
|
||||
if (null !== $this->logger) {
|
||||
$context = ['event' => $eventName, 'listener' => $listener->getPretty()];
|
||||
}
|
||||
|
||||
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])) {
|
||||
$this->called[$eventName] = new \SplObjectStorage();
|
||||
}
|
||||
|
||||
$this->called[$eventName]->attach($listener);
|
||||
} else {
|
||||
$this->callStack->detach($listener);
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -285,75 +305,12 @@ 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 sortNotCalledListeners(array $a, array $b)
|
||||
{
|
||||
$info = array(
|
||||
'event' => $eventName,
|
||||
'priority' => $this->getListenerPriority($eventName, $listener),
|
||||
);
|
||||
|
||||
// unwrap for correct listener info
|
||||
if ($listener instanceof WrappedListener) {
|
||||
$listener = $listener->getWrappedListener();
|
||||
if (0 !== $cmp = strcmp($a['event'], $b['event'])) {
|
||||
return $cmp;
|
||||
}
|
||||
|
||||
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],
|
||||
);
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
private function sortListenersByPriority($a, $b)
|
||||
{
|
||||
if (\is_int($a['priority']) && !\is_int($b['priority'])) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @method reset() Resets the trace.
|
||||
*/
|
||||
interface TraceableEventDispatcherInterface extends EventDispatcherInterface
|
||||
{
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Symfony\Component\EventDispatcher\Debug;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
use Symfony\Component\VarDumper\Caster\ClassStub;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
@@ -26,15 +27,46 @@ class WrappedListener
|
||||
private $stoppedPropagation;
|
||||
private $stopwatch;
|
||||
private $dispatcher;
|
||||
private $pretty;
|
||||
private $stub;
|
||||
private $priority;
|
||||
private static $hasClassStub;
|
||||
|
||||
public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
|
||||
{
|
||||
$this->listener = $listener;
|
||||
$this->name = $name;
|
||||
$this->stopwatch = $stopwatch;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->called = false;
|
||||
$this->stoppedPropagation = false;
|
||||
|
||||
if (\is_array($listener)) {
|
||||
$this->name = \is_object($listener[0]) ? \get_class($listener[0]) : $listener[0];
|
||||
$this->pretty = $this->name.'::'.$listener[1];
|
||||
} elseif ($listener instanceof \Closure) {
|
||||
$r = new \ReflectionFunction($listener);
|
||||
if (false !== strpos($r->name, '{closure}')) {
|
||||
$this->pretty = $this->name = 'closure';
|
||||
} elseif ($class = $r->getClosureScopeClass()) {
|
||||
$this->name = $class->name;
|
||||
$this->pretty = $this->name.'::'.$r->name;
|
||||
} else {
|
||||
$this->pretty = $this->name = $r->name;
|
||||
}
|
||||
} elseif (\is_string($listener)) {
|
||||
$this->pretty = $this->name = $listener;
|
||||
} else {
|
||||
$this->name = \get_class($listener);
|
||||
$this->pretty = $this->name.'::__invoke';
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
if (null === self::$hasClassStub) {
|
||||
self::$hasClassStub = class_exists(ClassStub::class);
|
||||
}
|
||||
}
|
||||
|
||||
public function getWrappedListener()
|
||||
@@ -52,13 +84,35 @@ class WrappedListener
|
||||
return $this->stoppedPropagation;
|
||||
}
|
||||
|
||||
public function getPretty()
|
||||
{
|
||||
return $this->pretty;
|
||||
}
|
||||
|
||||
public function getInfo($eventName)
|
||||
{
|
||||
if (null === $this->stub) {
|
||||
$this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()';
|
||||
}
|
||||
|
||||
return [
|
||||
'event' => $eventName,
|
||||
'priority' => null !== $this->priority ? $this->priority : (null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null),
|
||||
'pretty' => $this->pretty,
|
||||
'stub' => $this->stub,
|
||||
];
|
||||
}
|
||||
|
||||
public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$dispatcher = $this->dispatcher ?: $dispatcher;
|
||||
|
||||
$this->called = true;
|
||||
$this->priority = $dispatcher->getListenerPriority($eventName, $this->listener);
|
||||
|
||||
$e = $this->stopwatch->start($this->name, 'event_listener');
|
||||
|
||||
\call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
|
||||
\call_user_func($this->listener, $event, $eventName, $dispatcher);
|
||||
|
||||
if ($e->isStarted()) {
|
||||
$e->stop();
|
||||
|
||||
Reference in New Issue
Block a user