vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php line 115

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\EventDispatcher\Debug;
  11. use Psr\EventDispatcher\StoppableEventInterface;
  12. use Symfony\Component\EventDispatcher\Event;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\LegacyEventProxy;
  15. use Symfony\Component\Stopwatch\Stopwatch;
  16. use Symfony\Component\VarDumper\Caster\ClassStub;
  17. use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
  18. /**
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  *
  21.  * @final since Symfony 4.3: the "Event" type-hint on __invoke() will be replaced by "object" in 5.0
  22.  */
  23. class WrappedListener
  24. {
  25.     private $listener;
  26.     private $optimizedListener;
  27.     private $name;
  28.     private $called;
  29.     private $stoppedPropagation;
  30.     private $stopwatch;
  31.     private $dispatcher;
  32.     private $pretty;
  33.     private $stub;
  34.     private $priority;
  35.     private static $hasClassStub;
  36.     public function __construct($listener, ?string $nameStopwatch $stopwatchEventDispatcherInterface $dispatcher null)
  37.     {
  38.         $this->listener $listener;
  39.         $this->optimizedListener $listener instanceof \Closure $listener : (\is_callable($listener) ? \Closure::fromCallable($listener) : null);
  40.         $this->stopwatch $stopwatch;
  41.         $this->dispatcher $dispatcher;
  42.         $this->called false;
  43.         $this->stoppedPropagation false;
  44.         if (\is_array($listener)) {
  45.             $this->name \is_object($listener[0]) ? \get_class($listener[0]) : $listener[0];
  46.             $this->pretty $this->name.'::'.$listener[1];
  47.         } elseif ($listener instanceof \Closure) {
  48.             $r = new \ReflectionFunction($listener);
  49.             if (str_contains($r->name'{closure}')) {
  50.                 $this->pretty $this->name 'closure';
  51.             } elseif ($class $r->getClosureScopeClass()) {
  52.                 $this->name $class->name;
  53.                 $this->pretty $this->name.'::'.$r->name;
  54.             } else {
  55.                 $this->pretty $this->name $r->name;
  56.             }
  57.         } elseif (\is_string($listener)) {
  58.             $this->pretty $this->name $listener;
  59.         } else {
  60.             $this->name \get_class($listener);
  61.             $this->pretty $this->name.'::__invoke';
  62.         }
  63.         if (null !== $name) {
  64.             $this->name $name;
  65.         }
  66.         if (null === self::$hasClassStub) {
  67.             self::$hasClassStub class_exists(ClassStub::class);
  68.         }
  69.     }
  70.     public function getWrappedListener()
  71.     {
  72.         return $this->listener;
  73.     }
  74.     public function wasCalled()
  75.     {
  76.         return $this->called;
  77.     }
  78.     public function stoppedPropagation()
  79.     {
  80.         return $this->stoppedPropagation;
  81.     }
  82.     public function getPretty()
  83.     {
  84.         return $this->pretty;
  85.     }
  86.     public function getInfo($eventName)
  87.     {
  88.         if (null === $this->stub) {
  89.             $this->stub self::$hasClassStub ? new ClassStub($this->pretty.'()'$this->listener) : $this->pretty.'()';
  90.         }
  91.         return [
  92.             'event' => $eventName,
  93.             'priority' => null !== $this->priority $this->priority : (null !== $this->dispatcher $this->dispatcher->getListenerPriority($eventName$this->listener) : null),
  94.             'pretty' => $this->pretty,
  95.             'stub' => $this->stub,
  96.         ];
  97.     }
  98.     public function __invoke(Event $event$eventNameEventDispatcherInterface $dispatcher)
  99.     {
  100.         if ($event instanceof LegacyEventProxy) {
  101.             $event $event->getEvent();
  102.         }
  103.         $dispatcher $this->dispatcher ?: $dispatcher;
  104.         $this->called true;
  105.         $this->priority $dispatcher->getListenerPriority($eventName$this->listener);
  106.         $e $this->stopwatch->start($this->name'event_listener');
  107.         ($this->optimizedListener ?? $this->listener)($event$eventName$dispatcher);
  108.         if ($e->isStarted()) {
  109.             $e->stop();
  110.         }
  111.         if (($event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
  112.             $this->stoppedPropagation true;
  113.         }
  114.     }
  115. }