Skip to content

Commit 0c93d78

Browse files
committed
Move event alias mappings to their components.
1 parent 9a6e727 commit 0c93d78

File tree

11 files changed

+214
-67
lines changed

11 files changed

+214
-67
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use Closure;
1515
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
1616
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
17-
use Symfony\Component\Console\Event\ConsoleCommandEvent;
18-
use Symfony\Component\Console\Event\ConsoleErrorEvent;
19-
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
17+
use Symfony\Component\Console\ConsoleEvents;
2018
use Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker;
2119
use Symfony\Component\DependencyInjection\EnvVarProcessor;
2220
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
@@ -26,69 +24,33 @@
2624
use Symfony\Component\EventDispatcher\EventDispatcher;
2725
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcherInterfaceComponentAlias;
2826
use Symfony\Component\Filesystem\Filesystem;
29-
use Symfony\Component\Form\Event\PostSetDataEvent;
30-
use Symfony\Component\Form\Event\PostSubmitEvent;
31-
use Symfony\Component\Form\Event\PreSetDataEvent;
32-
use Symfony\Component\Form\Event\PreSubmitEvent;
33-
use Symfony\Component\Form\Event\SubmitEvent;
27+
use Symfony\Component\Form\FormEvents;
3428
use Symfony\Component\HttpFoundation\RequestStack;
3529
use Symfony\Component\HttpFoundation\UrlHelper;
3630
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
3731
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
3832
use Symfony\Component\HttpKernel\Config\FileLocator;
3933
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
40-
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
41-
use Symfony\Component\HttpKernel\Event\ControllerEvent;
42-
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
43-
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
44-
use Symfony\Component\HttpKernel\Event\RequestEvent;
45-
use Symfony\Component\HttpKernel\Event\ResponseEvent;
46-
use Symfony\Component\HttpKernel\Event\TerminateEvent;
47-
use Symfony\Component\HttpKernel\Event\ViewEvent;
4834
use Symfony\Component\HttpKernel\EventListener\LocaleAwareListener;
4935
use Symfony\Component\HttpKernel\HttpKernel;
5036
use Symfony\Component\HttpKernel\HttpKernelInterface;
37+
use Symfony\Component\HttpKernel\KernelEvents;
5138
use Symfony\Component\HttpKernel\KernelInterface;
5239
use Symfony\Component\HttpKernel\UriSigner;
5340
use Symfony\Component\String\LazyString;
5441
use Symfony\Component\String\Slugger\AsciiSlugger;
5542
use Symfony\Component\String\Slugger\SluggerInterface;
56-
use Symfony\Component\Workflow\Event\AnnounceEvent;
57-
use Symfony\Component\Workflow\Event\CompletedEvent;
58-
use Symfony\Component\Workflow\Event\EnteredEvent;
59-
use Symfony\Component\Workflow\Event\EnterEvent;
60-
use Symfony\Component\Workflow\Event\GuardEvent;
61-
use Symfony\Component\Workflow\Event\LeaveEvent;
62-
use Symfony\Component\Workflow\Event\TransitionEvent;
43+
use Symfony\Component\Workflow\WorkflowEvents;
6344
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
6445

