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
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $logger;
|
2010-08-27 10:24:30 +01:00
|
|
|
protected $called;
|
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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-05 07:10:17 +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-05 07:10:17 +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-05 07:10:17 +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-04 23:43:53 +01:00
|
|
|
protected function getListenerInfo($listener, $eventName)
|
|
|
|
{
|
|
|
|
$info = array('event' => $eventName);
|
|
|
|
if ($listener instanceof \Closure) {
|
|
|
|
$info += array('type' => 'Closure');
|
|
|
|
} else {
|
|
|
|
$info += array(
|
|
|
|
'type' => 'Method',
|
|
|
|
'class' => $class = get_class($listener)
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
$r = new \ReflectionMethod($class, $eventName);
|
|
|
|
$info += array(
|
|
|
|
'file' => $r->getFileName(),
|
|
|
|
'line' => $r->getStartLine()
|
|
|
|
);
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
$info += array(
|
|
|
|
'file' => null,
|
|
|
|
'line' => null
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|