Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / event-dispatcher / Debug / TraceableEventDispatcher.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\EventDispatcher\EventDispatcherInterface;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16 use Symfony\Component\EventDispatcher\Event;
17 use Symfony\Component\Stopwatch\Stopwatch;
18 use Psr\Log\LoggerInterface;
19
20 /**
21  * Collects some data about event listeners.
22  *
23  * This event dispatcher delegates the dispatching to another one.
24  *
25  * @author Fabien Potencier <fabien@symfony.com>
26  */
27 class TraceableEventDispatcher implements TraceableEventDispatcherInterface
28 {
29     protected $logger;
30     protected $stopwatch;
31
32     private $called;
33     private $dispatcher;
34     private $wrappedListeners;
35
36     /**
37      * Constructor.
38      *
39      * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
40      * @param Stopwatch                $stopwatch  A Stopwatch instance
41      * @param LoggerInterface          $logger     A LoggerInterface instance
42      */
43     public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
44     {
45         $this->dispatcher = $dispatcher;
46         $this->stopwatch = $stopwatch;
47         $this->logger = $logger;
48         $this->called = array();
49         $this->wrappedListeners = array();
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function addListener($eventName, $listener, $priority = 0)
56     {
57         $this->dispatcher->addListener($eventName, $listener, $priority);
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     public function addSubscriber(EventSubscriberInterface $subscriber)
64     {
65         $this->dispatcher->addSubscriber($subscriber);
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function removeListener($eventName, $listener)
72     {
73         if (isset($this->wrappedListeners[$eventName])) {
74             foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
75                 if ($wrappedListener->getWrappedListener() === $listener) {
76                     $listener = $wrappedListener;
77                     unset($this->wrappedListeners[$eventName][$index]);
78                     break;
79                 }
80             }
81         }
82
83         return $this->dispatcher->removeListener($eventName, $listener);
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function removeSubscriber(EventSubscriberInterface $subscriber)
90     {
91         return $this->dispatcher->removeSubscriber($subscriber);
92     }
93
94     /**
95      * {@inheritdoc}
96      */
97     public function getListeners($eventName = null)
98     {
99         return $this->dispatcher->getListeners($eventName);
100     }
101
102     /**
103      * {@inheritdoc}
104      */
105     public function getListenerPriority($eventName, $listener)
106     {
107         // we might have wrapped listeners for the event (if called while dispatching)
108         // in that case get the priority by wrapper
109         if (isset($this->wrappedListeners[$eventName])) {
110             foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
111                 if ($wrappedListener->getWrappedListener() === $listener) {
112                     return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
113                 }
114             }
115         }
116
117         return $this->dispatcher->getListenerPriority($eventName, $listener);
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     public function hasListeners($eventName = null)
124     {
125         return $this->dispatcher->hasListeners($eventName);
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     public function dispatch($eventName, Event $event = null)
132     {
133         if (null === $event) {
134             $event = new Event();
135         }
136
137         if (null !== $this->logger && $event->isPropagationStopped()) {
138             $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
139         }
140
141         $this->preProcess($eventName);
142         $this->preDispatch($eventName, $event);
143
144         $e = $this->stopwatch->start($eventName, 'section');
145
146         $this->dispatcher->dispatch($eventName, $event);
147
148         if ($e->isStarted()) {
149             $e->stop();
150         }
151
152         $this->postDispatch($eventName, $event);
153         $this->postProcess($eventName);
154
155         return $event;
156     }
157
158     /**
159      * {@inheritdoc}
160      */
161     public function getCalledListeners()
162     {
163         $called = array();
164         foreach ($this->called as $eventName => $listeners) {
165             foreach ($listeners as $listener) {
166                 $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
167             }
168         }
169
170         return $called;
171     }
172
173     /**
174      * {@inheritdoc}
175      */
176     public function getNotCalledListeners()
177     {
178         try {
179             $allListeners = $this->getListeners();
180         } catch (\Exception $e) {
181             if (null !== $this->logger) {
182                 $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
183             }
184
185             // unable to retrieve the uncalled listeners
186             return array();
187         }
188
189         $notCalled = array();
190         foreach ($allListeners as $eventName => $listeners) {
191             foreach ($listeners as $listener) {
192                 $called = false;
193                 if (isset($this->called[$eventName])) {
194                     foreach ($this->called[$eventName] as $l) {
195                         if ($l->getWrappedListener() === $listener) {
196                             $called = true;
197
198                             break;
199                         }
200                     }
201                 }
202
203                 if (!$called) {
204                     if (!$listener instanceof WrappedListener) {
205                         $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
206                     }
207                     $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
208                 }
209             }
210         }
211
212         uasort($notCalled, array($this, 'sortListenersByPriority'));
213
214         return $notCalled;
215     }
216
217     /**
218      * Proxies all method calls to the original event dispatcher.
219      *
220      * @param string $method    The method name
221      * @param array  $arguments The method arguments
222      *
223      * @return mixed
224      */
225     public function __call($method, $arguments)
226     {
227         return call_user_func_array(array($this->dispatcher, $method), $arguments);
228     }
229
230     /**
231      * Called before dispatching the event.
232      *
233      * @param string $eventName The event name
234      * @param Event  $event     The event
235      */
236     protected function preDispatch($eventName, Event $event)
237     {
238     }
239
240     /**
241      * Called after dispatching the event.
242      *
243      * @param string $eventName The event name
244      * @param Event  $event     The event
245      */
246     protected function postDispatch($eventName, Event $event)
247     {
248     }
249
250     private function preProcess($eventName)
251     {
252         foreach ($this->dispatcher->getListeners($eventName) as $listener) {
253             $priority = $this->getListenerPriority($eventName, $listener);
254             $wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
255             $this->wrappedListeners[$eventName][] = $wrappedListener;
256             $this->dispatcher->removeListener($eventName, $listener);
257             $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
258         }
259     }
260
261     private function postProcess($eventName)
262     {
263         unset($this->wrappedListeners[$eventName]);
264         $skipped = false;
265         foreach ($this->dispatcher->getListeners($eventName) as $listener) {
266             if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
267                 continue;
268             }
269             // Unwrap listener
270             $priority = $this->getListenerPriority($eventName, $listener);
271             $this->dispatcher->removeListener($eventName, $listener);
272             $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
273
274             if (null !== $this->logger) {
275                 $context = array('event' => $eventName, 'listener' => $listener->getPretty());
276             }
277
278             if ($listener->wasCalled()) {
279                 if (null !== $this->logger) {
280                     $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
281                 }
282
283                 if (!isset($this->called[$eventName])) {
284                     $this->called[$eventName] = new \SplObjectStorage();
285                 }
286
287                 $this->called[$eventName]->attach($listener);
288             }
289
290             if (null !== $this->logger && $skipped) {
291                 $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
292             }
293
294             if ($listener->stoppedPropagation()) {
295                 if (null !== $this->logger) {
296                     $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
297                 }
298
299                 $skipped = true;
300             }
301         }
302     }
303
304     private function sortListenersByPriority($a, $b)
305     {
306         if (is_int($a['priority']) && !is_int($b['priority'])) {
307             return 1;
308         }
309
310         if (!is_int($a['priority']) && is_int($b['priority'])) {
311             return -1;
312         }
313
314         if ($a['priority'] === $b['priority']) {
315             return 0;
316         }
317
318         if ($a['priority'] > $b['priority']) {
319             return -1;
320         }
321
322         return 1;
323     }
324 }