This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Debug/EventDispatcher.php

158 lines
4.4 KiB
PHP
Raw Normal View History

<?php
removed Symfony\Framework Things have been moved to Symfony\Component\HttpKernel and Symfony\Bundle\FrameworkBundle The kernel configuration namespace was removed and merged with the main web configuration namespace (kernel:config => web:config, kernel:test => web:test, and kernel:session => web:session): Before: <kernel:config charset="UTF-8" error_handler="null" /> <web:config csrf-secret="xxxxxxxxxx"> <web:router resource="%kernel.root_dir%/config/routing.xml" /> <web:validation enabled="true" annotations="true" /> </web:config> After: <web:config csrf-secret="xxxxxxxxxx" charset="UTF-8" error-handler="null"> <web:router resource="%kernel.root_dir%/config/routing.xml" /> <web:validation enabled="true" annotations="true" /> </web:config> Renamed classes: Symfony\{Framework => Bundle\FrameworkBundle}\Cache\Cache Symfony\{Framework => Bundle\FrameworkBundle}\Client Symfony\{Framework => Bundle\FrameworkBundle}\Debug\EventDispatcher Symfony\{Framework => Bundle\FrameworkBundle}\Debug\EventDispatcherTraceableInterface Symfony\{Framework => Bundle\FrameworkBundle}\EventDispatcher Symfony\{Framework => Component\HttpFoundation}\UniversalClassLoader Symfony\{Framework => Component\HttpKernel}\Bundle\Bundle Symfony\{Framework => Component\HttpKernel}\Bundle\BundleInterface Symfony\{Framework => Component\HttpKernel}\ClassCollectionLoader Symfony\{Framework => Component\HttpKernel}\Debug\ErrorException Symfony\{Framework => Component\HttpKernel}\Debug\ErrorHandler Symfony\{Bundle\FrameworkBundle => Component\HttpKernel}\Debug\ExceptionListener Symfony\{Framework => Component\HttpKernel}\Kernel
2010-09-15 19:49:16 +01:00
namespace Symfony\Bundle\FrameworkBundle\Debug;
removed Symfony\Framework Things have been moved to Symfony\Component\HttpKernel and Symfony\Bundle\FrameworkBundle The kernel configuration namespace was removed and merged with the main web configuration namespace (kernel:config => web:config, kernel:test => web:test, and kernel:session => web:session): Before: <kernel:config charset="UTF-8" error_handler="null" /> <web:config csrf-secret="xxxxxxxxxx"> <web:router resource="%kernel.root_dir%/config/routing.xml" /> <web:validation enabled="true" annotations="true" /> </web:config> After: <web:config csrf-secret="xxxxxxxxxx" charset="UTF-8" error-handler="null"> <web:router resource="%kernel.root_dir%/config/routing.xml" /> <web:validation enabled="true" annotations="true" /> </web:config> Renamed classes: Symfony\{Framework => Bundle\FrameworkBundle}\Cache\Cache Symfony\{Framework => Bundle\FrameworkBundle}\Client Symfony\{Framework => Bundle\FrameworkBundle}\Debug\EventDispatcher Symfony\{Framework => Bundle\FrameworkBundle}\Debug\EventDispatcherTraceableInterface Symfony\{Framework => Bundle\FrameworkBundle}\EventDispatcher Symfony\{Framework => Component\HttpFoundation}\UniversalClassLoader Symfony\{Framework => Component\HttpKernel}\Bundle\Bundle Symfony\{Framework => Component\HttpKernel}\Bundle\BundleInterface Symfony\{Framework => Component\HttpKernel}\ClassCollectionLoader Symfony\{Framework => Component\HttpKernel}\Debug\ErrorException Symfony\{Framework => Component\HttpKernel}\Debug\ErrorHandler Symfony\{Bundle\FrameworkBundle => Component\HttpKernel}\Debug\ExceptionListener Symfony\{Framework => Component\HttpKernel}\Kernel
2010-09-15 19:49:16 +01:00
use Symfony\Bundle\FrameworkBundle\EventDispatcher as BaseEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
2010-09-20 09:56:35 +01:00
use Symfony\Component\HttpKernel\Debug\EventDispatcherTraceableInterface;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* EventDispatcher extends the original EventDispatcher class to add some debugging tools.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class EventDispatcher extends BaseEventDispatcher implements EventDispatcherTraceableInterface
{
protected $logger;
protected $called;
/**
* Constructor.
*
* @param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->called = array();
}
/**
* {@inheritDoc}
*/
public function notify(Event $event)
{
foreach ($this->getListeners($event->getName()) as $listener) {
$this->addCall($event, $listener, 'notify');
call_user_func($listener, $event);
}
return $event;
}
/**
* {@inheritDoc}
*/
public function notifyUntil(Event $event)
{
foreach ($this->getListeners($event->getName()) as $i => $listener) {
$this->addCall($event, $listener, 'notifyUntil');
if (call_user_func($listener, $event)) {
if (null !== $this->logger) {
$this->logger->debug(sprintf('Listener "%s" processed the event "%s"', $this->listenerToString($listener), $event->getName()));
$listeners = $this->getListeners($event->getName());
while (++$i < count($listeners)) {
$this->logger->debug(sprintf('Listener "%s" was not called for event "%s"', $this->listenerToString($listeners[$i]), $event->getName()));
}
}
$event->setProcessed(true);
break;
}
}
return $event;
}
/**
* {@inheritDoc}
*/
public function filter(Event $event, $value)
{
foreach ($this->getListeners($event->getName()) as $listener) {
$this->addCall($event, $listener, 'filter');
$value = call_user_func($listener, $event, $value);
}
$event->setReturnValue($value);
return $event;
}
/**
* {@inheritDoc}
*/
public function getCalledListeners()
{
return $this->called;
}
/**
* {@inheritDoc}
*/
public function getNotCalledListeners()
{
$notCalled = array();
foreach (array_keys($this->listeners) as $name) {
foreach ($this->getListeners($name) as $listener) {
$listener = $this->listenerToString($listener);
if (!isset($this->called[$name.'.'.$listener])) {
$notCalled[] = array(
'event' => $name,
'listener' => $listener,
);
}
}
}
return $notCalled;
}
protected function listenerToString($listener)
{
if (is_object($listener) && $listener instanceof \Closure) {
return 'Closure';
}
if (is_string($listener)) {
return $listener;
}
if (is_array($listener)) {
return sprintf('%s::%s', is_object($listener[0]) ? get_class($listener[0]) : $listener[0], $listener[1]);
}
}
protected function addCall(Event $event, $listener, $type)
{
$listener = $this->listenerToString($listener);
if (null !== $this->logger) {
$this->logger->debug(sprintf('Notified event "%s" to listener "%s" (%s)', $event->getName(), $listener, $type));
}
$this->called[$event->getName().'.'.$listener] = array(
'event' => $event->getName(),
'caller' => null !== $event->getSubject() ? get_class($event->getSubject()) : null,
'listener' => $listener,
);
}
}