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;
18 use Symfony\Component\VarDumper\Cloner\VarCloner;
21 * @author Fabien Potencier <fabien@symfony.com>
28 private $stoppedPropagation;
34 private static $cloner;
36 public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
38 $this->listener = $listener;
40 $this->stopwatch = $stopwatch;
41 $this->dispatcher = $dispatcher;
42 $this->called = false;
43 $this->stoppedPropagation = false;
45 if (is_array($listener)) {
46 $this->name = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
47 $this->pretty = $this->name.'::'.$listener[1];
48 } elseif ($listener instanceof \Closure) {
49 $this->pretty = $this->name = 'closure';
50 } elseif (is_string($listener)) {
51 $this->pretty = $this->name = $listener;
53 $this->name = get_class($listener);
54 $this->pretty = $this->name.'::__invoke';
61 if (null === self::$cloner) {
62 self::$cloner = class_exists(ClassStub::class) ? new VarCloner() : false;
66 public function getWrappedListener()
68 return $this->listener;
71 public function wasCalled()
76 public function stoppedPropagation()
78 return $this->stoppedPropagation;
81 public function getPretty()
86 public function getInfo($eventName)
88 if (null === $this->data) {
89 $this->data = false !== self::$cloner ? self::$cloner->cloneVar(array(new ClassStub($this->pretty.'()', $this->listener)))->seek(0) : $this->pretty;
93 'event' => $eventName,
94 'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
95 'pretty' => $this->pretty,
96 'data' => $this->data,
100 public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
102 $this->called = true;
104 $e = $this->stopwatch->start($this->name, 'event_listener');
106 call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
108 if ($e->isStarted()) {
112 if ($event->isPropagationStopped()) {
113 $this->stoppedPropagation = true;