Newer
Older
Klaranouba7
a validé
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Contracts\EventDispatcher\Event;
/**
* @internal
*/
class EventRegistry
{
// list of *known* events to always include (if they exist)
private static $newEventsMap = [
'kernel.exception' => ExceptionEvent::class,
'kernel.request' => RequestEvent::class,
'kernel.response' => ResponseEvent::class,
'kernel.view' => ViewEvent::class,
'kernel.controller_arguments' => ControllerArgumentsEvent::class,
'kernel.controller' => ControllerEvent::class,
'kernel.terminate' => TerminateEvent::class,
];
private static $eventsMap = [
'console.command' => ConsoleCommandEvent::class,
'console.terminate' => ConsoleTerminateEvent::class,
'console.error' => ConsoleErrorEvent::class,
'kernel.request' => GetResponseEvent::class,
'kernel.exception' => GetResponseForExceptionEvent::class,
'kernel.view' => GetResponseForControllerResultEvent::class,
'kernel.controller' => FilterControllerEvent::class,
'kernel.controller_arguments' => FilterControllerArgumentsEvent::class,
'kernel.response' => FilterResponseEvent::class,
'kernel.terminate' => PostResponseEvent::class,
'kernel.finish_request' => FinishRequestEvent::class,
'security.authentication.success' => AuthenticationSuccessEvent::class,
'security.authentication.failure' => AuthenticationFailureEvent::class,
'security.interactive_login' => InteractiveLoginEvent::class,
'security.switch_user' => SwitchUserEvent::class,
];
private $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
// Loop through the new event classes
foreach (self::$newEventsMap as $eventName => $newEventClass) {
//Check if the new event classes exist, if so replace the old one with the new.
if (isset(self::$eventsMap[$eventName]) && class_exists($newEventClass)) {
self::$eventsMap[$eventName] = $newEventClass;
}
}
}
/**
* Returns all known event names in the system.
*/
public function getAllActiveEvents(): array
{
$activeEvents = [];
foreach (self::$eventsMap as $eventName => $eventClass) {
if (!class_exists($eventClass)) {
continue;
}
$activeEvents[] = $eventName;
}
$listeners = $this->eventDispatcher->getListeners();
// Check if these listeners are part of the new events.
foreach (array_keys($listeners) as $listenerKey) {
if (isset(self::$newEventsMap[$listenerKey])) {
unset($listeners[$listenerKey]);
}
if (!isset(self::$eventsMap[$listenerKey])) {
self::$eventsMap[$listenerKey] = $this->getEventClassName($listenerKey);
}
}
$activeEvents = array_unique(array_merge($activeEvents, array_keys($listeners)));
asort($activeEvents);
return $activeEvents;
}
/**
* Attempts to get the event class for a given event.
*/
public function getEventClassName(string $event): ?string
{
// if the event is already a class name, use it
if (class_exists($event)) {
return $event;
}
if (isset(self::$eventsMap[$event])) {
return self::$eventsMap[$event];
}
$listeners = $this->eventDispatcher->getListeners($event);
if (empty($listeners)) {
return null;
}
foreach ($listeners as $listener) {
if (!\is_array($listener) || 2 !== \count($listener)) {
continue;
}
$reflectionMethod = new \ReflectionMethod($listener[0], $listener[1]);
$args = $reflectionMethod->getParameters();
if (!$args) {
continue;
}
if (null !== $type = $args[0]->getType()) {
$type = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
if (LegacyEvent::class === $type && class_exists(Event::class)) {
return Event::class;
}
// ignore an "object" type-hint
if ('object' === $type) {
continue;
}
return $type;
}
}
return null;
}
public function listActiveEvents(array $events): array
{
foreach ($events as $key => $event) {
$events[$key] = sprintf('%s (<fg=yellow>%s</>)', $event, self::$eventsMap[$event]);
}
return $events;
}
}