2010-02-17 13:54:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Debug;
|
|
|
|
|
2011-03-13 18:16:56 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\HttpKernel\Log\LoggerInterface;
|
2011-03-13 18:16:56 +00:00
|
|
|
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcherInterface;
|
2011-01-23 17:02:16 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2011-03-13 18:16:56 +00:00
|
|
|
use Symfony\Component\EventDispatcher\Event;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:54:36 +00:00
|
|
|
/**
|
2011-03-13 18:16:56 +00:00
|
|
|
* Extends the ContainerAwareEventDispatcher to add some debugging tools.
|
2010-02-17 13:54:36 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:54:36 +00:00
|
|
|
*/
|
2011-03-13 18:16:56 +00:00
|
|
|
class TraceableEventDispatcher extends ContainerAwareEventDispatcher implements TraceableEventDispatcherInterface
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-04-06 08:35:15 +01:00
|
|
|
private $logger;
|
|
|
|
private $called;
|
|
|
|
private $container;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-23 17:02:16 +00:00
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
|
|
|
* @param LoggerInterface $logger A LoggerInterface instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-23 17:02:16 +00:00
|
|
|
public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-01-23 17:02:16 +00:00
|
|
|
parent::__construct($container);
|
|
|
|
|
2011-04-06 08:14:12 +01:00
|
|
|
$this->container = $container;
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->logger = $logger;
|
2010-08-27 10:24:30 +01:00
|
|
|
$this->called = array();
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 08:14:12 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException if the listener method is not callable
|
|
|
|
*/
|
|
|
|
public function addListener($eventNames, $listener, $priority = 0)
|
|
|
|
{
|
|
|
|
if (!$listener instanceof \Closure) {
|
|
|
|
foreach ((array) $eventNames as $method) {
|
|
|
|
if (!is_callable(array($listener, $method))) {
|
2011-04-13 13:07:54 +01:00
|
|
|
$msg = sprintf('The event method "%s()" is not callable on the class "%s".', $method, get_class($listener));
|
2011-04-06 08:14:12 +01:00
|
|
|
if (null !== $this->logger) {
|
|
|
|
$this->logger->err($msg);
|
|
|
|
}
|
|
|
|
throw new \RuntimeException($msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 13:07:54 +01:00
|
|
|
|
2011-04-06 08:14:12 +01:00
|
|
|
parent::addListener($eventNames, $listener, $priority);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2011-01-09 10:25:50 +00:00
|
|
|
* {@inheritDoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-13 18:16:56 +00:00
|
|
|
protected function triggerListener($listener, $eventName, Event $event)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-03-13 18:16:56 +00:00
|
|
|
parent::triggerListener($listener, $eventName, $event);
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2011-03-13 17:10:39 +00:00
|
|
|
if (null !== $this->logger) {
|
2011-04-13 13:07:54 +01:00
|
|
|
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, get_class($listener)));
|
2011-03-13 17:10:39 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 07:10:17 +01:00
|
|
|
$this->called[$eventName.'.'.get_class($listener)] = $this->getListenerInfo($listener, $eventName);
|
2011-01-24 15:46:04 +00:00
|
|
|
|
2011-03-13 18:16:56 +00:00
|
|
|
if ($event->isPropagationStopped() && null !== $this->logger) {
|
2011-04-13 13:07:54 +01:00
|
|
|
$this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', get_class($listener), $eventName));
|
2011-03-13 17:10:39 +00:00
|
|
|
|
|
|
|
$skippedListeners = $this->getListeners($eventName);
|
|
|
|
$skipped = false;
|
|
|
|
|
|
|
|
foreach ($skippedListeners as $skippedListener) {
|
|
|
|
if ($skipped) {
|
2011-04-13 13:07:54 +01:00
|
|
|
$this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', get_class($skippedListener), $eventName));
|
2011-03-13 17:10:39 +00:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2011-03-13 17:10:39 +00:00
|
|
|
if ($skippedListener === $listener) {
|
2011-04-04 18:22:55 +01:00
|
|
|
$skipped = true;
|
2011-03-13 17:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-09 10:25:50 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getCalledListeners()
|
2010-08-27 10:24:30 +01:00
|
|
|
{
|
|
|
|
return $this->called;
|
|
|
|
}
|
|
|
|
|
2011-01-09 10:25:50 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getNotCalledListeners()
|
2010-08-27 10:24:30 +01:00
|
|
|
{
|
|
|
|
$notCalled = array();
|
2011-03-18 06:42:41 +00:00
|
|
|
foreach (array_keys($this->getListeners()) as $name) {
|
2010-08-27 10:24:30 +01:00
|
|
|
foreach ($this->getListeners($name) as $listener) {
|
2011-04-05 07:10:17 +01:00
|
|
|
if (!isset($this->called[$name.'.'.get_class($listener)])) {
|
|
|
|
$notCalled[$name.'.'.get_class($listener)] = $this->getListenerInfo($listener, $name);
|
2010-08-27 10:24:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notCalled;
|
|
|
|
}
|
|
|
|
|
2011-04-06 08:31:06 +01:00
|
|
|
/**
|
|
|
|
* Returns information about the listener
|
|
|
|
*
|
|
|
|
* @param object $listener The listener
|
|
|
|
* @param string $eventName The event name
|
|
|
|
*
|
|
|
|
* @return array Informations about the listener
|
|
|
|
*/
|
|
|
|
private function getListenerInfo($listener, $eventName)
|
2011-04-04 23:43:53 +01:00
|
|
|
{
|
|
|
|
$info = array('event' => $eventName);
|
|
|
|
if ($listener instanceof \Closure) {
|
|
|
|
$info += array('type' => 'Closure');
|
|
|
|
} else {
|
2011-04-06 08:14:12 +01:00
|
|
|
$class = get_class($listener);
|
2011-04-04 23:43:53 +01:00
|
|
|
try {
|
|
|
|
$r = new \ReflectionMethod($class, $eventName);
|
2011-04-06 08:14:12 +01:00
|
|
|
$file = $r->getFileName();
|
|
|
|
$line = $r->getStartLine();
|
2011-04-04 23:43:53 +01:00
|
|
|
} catch (\ReflectionException $e) {
|
2011-04-06 08:14:12 +01:00
|
|
|
$file = null;
|
|
|
|
$line = null;
|
2011-04-04 23:43:53 +01:00
|
|
|
}
|
2011-04-06 08:14:12 +01:00
|
|
|
$info += array(
|
|
|
|
'type' => 'Method',
|
|
|
|
'class' => $class,
|
|
|
|
'file' => $file,
|
|
|
|
'line' => $line
|
|
|
|
);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 23:43:53 +01:00
|
|
|
return $info;
|
2010-08-27 10:24:30 +01:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|