Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\Debug; Chris@0: Chris@0: use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher; Chris@17: use Symfony\Component\EventDispatcher\Event; Chris@0: use Symfony\Component\HttpKernel\KernelEvents; Chris@0: Chris@0: /** Chris@0: * Collects some data about event listeners. Chris@0: * Chris@0: * This event dispatcher delegates the dispatching to another one. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class TraceableEventDispatcher extends BaseTraceableEventDispatcher Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function preDispatch($eventName, Event $event) Chris@0: { Chris@0: switch ($eventName) { Chris@0: case KernelEvents::REQUEST: Chris@0: $this->stopwatch->openSection(); Chris@0: break; Chris@0: case KernelEvents::VIEW: Chris@0: case KernelEvents::RESPONSE: Chris@0: // stop only if a controller has been executed Chris@0: if ($this->stopwatch->isStarted('controller')) { Chris@0: $this->stopwatch->stop('controller'); Chris@0: } Chris@0: break; Chris@0: case KernelEvents::TERMINATE: Chris@0: $token = $event->getResponse()->headers->get('X-Debug-Token'); Chris@0: // There is a very special case when using built-in AppCache class as kernel wrapper, in the case Chris@0: // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A]. Chris@0: // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID Chris@0: // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception Chris@0: // which must be caught. Chris@0: try { Chris@0: $this->stopwatch->openSection($token); Chris@0: } catch (\LogicException $e) { Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function postDispatch($eventName, Event $event) Chris@0: { Chris@0: switch ($eventName) { Chris@0: case KernelEvents::CONTROLLER_ARGUMENTS: Chris@0: $this->stopwatch->start('controller', 'section'); Chris@0: break; Chris@0: case KernelEvents::RESPONSE: Chris@0: $token = $event->getResponse()->headers->get('X-Debug-Token'); Chris@0: $this->stopwatch->stopSection($token); Chris@0: break; Chris@0: case KernelEvents::TERMINATE: Chris@0: // In the special case described in the `preDispatch` method above, the `$token` section Chris@0: // does not exist, then closing it throws an exception which must be caught. Chris@0: $token = $event->getResponse()->headers->get('X-Debug-Token'); Chris@0: try { Chris@0: $this->stopwatch->stopSection($token); Chris@0: } catch (\LogicException $e) { Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: }