2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\EventDispatcher\Debug ;
2018-11-23 12:29:20 +00:00
use Psr\Log\LoggerInterface ;
use Symfony\Component\EventDispatcher\Event ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface ;
use Symfony\Component\EventDispatcher\EventSubscriberInterface ;
use Symfony\Component\Stopwatch\Stopwatch ;
/**
* Collects some data about event listeners .
*
* This event dispatcher delegates the dispatching to another one .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class TraceableEventDispatcher implements TraceableEventDispatcherInterface
{
protected $logger ;
protected $stopwatch ;
2019-01-24 08:00:03 +00:00
private $callStack ;
2015-08-17 17:00:26 -07:00
private $dispatcher ;
private $wrappedListeners ;
public function __construct ( EventDispatcherInterface $dispatcher , Stopwatch $stopwatch , LoggerInterface $logger = null )
{
$this -> dispatcher = $dispatcher ;
$this -> stopwatch = $stopwatch ;
$this -> logger = $logger ;
$this -> wrappedListeners = array ();
}
/**
* { @ inheritdoc }
*/
public function addListener ( $eventName , $listener , $priority = 0 )
{
$this -> dispatcher -> addListener ( $eventName , $listener , $priority );
}
/**
* { @ inheritdoc }
*/
public function addSubscriber ( EventSubscriberInterface $subscriber )
{
$this -> dispatcher -> addSubscriber ( $subscriber );
}
/**
* { @ inheritdoc }
*/
public function removeListener ( $eventName , $listener )
{
if ( isset ( $this -> wrappedListeners [ $eventName ])) {
foreach ( $this -> wrappedListeners [ $eventName ] as $index => $wrappedListener ) {
if ( $wrappedListener -> getWrappedListener () === $listener ) {
$listener = $wrappedListener ;
unset ( $this -> wrappedListeners [ $eventName ][ $index ]);
break ;
}
}
}
return $this -> dispatcher -> removeListener ( $eventName , $listener );
}
/**
* { @ inheritdoc }
*/
public function removeSubscriber ( EventSubscriberInterface $subscriber )
{
return $this -> dispatcher -> removeSubscriber ( $subscriber );
}
/**
* { @ inheritdoc }
*/
public function getListeners ( $eventName = null )
{
return $this -> dispatcher -> getListeners ( $eventName );
}
2016-04-20 09:56:34 -07:00
/**
* { @ inheritdoc }
*/
public function getListenerPriority ( $eventName , $listener )
{
2018-11-23 12:29:20 +00:00
// we might have wrapped listeners for the event (if called while dispatching)
// in that case get the priority by wrapper
if ( isset ( $this -> wrappedListeners [ $eventName ])) {
foreach ( $this -> wrappedListeners [ $eventName ] as $index => $wrappedListener ) {
if ( $wrappedListener -> getWrappedListener () === $listener ) {
return $this -> dispatcher -> getListenerPriority ( $eventName , $wrappedListener );
}
}
2017-02-02 16:28:38 -08:00
}
2016-04-20 09:56:34 -07:00
return $this -> dispatcher -> getListenerPriority ( $eventName , $listener );
}
2015-08-17 17:00:26 -07:00
/**
* { @ inheritdoc }
*/
public function hasListeners ( $eventName = null )
{
return $this -> dispatcher -> hasListeners ( $eventName );
}
/**
* { @ inheritdoc }
*/
public function dispatch ( $eventName , Event $event = null )
{
2019-01-24 08:00:03 +00:00
if ( null === $this -> callStack ) {
$this -> callStack = new \SplObjectStorage ();
}
2015-08-17 17:00:26 -07:00
if ( null === $event ) {
$event = new Event ();
}
2017-02-02 16:28:38 -08:00
if ( null !== $this -> logger && $event -> isPropagationStopped ()) {
$this -> logger -> debug ( sprintf ( 'The "%s" event is already stopped. No listeners have been called.' , $eventName ));
}
2015-08-17 17:00:26 -07:00
$this -> preProcess ( $eventName );
2019-01-24 08:00:03 +00:00
try {
$this -> preDispatch ( $eventName , $event );
try {
$e = $this -> stopwatch -> start ( $eventName , 'section' );
try {
$this -> dispatcher -> dispatch ( $eventName , $event );
} finally {
if ( $e -> isStarted ()) {
$e -> stop ();
}
}
} finally {
$this -> postDispatch ( $eventName , $event );
}
} finally {
$this -> postProcess ( $eventName );
2015-08-17 17:00:26 -07:00
}
return $event ;
}
/**
* { @ inheritdoc }
*/
public function getCalledListeners ()
{
2019-01-24 08:00:03 +00:00
if ( null === $this -> callStack ) {
return array ();
}
2015-08-17 17:00:26 -07:00
$called = array ();
2019-01-24 08:00:03 +00:00
foreach ( $this -> callStack as $listener ) {
list ( $eventName ) = $this -> callStack -> getInfo ();
$called [] = $listener -> getInfo ( $eventName );
2015-08-17 17:00:26 -07:00
}
return $called ;
}
/**
* { @ inheritdoc }
*/
public function getNotCalledListeners ()
{
try {
$allListeners = $this -> getListeners ();
} catch ( \Exception $e ) {
if ( null !== $this -> logger ) {
$this -> logger -> info ( 'An exception was thrown while getting the uncalled listeners.' , array ( 'exception' => $e ));
}
// unable to retrieve the uncalled listeners
return array ();
}
$notCalled = array ();
foreach ( $allListeners as $eventName => $listeners ) {
foreach ( $listeners as $listener ) {
$called = false ;
2019-01-24 08:00:03 +00:00
if ( null !== $this -> callStack ) {
foreach ( $this -> callStack as $calledListener ) {
if ( $calledListener -> getWrappedListener () === $listener ) {
2015-08-17 17:00:26 -07:00
$called = true ;
break ;
}
}
}
if ( ! $called ) {
2018-11-23 12:29:20 +00:00
if ( ! $listener instanceof WrappedListener ) {
$listener = new WrappedListener ( $listener , null , $this -> stopwatch , $this );
}
2019-01-24 08:00:03 +00:00
$notCalled [] = $listener -> getInfo ( $eventName );
2015-08-17 17:00:26 -07:00
}
}
}
2019-01-24 08:00:03 +00:00
uasort ( $notCalled , array ( $this , 'sortNotCalledListeners' ));
2016-04-20 09:56:34 -07:00
2015-08-17 17:00:26 -07:00
return $notCalled ;
}
2018-11-23 12:29:20 +00:00
public function reset ()
{
2019-01-24 08:00:03 +00:00
$this -> callStack = null ;
2018-11-23 12:29:20 +00:00
}
2015-08-17 17:00:26 -07:00
/**
* Proxies all method calls to the original event dispatcher .
*
* @ param string $method The method name
* @ param array $arguments The method arguments
*
* @ return mixed
*/
public function __call ( $method , $arguments )
{
2018-11-23 12:29:20 +00:00
return \call_user_func_array ( array ( $this -> dispatcher , $method ), $arguments );
2015-08-17 17:00:26 -07:00
}
/**
* Called before dispatching the event .
*
* @ param string $eventName The event name
* @ param Event $event The event
*/
protected function preDispatch ( $eventName , Event $event )
{
}
/**
* Called after dispatching the event .
*
* @ param string $eventName The event name
* @ param Event $event The event
*/
protected function postDispatch ( $eventName , Event $event )
{
}
private function preProcess ( $eventName )
{
foreach ( $this -> dispatcher -> getListeners ( $eventName ) as $listener ) {
2018-11-23 12:29:20 +00:00
$priority = $this -> getListenerPriority ( $eventName , $listener );
2019-01-24 08:00:03 +00:00
$wrappedListener = new WrappedListener ( $listener instanceof WrappedListener ? $listener -> getWrappedListener () : $listener , null , $this -> stopwatch , $this );
2015-08-17 17:00:26 -07:00
$this -> wrappedListeners [ $eventName ][] = $wrappedListener ;
2016-04-20 09:56:34 -07:00
$this -> dispatcher -> removeListener ( $eventName , $listener );
2018-11-23 12:29:20 +00:00
$this -> dispatcher -> addListener ( $eventName , $wrappedListener , $priority );
2019-01-24 08:00:03 +00:00
$this -> callStack -> attach ( $wrappedListener , array ( $eventName ));
2015-08-17 17:00:26 -07:00
}
}
private function postProcess ( $eventName )
{
unset ( $this -> wrappedListeners [ $eventName ]);
$skipped = false ;
foreach ( $this -> dispatcher -> getListeners ( $eventName ) as $listener ) {
if ( ! $listener instanceof WrappedListener ) { // #12845: a new listener was added during dispatch.
continue ;
}
// Unwrap listener
2016-04-20 09:56:34 -07:00
$priority = $this -> getListenerPriority ( $eventName , $listener );
2015-08-17 17:00:26 -07:00
$this -> dispatcher -> removeListener ( $eventName , $listener );
2016-04-20 09:56:34 -07:00
$this -> dispatcher -> addListener ( $eventName , $listener -> getWrappedListener (), $priority );
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
if ( null !== $this -> logger ) {
$context = array ( 'event' => $eventName , 'listener' => $listener -> getPretty ());
}
2015-08-17 17:00:26 -07:00
if ( $listener -> wasCalled ()) {
if ( null !== $this -> logger ) {
2018-11-23 12:29:20 +00:00
$this -> logger -> debug ( 'Notified event "{event}" to listener "{listener}".' , $context );
2015-08-17 17:00:26 -07:00
}
if ( ! isset ( $this -> called [ $eventName ])) {
$this -> called [ $eventName ] = new \SplObjectStorage ();
}
2019-01-24 08:00:03 +00:00
} else {
$this -> callStack -> detach ( $listener );
2015-08-17 17:00:26 -07:00
}
if ( null !== $this -> logger && $skipped ) {
2018-11-23 12:29:20 +00:00
$this -> logger -> debug ( 'Listener "{listener}" was not called for event "{event}".' , $context );
2015-08-17 17:00:26 -07:00
}
if ( $listener -> stoppedPropagation ()) {
if ( null !== $this -> logger ) {
2018-11-23 12:29:20 +00:00
$this -> logger -> debug ( 'Listener "{listener}" stopped propagation of the event "{event}".' , $context );
2015-08-17 17:00:26 -07:00
}
$skipped = true ;
}
}
}
2019-01-24 08:00:03 +00:00
private function sortNotCalledListeners ( array $a , array $b )
2016-04-20 09:56:34 -07:00
{
2019-01-24 08:00:03 +00:00
if ( 0 !== $cmp = strcmp ( $a [ 'event' ], $b [ 'event' ])) {
return $cmp ;
}
2018-11-23 12:29:20 +00:00
if ( \is_int ( $a [ 'priority' ]) && ! \is_int ( $b [ 'priority' ])) {
2016-04-20 09:56:34 -07:00
return 1 ;
}
2018-11-23 12:29:20 +00:00
if ( ! \is_int ( $a [ 'priority' ]) && \is_int ( $b [ 'priority' ])) {
2016-04-20 09:56:34 -07:00
return - 1 ;
}
if ( $a [ 'priority' ] === $b [ 'priority' ]) {
return 0 ;
}
if ( $a [ 'priority' ] > $b [ 'priority' ]) {
return - 1 ;
}
return 1 ;
}
2015-08-17 17:00:26 -07:00
}