4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher\Debug;
14 use Symfony\Component\Stopwatch\Stopwatch;
15 use Symfony\Component\EventDispatcher\Event;
16 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17 use Symfony\Component\VarDumper\Caster\ClassStub;
20 * @author Fabien Potencier <fabien@symfony.com>
27 private $stoppedPropagation;
32 private static $hasClassStub;
34 public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
36 $this->listener = $listener;
38 $this->stopwatch = $stopwatch;
39 $this->dispatcher = $dispatcher;
40 $this->called = false;
41 $this->stoppedPropagation = false;
43 if (is_array($listener)) {
44 $this->name = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
45 $this->pretty = $this->name.'::'.$listener[1];
46 } elseif ($listener instanceof \Closure) {
47 $this->pretty = $this->name = 'closure';
48 } elseif (is_string($listener)) {
49 $this->pretty = $this->name = $listener;
51 $this->name = get_class($listener);
52 $this->pretty = $this->name.'::__invoke';
59 if (null === self::$hasClassStub) {
60 self::$hasClassStub = class_exists(ClassStub::class);
64 public function getWrappedListener()
66 return $this->listener;
69 public function wasCalled()
74 public function stoppedPropagation()
76 return $this->stoppedPropagation;
79 public function getPretty()
84 public function getInfo($eventName)
86 if (null === $this->stub) {
87 $this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()';
91 'event' => $eventName,
92 'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
93 'pretty' => $this->pretty,
94 'stub' => $this->stub,
98 public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
100 $this->called = true;
102 $e = $this->stopwatch->start($this->name, 'event_listener');
104 call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
106 if ($e->isStarted()) {
110 if ($event->isPropagationStopped()) {
111 $this->stoppedPropagation = true;