Skip to content

Commit 394a970

Browse files
committed
feature #1418 Determine events map dynamically (HypeMC)
This PR was merged into the 1.x-dev branch. Discussion ---------- Determine events map dynamically The current events map is missing some events, such as the `command.signal` event. It also references some legacy event classes, like the `GetResponseEvent` class, which are no longer necessary since the minimum required version does not support these classes anymore. This PR modifies the `EventRegistry` so that the events map is determined dynamically. The added benefit of this approach is that only the events that are actually available are listed. Commits ------- dc1ffae Determine events map dynamically
2 parents 3f0d95a + dc1ffae commit 394a970

File tree

1 file changed

+15
-62
lines changed

1 file changed

+15
-62
lines changed

src/EventRegistry.php

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,32 @@
1111

1212
namespace Symfony\Bundle\MakerBundle;
1313

14-
use Symfony\Component\Console\Event\ConsoleCommandEvent;
15-
use Symfony\Component\Console\Event\ConsoleErrorEvent;
16-
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
14+
use Symfony\Component\Console\ConsoleEvents;
1715
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18-
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
19-
use Symfony\Component\HttpKernel\Event\ControllerEvent;
20-
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
21-
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
22-
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
23-
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
24-
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
25-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
26-
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
27-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
28-
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
29-
use Symfony\Component\HttpKernel\Event\RequestEvent;
30-
use Symfony\Component\HttpKernel\Event\ResponseEvent;
31-
use Symfony\Component\HttpKernel\Event\TerminateEvent;
32-
use Symfony\Component\HttpKernel\Event\ViewEvent;
33-
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
34-
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
35-
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
36-
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
16+
use Symfony\Component\Form\FormEvents;
17+
use Symfony\Component\HttpKernel\KernelEvents;
18+
use Symfony\Component\Security\Core\AuthenticationEvents;
19+
use Symfony\Component\Security\Http\SecurityEvents;
20+
use Symfony\Component\Workflow\WorkflowEvents;
3721

3822
/**
3923
* @internal
4024
*/
4125
class EventRegistry
4226
{
43-
// list of *known* events to always include (if they exist)
44-
private static array $newEventsMap = [
45-
'kernel.exception' => ExceptionEvent::class,
46-
'kernel.request' => RequestEvent::class,
47-
'kernel.response' => ResponseEvent::class,
48-
'kernel.view' => ViewEvent::class,
49-
'kernel.controller_arguments' => ControllerArgumentsEvent::class,
50-
'kernel.controller' => ControllerEvent::class,
51-
'kernel.terminate' => TerminateEvent::class,
52-
];
53-
54-
private static array $eventsMap = [
55-
'console.command' => ConsoleCommandEvent::class,
56-
'console.terminate' => ConsoleTerminateEvent::class,
57-
'console.error' => ConsoleErrorEvent::class,
58-
'kernel.request' => GetResponseEvent::class,
59-
'kernel.exception' => GetResponseForExceptionEvent::class,
60-
'kernel.view' => GetResponseForControllerResultEvent::class,
61-
'kernel.controller' => FilterControllerEvent::class,
62-
'kernel.controller_arguments' => FilterControllerArgumentsEvent::class,
63-
'kernel.response' => FilterResponseEvent::class,
64-
'kernel.terminate' => PostResponseEvent::class,
65-
'kernel.finish_request' => FinishRequestEvent::class,
66-
'security.authentication.success' => AuthenticationSuccessEvent::class,
67-
'security.authentication.failure' => AuthenticationFailureEvent::class,
68-
'security.interactive_login' => InteractiveLoginEvent::class,
69-
'security.switch_user' => SwitchUserEvent::class,
70-
];
27+
private static array $eventsMap = [];
7128

7229
public function __construct(
7330
private EventDispatcherInterface $eventDispatcher,
7431
) {
75-
// Loop through the new event classes
76-
foreach (self::$newEventsMap as $eventName => $newEventClass) {
77-
// Check if the new event classes exist, if so replace the old one with the new.
78-
if (isset(self::$eventsMap[$eventName]) && class_exists($newEventClass)) {
79-
self::$eventsMap[$eventName] = $newEventClass;
80-
}
81-
}
32+
self::$eventsMap = array_flip([
33+
...ConsoleEvents::ALIASES,
34+
...KernelEvents::ALIASES,
35+
...(class_exists(AuthenticationEvents::class) ? AuthenticationEvents::ALIASES : []),
36+
...(class_exists(SecurityEvents::class) ? SecurityEvents::ALIASES : []),
37+
...(class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []),
38+
...(class_exists(FormEvents::class) ? FormEvents::ALIASES : []),
39+
]);
8240
}
8341

8442
/**
@@ -97,12 +55,7 @@ public function getAllActiveEvents(): array
9755

9856
$listeners = $this->eventDispatcher->getListeners();
9957

100-
// Check if these listeners are part of the new events.
10158
foreach (array_keys($listeners) as $listenerKey) {
102-
if (isset(self::$newEventsMap[$listenerKey])) {
103-
unset($listeners[$listenerKey]);
104-
}
105-
10659
if (!isset(self::$eventsMap[$listenerKey])) {
10760
self::$eventsMap[$listenerKey] = $this->getEventClassName($listenerKey);
10861
}

0 commit comments

Comments
 (0)