Skip to content

Commit f9df6e2

Browse files
committed
Move event alias mappings to their components.
1 parent 6aecad7 commit f9df6e2

File tree

11 files changed

+215
-65
lines changed

11 files changed

+215
-65
lines changed

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

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
1616
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
1717
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
18-
use Symfony\Component\Console\Event\ConsoleCommandEvent;
19-
use Symfony\Component\Console\Event\ConsoleErrorEvent;
20-
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
18+
use Symfony\Component\Console\ConsoleEvents;
2119
use Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker;
2220
use Symfony\Component\DependencyInjection\EnvVarProcessor;
2321
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
@@ -27,70 +25,34 @@
2725
use Symfony\Component\EventDispatcher\EventDispatcher;
2826
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcherInterfaceComponentAlias;
2927
use Symfony\Component\Filesystem\Filesystem;
30-
use Symfony\Component\Form\Event\PostSetDataEvent;
31-
use Symfony\Component\Form\Event\PostSubmitEvent;
32-
use Symfony\Component\Form\Event\PreSetDataEvent;
33-
use Symfony\Component\Form\Event\PreSubmitEvent;
34-
use Symfony\Component\Form\Event\SubmitEvent;
28+
use Symfony\Component\Form\FormEvents;
3529
use Symfony\Component\HttpFoundation\RequestStack;
3630
use Symfony\Component\HttpFoundation\UrlHelper;
3731
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
3832
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
3933
use Symfony\Component\HttpKernel\Config\FileLocator;
4034
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
41-
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
42-
use Symfony\Component\HttpKernel\Event\ControllerEvent;
43-
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
44-
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
45-
use Symfony\Component\HttpKernel\Event\RequestEvent;
46-
use Symfony\Component\HttpKernel\Event\ResponseEvent;
47-
use Symfony\Component\HttpKernel\Event\TerminateEvent;
48-
use Symfony\Component\HttpKernel\Event\ViewEvent;
4935
use Symfony\Component\HttpKernel\EventListener\LocaleAwareListener;
5036
use Symfony\Component\HttpKernel\HttpCache\Store;
5137
use Symfony\Component\HttpKernel\HttpKernel;
5238
use Symfony\Component\HttpKernel\HttpKernelInterface;
39+
use Symfony\Component\HttpKernel\KernelEvents;
5340
use Symfony\Component\HttpKernel\KernelInterface;
5441
use Symfony\Component\HttpKernel\UriSigner;
5542
use Symfony\Component\String\LazyString;
5643
use Symfony\Component\String\Slugger\AsciiSlugger;
5744
use Symfony\Component\String\Slugger\SluggerInterface;
58-
use Symfony\Component\Workflow\Event\AnnounceEvent;
59-
use Symfony\Component\Workflow\Event\CompletedEvent;
60-
use Symfony\Component\Workflow\Event\EnteredEvent;
61-
use Symfony\Component\Workflow\Event\EnterEvent;
62-
use Symfony\Component\Workflow\Event\GuardEvent;
63-
use Symfony\Component\Workflow\Event\LeaveEvent;
64-
use Symfony\Component\Workflow\Event\TransitionEvent;
45+
use Symfony\Component\Workflow\WorkflowEvents;
6546
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
6647

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

9557
$container->services()
9658

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 +73,7 @@
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",
@@ -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: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
3838
use Symfony\Component\HttpKernel\Bundle\Bundle;
3939
use Symfony\Component\Security\Core\AuthenticationEvents;
40-
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
41-
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
42-
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
43-
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
4440
use Symfony\Component\Security\Http\SecurityEvents;
4541

4642
/**
@@ -79,11 +75,9 @@ public function build(ContainerBuilder $container)
7975
// must be registered after RegisterListenersPass (in the FrameworkBundle)
8076
$container->addCompilerPass(new RegisterGlobalSecurityEventListenersPass(), PassConfig::TYPE_BEFORE_REMOVING, -200);
8177

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

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)