Security update for Core, with self-updated composer
[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 use Symfony\Component\VarDumper\Cloner\VarCloner;
19
20 /**
21  * @author Fabien Potencier <fabien@symfony.com>
22  */
23 class WrappedListener
24 {
25     private $listener;
26     private $name;
27     private $called;
28     private $stoppedPropagation;
29     private $stopwatch;
30     private $dispatcher;
31     private $pretty;
32     private $data;
33
34     private static $cloner;
35
36     public function __construct($listener, $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
37     {
38         $this->listener = $listener;
39         $this->name = $name;
40         $this->stopwatch = $stopwatch;
41         $this->dispatcher = $dispatcher;
42         $this->called = false;
43         $this->stoppedPropagation = false;
44
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;
52         } else {
53             $this->name = get_class($listener);
54             $this->pretty = $this->name.'::__invoke';
55         }
56
57         if (null !== $name) {
58             $this->name = $name;
59         }
60
61         if (null === self::$cloner) {
62             self::$cloner = class_exists(ClassStub::class) ? new VarCloner() : false;
63         }
64     }
65
66     public function getWrappedListener()
67     {
68         return $this->listener;
69     }
70
71     public function wasCalled()
72     {
73         return $this->called;
74     }
75
76     public function stoppedPropagation()
77     {
78         return $this->stoppedPropagation;
79     }
80
81     public function getPretty()
82     {
83         return $this->pretty;
84     }
85
86     public function getInfo($eventName)
87     {
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;
90         }
91
92         return array(
93             'event' => $eventName,
94             'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
95             'pretty' => $this->pretty,
96             'data' => $this->data,
97         );
98     }
99
100     public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
101     {
102         $this->called = true;
103
104         $e = $this->stopwatch->start($this->name, 'event_listener');
105
106         call_user_func($this->listener, $event, $eventName, $this->dispatcher ?: $dispatcher);
107
108         if ($e->isStarted()) {
109             $e->stop();
110         }
111
112         if ($event->isPropagationStopped()) {
113             $this->stoppedPropagation = true;
114         }
115     }
116 }