f7b0273e15aaa88e685d31bb76c340de15eba246
[yaffs-website] / vendor / symfony / event-dispatcher / Debug / WrappedListener.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\EventDispatcher\Debug;
13
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
19 /**
20  * @author Fabien Potencier <fabien@symfony.com>
21  */
22 class WrappedListener
23 {
24     private $listener;
25     private $name;
26     private $called;
27     private $stoppedPropagation;
28     private $stopwatch;
29     private $dispatcher;
30     private $pretty;
31     private $stub;
32     private static $hasClassStub;
33
34     public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
35     {
36         $this->listener = $listener;
37         $this->name = $name;
38         $this->stopwatch = $stopwatch;
39         $this->dispatcher = $dispatcher;
40         $this->called = false;
41         $this->stoppedPropagation = false;
42
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;
50         } else {
51             $this->name = get_class($listener);
52             $this->pretty = $this->name.'::__invoke';
53         }
54
55         if (null !== $name) {
56             $this->name = $name;
57         }
58
59         if (null === self::$hasClassStub) {
60             self::$hasClassStub = class_exists(ClassStub::class);
61         }
62     }
63
64     public function getWrappedListener()
65     {
66         return $this->listener;
67     }
68
69     public function wasCalled()
70     {
71         return $this->called;
72     }
73
74     public function stoppedPropagation()
75     {
76         return $this->stoppedPropagation;
77     }
78
79     public function getPretty()
80     {
81         return $this->pretty;
82     }
83
84     public function getInfo($eventName)
85     {
86         if (null === $this->stub) {
87             $this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()';
88         }
89
90         return array(
91             'event' => $eventName,
92             'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
93             'pretty' => $this->pretty,
94             'stub' => $this->stub,
95         );
96     }
97
98     public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
99     {
100         $this->called = true;
101
102         $e = $this->stopwatch->start($this->name, 'event_listener');
103
104         call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
105
106         if ($e->isStarted()) {
107             $e->stop();
108         }
109
110         if ($event->isPropagationStopped()) {
111             $this->stoppedPropagation = true;
112         }
113     }
114 }