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-10-17 09:27:10 +01:00
|
|
|
use Symfony\Component\HttpKernel\Debug\Stopwatch;
|
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-10-17 09:27:10 +01:00
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profiler;
|
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;
|
2011-10-17 09:27:10 +01:00
|
|
|
private $stopwatch;
|
2011-11-17 07:26:11 +00:00
|
|
|
private $priorities;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-23 17:02:16 +00:00
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
2011-10-17 09:27:10 +01:00
|
|
|
* @param Stopwatch $stopwatch A Stopwatch instance
|
2011-01-23 17:02:16 +00:00
|
|
|
* @param LoggerInterface $logger A LoggerInterface instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-10-17 09:27:10 +01:00
|
|
|
public function __construct(ContainerInterface $container, Stopwatch $stopwatch, LoggerInterface $logger = null)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-01-23 17:02:16 +00:00
|
|
|
parent::__construct($container);
|
|
|
|
|
2011-10-17 09:27:10 +01:00
|
|
|
$this->stopwatch = $stopwatch;
|
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-10-17 09:27:10 +01:00
|
|
|
public function dispatch($eventName, Event $event = null)
|
|
|
|
{
|
2012-01-30 15:14:38 +00:00
|
|
|
switch ($eventName) {
|
|
|
|
case 'kernel.request':
|
2012-02-06 14:51:54 +00:00
|
|
|
$this->stopwatch->openSection();
|
2012-01-30 15:14:38 +00:00
|
|
|
break;
|
|
|
|
case 'kernel.view':
|
|
|
|
case 'kernel.response':
|
|
|
|
// stop only if a controller has been executed
|
|
|
|
try {
|
|
|
|
$this->stopwatch->stop('controller');
|
|
|
|
} catch (\LogicException $e) {
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'kernel.terminate':
|
2012-02-06 14:51:54 +00:00
|
|
|
$token = $event->getResponse()->headers->get('X-Debug-Token');
|
|
|
|
$this->stopwatch->openSection($token);
|
2012-01-30 15:14:38 +00:00
|
|
|
break;
|
2011-10-17 09:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$e1 = $this->stopwatch->start($eventName, 'section');
|
|
|
|
|
|
|
|
parent::dispatch($eventName, $event);
|
|
|
|
|
|
|
|
$e1->stop();
|
|
|
|
|
2012-01-30 15:14:38 +00:00
|
|
|
switch ($eventName) {
|
|
|
|
case 'kernel.controller':
|
|
|
|
$this->stopwatch->start('controller', 'section');
|
|
|
|
break;
|
|
|
|
case 'kernel.response':
|
|
|
|
$token = $event->getResponse()->headers->get('X-Debug-Token');
|
|
|
|
$this->stopwatch->stopSection($token);
|
|
|
|
$this->updateProfile($token);
|
|
|
|
break;
|
|
|
|
case 'kernel.terminate':
|
2012-02-06 14:51:54 +00:00
|
|
|
$this->stopwatch->stopSection($token);
|
2012-01-30 15:14:38 +00:00
|
|
|
$this->updateProfile($token);
|
|
|
|
break;
|
2011-10-17 09:27:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 08:14:12 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException if the listener method is not callable
|
|
|
|
*/
|
2011-05-05 18:02:52 +01:00
|
|
|
public function addListener($eventName, $listener, $priority = 0)
|
2011-04-06 08:14:12 +01:00
|
|
|
{
|
2011-05-05 18:02:52 +01:00
|
|
|
if (!is_callable($listener)) {
|
2011-11-17 07:26:11 +00:00
|
|
|
throw new \RuntimeException(sprintf('The given callback (%s) for event "%s" is not callable.', $this->getListenerAsString($listener), $eventName));
|
2011-04-06 08:14:12 +01:00
|
|
|
}
|
2011-04-13 13:07:54 +01:00
|
|
|
|
2011-11-17 07:26:11 +00:00
|
|
|
$this->priorities[$eventName.'_'.$this->getListenerAsString($listener)] = $priority;
|
|
|
|
|
2011-05-05 18:02:52 +01:00
|
|
|
parent::addListener($eventName, $listener, $priority);
|
2011-04-06 08:14:12 +01: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-05-05 08:15:48 +01:00
|
|
|
protected function doDispatch($listeners, $eventName, Event $event)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-05-05 08:15:48 +01:00
|
|
|
foreach ($listeners as $listener) {
|
2011-05-05 18:02:52 +01:00
|
|
|
$info = $this->getListenerInfo($listener, $eventName);
|
|
|
|
|
2011-05-05 08:15:48 +01:00
|
|
|
if (null !== $this->logger) {
|
2011-05-05 18:02:52 +01:00
|
|
|
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
|
2011-05-05 08:15:48 +01:00
|
|
|
}
|
2011-03-13 17:10:39 +00:00
|
|
|
|
2011-05-05 18:02:52 +01:00
|
|
|
$this->called[$eventName.'.'.$info['pretty']] = $info;
|
2011-01-24 15:46:04 +00:00
|
|
|
|
2011-12-01 07:44:53 +00:00
|
|
|
$e2 = $this->stopwatch->start(isset($info['class']) ? substr($info['class'], strrpos($info['class'], '\\') + 1) : $info['type'], 'event_listener');
|
2011-10-17 09:27:10 +01:00
|
|
|
|
2011-07-23 09:49:25 +01:00
|
|
|
call_user_func($listener, $event);
|
|
|
|
|
2011-10-17 09:27:10 +01:00
|
|
|
$e2->stop();
|
|
|
|
|
2011-05-05 08:15:48 +01:00
|
|
|
if ($event->isPropagationStopped()) {
|
|
|
|
if (null !== $this->logger) {
|
2011-05-05 18:02:52 +01:00
|
|
|
$this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
|
2011-03-13 17:10:39 +00:00
|
|
|
|
2011-05-05 08:15:48 +01:00
|
|
|
$skippedListeners = $this->getListeners($eventName);
|
|
|
|
$skipped = false;
|
2011-03-13 17:10:39 +00:00
|
|
|
|
2011-05-05 08:15:48 +01:00
|
|
|
foreach ($skippedListeners as $skippedListener) {
|
|
|
|
if ($skipped) {
|
2012-01-30 11:56:55 +00:00
|
|
|
$info = $this->getListenerInfo($skippedListener, $eventName);
|
|
|
|
$this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
|
2011-05-05 08:15:48 +01:00
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
|
2011-05-05 08:15:48 +01:00
|
|
|
if ($skippedListener === $listener) {
|
|
|
|
$skipped = true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-13 17:10:39 +00:00
|
|
|
}
|
2011-05-05 08:15:48 +01:00
|
|
|
|
|
|
|
break;
|
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-10-17 09:27:10 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function lazyLoad($eventName)
|
|
|
|
{
|
|
|
|
$e = $this->stopwatch->start($eventName.'.loading', 'event_listener_loading');
|
|
|
|
|
|
|
|
parent::lazyLoad($eventName);
|
|
|
|
|
|
|
|
$e->stop();
|
|
|
|
}
|
|
|
|
|
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-05-05 18:02:52 +01:00
|
|
|
foreach ($this->getListeners() as $name => $listeners) {
|
|
|
|
foreach ($listeners as $listener) {
|
|
|
|
$info = $this->getListenerInfo($listener, $name);
|
|
|
|
if (!isset($this->called[$name.'.'.$info['pretty']])) {
|
|
|
|
$notCalled[$name.'.'.$info['pretty']] = $info;
|
2010-08-27 10:24:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notCalled;
|
|
|
|
}
|
|
|
|
|
2011-04-06 08:31:06 +01:00
|
|
|
/**
|
|
|
|
* Returns information about the listener
|
2011-04-15 20:12:02 +01:00
|
|
|
*
|
2011-04-06 08:31:06 +01:00
|
|
|
* @param object $listener The listener
|
|
|
|
* @param string $eventName The event name
|
2011-04-15 20:12:02 +01:00
|
|
|
*
|
2011-04-06 08:31:06 +01:00
|
|
|
* @return array Informations about the listener
|
|
|
|
*/
|
|
|
|
private function getListenerInfo($listener, $eventName)
|
2010-02-17 13:54:36 +00:00
|
|
|
{
|
2011-11-17 07:26:11 +00:00
|
|
|
$info = array(
|
|
|
|
'event' => $eventName,
|
|
|
|
'priority' => $this->priorities[$eventName.'_'.$this->getListenerAsString($listener)],
|
|
|
|
);
|
2011-04-04 23:43:53 +01:00
|
|
|
if ($listener instanceof \Closure) {
|
2011-05-05 18:02:52 +01:00
|
|
|
$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 = get_class($listener[0]);
|
2011-04-04 23:43:53 +01:00
|
|
|
try {
|
2011-05-05 18:02:52 +01:00
|
|
|
$r = new \ReflectionMethod($class, $listener[1]);
|
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-03-13 17:10:39 +00:00
|
|
|
}
|
2011-04-06 08:14:12 +01:00
|
|
|
$info += array(
|
|
|
|
'type' => 'Method',
|
|
|
|
'class' => $class,
|
2011-05-05 18:02:52 +01:00
|
|
|
'method' => $listener[1],
|
2011-04-06 08:14:12 +01:00
|
|
|
'file' => $file,
|
2011-05-05 18:02:52 +01:00
|
|
|
'line' => $line,
|
|
|
|
'pretty' => $class.'::'.$listener[1],
|
2011-04-06 08:14:12 +01:00
|
|
|
);
|
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
|
|
|
}
|
2011-10-17 09:27:10 +01:00
|
|
|
|
2012-01-30 15:14:38 +00:00
|
|
|
/**
|
|
|
|
* Updates the profile data.
|
|
|
|
*
|
|
|
|
* @param string $token Profile token
|
|
|
|
*/
|
2011-10-17 09:27:10 +01:00
|
|
|
private function updateProfile($token)
|
|
|
|
{
|
|
|
|
if (!$this->getContainer()->has('profiler')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$profiler = $this->getContainer()->get('profiler');
|
|
|
|
if (!$profile = $profiler->loadProfile($token)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-06 14:51:54 +00:00
|
|
|
$profile->getCollector('time')->setEvents($this->stopwatch->getSectionEvents($token));
|
2011-10-17 09:27:10 +01:00
|
|
|
$profiler->saveProfile($profile);
|
|
|
|
|
|
|
|
// children
|
|
|
|
foreach ($profile->getChildren() as $child) {
|
|
|
|
$child->getCollector('time')->setEvents($this->stopwatch->getSectionEvents($child->getToken()));
|
|
|
|
$profiler->saveProfile($child);
|
|
|
|
}
|
|
|
|
}
|
2011-11-17 07:26:11 +00:00
|
|
|
|
|
|
|
private function getListenerAsString($listener)
|
|
|
|
{
|
|
|
|
if (is_string($listener)) {
|
|
|
|
return '[string] '.$listener;
|
|
|
|
} elseif (is_array($listener)) {
|
|
|
|
return '[array] '.(is_object($listener[0]) ? get_class($listener[0]) : $listener[0]).'::'.$listener[1];
|
|
|
|
} elseif (is_object($listener)) {
|
|
|
|
return '[object] '.get_class($listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '[?] '.var_export($listener, true);
|
|
|
|
}
|
2010-02-17 13:54:36 +00:00
|
|
|
}
|