6546
return static function (ContainerConfigurator $container) {
6647
// this parameter is used at compile time in RegisterListenersPass
67-
$container->parameters()->set('event_dispatcher.event_aliases', [
68-
ConsoleCommandEvent::class => 'console.command',
69-
ConsoleErrorEvent::class => 'console.error',
70-
ConsoleTerminateEvent::class => 'console.terminate',
71-
PreSubmitEvent::class => 'form.pre_submit',
72-
SubmitEvent::class => 'form.submit',
73-
PostSubmitEvent::class => 'form.post_submit',
74-
PreSetDataEvent::class => 'form.pre_set_data',
75-
PostSetDataEvent::class => 'form.post_set_data',
76-
ControllerArgumentsEvent::class => 'kernel.controller_arguments',
77-
ControllerEvent::class => 'kernel.controller',
78-
ResponseEvent::class => 'kernel.response',
79-
FinishRequestEvent::class => 'kernel.finish_request',
80-
RequestEvent::class => 'kernel.request',
81-
ViewEvent::class => 'kernel.view',
82-
ExceptionEvent::class => 'kernel.exception',
83-
TerminateEvent::class => 'kernel.terminate',
84-
GuardEvent::class => 'workflow.guard',
85-
LeaveEvent::class => 'workflow.leave',
86-
TransitionEvent::class => 'workflow.transition',
87-
EnterEvent::class => 'workflow.enter',
88-
EnteredEvent::class => 'workflow.entered',
89-
CompletedEvent::class => 'workflow.completed',
90-
AnnounceEvent::class => 'workflow.announce',
91-
]);
48+
$container->parameters()->set('event_dispatcher.event_aliases', array_merge(
49+
ConsoleEvents::ALIASES,
50+
FormEvents::ALIASES,
51+
KernelEvents::ALIASES,
52+
WorkflowEvents::ALIASES
53+
));
9254

9355
$container->services()
9456

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"symfony/event-dispatcher": "^5.1",
2525
"symfony/error-handler": "^4.4.1|^5.0.1",
2626
"symfony/http-foundation": "^4.4|^5.0",
27-
"symfony/http-kernel": "^5.0",
27+
"symfony/http-kernel": "^5.2",
2828
"symfony/polyfill-mbstring": "~1.0",
2929
"symfony/polyfill-php80": "^1.15",
3030
"symfony/filesystem": "^4.4|^5.0",
@@ -36,12 +36,12 @@
3636
"doctrine/cache": "~1.0",
3737
"symfony/asset": "^5.1",
3838
"symfony/browser-kit": "^4.4|^5.0",
39-
"symfony/console": "^4.4|^5.0",
39+
"symfony/console": "^5.2",
4040
"symfony/css-selector": "^4.4|^5.0",
4141
"symfony/dom-crawler": "^4.4|^5.0",
4242
"symfony/dotenv": "^5.1",
4343
"symfony/polyfill-intl-icu": "~1.0",
44-
"symfony/form": "^4.4|^5.0",
44+
"symfony/form": "^5.2",
4545
"symfony/expression-language": "^4.4|^5.0",
4646
"symfony/http-client": "^4.4|^5.0",
4747
"symfony/lock": "^4.4|^5.0",
@@ -58,7 +58,7 @@
5858
"symfony/translation": "^5.0",
5959
"symfony/twig-bundle": "^4.4|^5.0",
6060
"symfony/validator": "^4.4|^5.0",
61-
"symfony/workflow": "^4.4|^5.0",
61+
"symfony/workflow": "^5.2",
6262
"symfony/yaml": "^4.4|^5.0",
6363
"symfony/property-info": "^4.4|^5.0",
6464
"symfony/web-link": "^4.4|^5.0",
@@ -73,11 +73,11 @@
7373
"phpunit/phpunit": "<5.4.3",
7474
"symfony/asset": "<5.1",
7575
"symfony/browser-kit": "<4.4",
76-
"symfony/console": "<4.4",
76+
"symfony/console": "<5.2",
7777
"symfony/dotenv": "<5.1",
7878
"symfony/dom-crawler": "<4.4",
7979
"symfony/http-client": "<4.4",
80-
"symfony/form": "<4.4",
80+
"symfony/form": "<5.2",
8181
"symfony/lock": "<4.4",
8282
"symfony/mailer": "<5.2",
8383
"symfony/messenger": "<4.4",
@@ -90,7 +90,7 @@
9090
"symfony/twig-bundle": "<4.4",
9191
"symfony/validator": "<4.4",
9292
"symfony/web-profiler-bundle": "<4.4",
93-
"symfony/workflow": "<4.4"
93+
"symfony/workflow": "<5.2"
9494
},
9595
"suggest": {
9696
"ext-apcu": "For best performance of the system caches",

src/Symfony/Bundle/SecurityBundle/SecurityBundle.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
3737
use Symfony\Component\HttpKernel\Bundle\Bundle;
3838
use Symfony\Component\Security\Core\AuthenticationEvents;
39-
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
40-
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
41-
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
42-
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
4339
use Symfony\Component\Security\Http\SecurityEvents;
4440

4541
/**
@@ -76,11 +72,6 @@ public function build(ContainerBuilder $container)
7672
$container->addCompilerPass(new RegisterTokenUsageTrackingPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 200);
7773
$container->addCompilerPass(new RegisterLdapLocatorPass());
7874

79-
$container->addCompilerPass(new AddEventAliasesPass([
80-
AuthenticationSuccessEvent::class => AuthenticationEvents::AUTHENTICATION_SUCCESS,
81-
AuthenticationFailureEvent::class => AuthenticationEvents::AUTHENTICATION_FAILURE,
82-
InteractiveLoginEvent::class => SecurityEvents::INTERACTIVE_LOGIN,
83-
SwitchUserEvent::class => SecurityEvents::SWITCH_USER,
84-
]));
75+
$container->addCompilerPass(new AddEventAliasesPass(array_merge(AuthenticationEvents::ALIASES, SecurityEvents::ALIASES)));
8576
}
8677
}

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
"symfony/event-dispatcher": "^5.1",
2424
"symfony/http-kernel": "^5.0",
2525
"symfony/polyfill-php80": "^1.15",
26-
"symfony/security-core": "^5.1",
26+
"symfony/security-core": "^5.2",
2727
"symfony/security-csrf": "^4.4|^5.0",
2828
"symfony/security-guard": "^5.1",
29-
"symfony/security-http": "^5.1,>=5.1.2"
29+
"symfony/security-http": "^5.2"
3030
},
3131
"require-dev": {
3232
"doctrine/doctrine-bundle": "^2.0",

src/Symfony/Component/Console/ConsoleEvents.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Component\Console;
1313

14+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
15+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
16+
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
17+
1418
/**
1519
* Contains all events dispatched by an Application.
1620
*
@@ -44,4 +48,15 @@ final class ConsoleEvents
4448
* @Event("Symfony\Component\Console\Event\ConsoleErrorEvent")
4549
*/
4650
const ERROR = 'console.error';
51+
52+
/**
53+
* Event aliases.
54+
*
55+
* These aliases can be consumed by RegisterListenersPass.
56+
*/
57+
const ALIASES = [
58+
ConsoleCommandEvent::class => self::COMMAND,
59+
ConsoleErrorEvent::class => self::ERROR,
60+
ConsoleTerminateEvent::class => self::TERMINATE,
61+
];
4762
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\Console\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\ConsoleEvents;
18+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
19+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
20+
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Output\OutputInterface;
23+
use Symfony\Component\Console\Tester\ApplicationTester;
24+
use Symfony\Component\DependencyInjection\ContainerBuilder;
25+
use Symfony\Component\DependencyInjection\Reference;
26+
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
27+
use Symfony\Component\EventDispatcher\EventDispatcher;
28+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
29+
30+
class ConsoleEventsTest extends TestCase
31+
{
32+
public function testEventAliases(): void
33+
{
34+
$container = new ContainerBuilder();
35+
$container->setParameter('event_dispatcher.event_aliases', ConsoleEvents::ALIASES);
36+
$container->addCompilerPass(new RegisterListenersPass());
37+
38+
$container->register('event_dispatcher', EventDispatcher::class);
39+
$container->register('tracer', EventTraceSubscriber::class)
40+
->setPublic(true)
41+
->addTag('kernel.event_subscriber');
42+
$container->register('failing_command', FailingCommand::class);
43+
$container->register('application', Application::class)
44+
->setPublic(true)
45+
->addMethodCall('setAutoExit', [false])
46+
->addMethodCall('setDispatcher', [new Reference('event_dispatcher')])
47+
->addMethodCall('add', [new Reference('failing_command')])
48+
;
49+
50+
$container->compile();
51+
52+
$tester = new ApplicationTester($container->get('application'));
53+
$tester->run(['fail']);
54+
55+
$this->assertSame([ConsoleCommandEvent::class, ConsoleErrorEvent::class, ConsoleTerminateEvent::class], $container->get('tracer')->observedEvents);
56+
}
57+
}
58+
59+
class EventTraceSubscriber implements EventSubscriberInterface
60+
{
61+
public $observedEvents = [];
62+
63+
public static function getSubscribedEvents(): array
64+
{
65+
return [
66+
ConsoleCommandEvent::class => 'observe',
67+
ConsoleErrorEvent::class => 'observe',
68+
ConsoleTerminateEvent::class => 'observe',
69+
];
70+
}
71+
72+
public function observe(object $event): void
73+
{
74+
$this->observedEvents[] = get_debug_type($event);
75+
}
76+
}
77+
78+
class FailingCommand extends Command
79+
{
80+
protected static $defaultName = 'fail';
81+
82+
protected function execute(InputInterface $input, OutputInterface $output): int
83+
{
84+
throw new \RuntimeException('I failed. Sorry.');
85+
}
86+
}

src/Symfony/Component/Form/FormEvents.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
namespace Symfony\Component\Form;
1313

14+
use Symfony\Component\Form\Event\PostSetDataEvent;
15+
use Symfony\Component\Form\Event\PostSubmitEvent;
16+
use Symfony\Component\Form\Event\PreSetDataEvent;
17+
use Symfony\Component\Form\Event\PreSubmitEvent;
18+
use Symfony\Component\Form\Event\SubmitEvent;
19+
1420
/**
1521
* To learn more about how form events work check the documentation
1622
* entry at {@link https://symfony.com/doc/any/components/form/form_events.html}.
@@ -88,6 +94,19 @@ final class FormEvents
8894
*/
8995
const POST_SET_DATA = 'form.post_set_data';
9096

97+
/**
98+
* Event aliases.
99+
*
100+
* These aliases can be consumed by RegisterListenersPass.
101+
*/
102+
const ALIASES = [
103+
PreSubmitEvent::class => self::PRE_SUBMIT,
104+
SubmitEvent::class => self::SUBMIT,
105+
PostSubmitEvent::class => self::POST_SUBMIT,
106+
PreSetDataEvent::class => self::PRE_SET_DATA,
107+
PostSetDataEvent::class => self::POST_SET_DATA,
108+
];
109+
91110
private function __construct()
92111
{
93112
}

src/Symfony/Component/HttpKernel/KernelEvents.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111

1212
namespace Symfony\Component\HttpKernel;
1313

14+
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
15+
use Symfony\Component\HttpKernel\Event\ControllerEvent;
16+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
17+
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
18+
use Symfony\Component\HttpKernel\Event\RequestEvent;
19+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
20+
use Symfony\Component\HttpKernel\Event\TerminateEvent;
21+
use Symfony\Component\HttpKernel\Event\ViewEvent;
22+
1423
/**
1524
* Contains all events thrown in the HttpKernel component.
1625
*
@@ -100,4 +109,20 @@ final class KernelEvents
100109
* @Event("Symfony\Component\HttpKernel\Event\TerminateEvent")
101110
*/
102111
const TERMINATE = 'kernel.terminate';
112+
113+
/**
114+
* Event aliases.
115+
*
116+
* These aliases can be consumed by RegisterListenersPass.
117+
*/
118+
const ALIASES = [
119+
ControllerArgumentsEvent::class => self::CONTROLLER_ARGUMENTS,
120+
ControllerEvent::class => self::CONTROLLER,
121+
ResponseEvent::class => self::RESPONSE,
122+
FinishRequestEvent::class => self::FINISH_REQUEST,
123+
RequestEvent::class => self::REQUEST,
124+
ViewEvent::class => self::VIEW,
125+
ExceptionEvent::class => self::EXCEPTION,
126+
TerminateEvent::class => self::TERMINATE,
127+
];
103128
}

src/Symfony/Component/Security/Core/AuthenticationEvents.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Security\Core;
1313

14+
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
15+
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
16+
1417
final class AuthenticationEvents
1518
{
1619
/**
@@ -28,4 +31,14 @@ final class AuthenticationEvents
2831
* @Event("Symfony\Component\Security\Core\Event\AuthenticationFailureEvent")
2932
*/
3033
const AUTHENTICATION_FAILURE = 'security.authentication.failure';
34+
35+
/**
36+
* Event aliases.
37+
*
38+
* These aliases can be consumed by RegisterListenersPass.
39+
*/
40+
const ALIASES = [
41+
AuthenticationSuccessEvent::class => self::AUTHENTICATION_SUCCESS,
42+
AuthenticationFailureEvent::class => self::AUTHENTICATION_FAILURE,
43+
];
3144
}

0 commit comments

Comments
 (0)