Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1479a26

Browse files
committed
feature #28920 [EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - PR green and ready. From UPGRADE files: EventDispatcher --------------- * The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated HttpKernel ---------- * Renamed `FilterControllerArgumentsEvent` to `ControllerArgumentsEvent` * Renamed `FilterControllerEvent` to `ControllerEvent` * Renamed `FilterResponseEvent` to `ResponseEvent` * Renamed `GetResponseEvent` to `RequestEvent` * Renamed `GetResponseForControllerResultEvent` to `ViewEvent` * Renamed `GetResponseForExceptionEvent` to `ExceptionEvent` * Renamed `PostResponseEvent` to `TerminateEvent` Security --------- * The `ListenerInterface` is deprecated, turn your listeners into callables instead. * The `Firewall::handleRequest()` method is deprecated, use `Firewall::callListeners()` instead. Commits ------- 75369da [EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN
2 parents 81bf2ab + 75369da commit 1479a26

File tree

163 files changed

+1649
-658
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+1649
-658
lines changed

UPGRADE-4.3.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ Config
2121

2222
* Deprecated using environment variables with `cannotBeEmpty()` if the value is validated with `validate()`
2323

24+
EventDispatcher
25+
---------------
26+
27+
* The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated
28+
2429
Form
2530
----
2631

@@ -55,7 +60,14 @@ HttpFoundation
5560
HttpKernel
5661
----------
5762

58-
* renamed `Client` to `HttpKernelBrowser`
63+
* Renamed `Client` to `HttpKernelBrowser`
64+
* Renamed `FilterControllerArgumentsEvent` to `ControllerArgumentsEvent`
65+
* Renamed `FilterControllerEvent` to `ControllerEvent`
66+
* Renamed `FilterResponseEvent` to `ResponseEvent`
67+
* Renamed `GetResponseEvent` to `RequestEvent`
68+
* Renamed `GetResponseForControllerResultEvent` to `ViewEvent`
69+
* Renamed `GetResponseForExceptionEvent` to `ExceptionEvent`
70+
* Renamed `PostResponseEvent` to `TerminateEvent`
5971

6072
Messenger
6173
---------
@@ -81,6 +93,8 @@ Security
8193
Use the `getReachableRoleNames()` method instead.
8294
* The `getRoles()` method of the `TokenInterface` is deprecated. Tokens must implement the `getRoleNames()`
8395
method instead and return roles as strings.
96+
* The `ListenerInterface` is deprecated, turn your listeners into callables instead.
97+
* The `Firewall::handleRequest()` method is deprecated, use `Firewall::callListeners()` instead.
8498
* The `AbstractToken::serialize()`, `AbstractToken::unserialize()`,
8599
`AuthenticationException::serialize()` and `AuthenticationException::unserialize()`
86100
methods are now final, use `getState()` and `setState()` instead.

UPGRADE-5.0.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ EventDispatcher
7070
---------------
7171

7272
* The `TraceableEventDispatcherInterface` has been removed.
73+
* The signature of the `EventDispatcherInterface::dispatch()` method has been updated to `dispatch($event, string $eventName = null)`
7374

7475
Filesystem
7576
----------
@@ -206,6 +207,13 @@ HttpKernel
206207
* Removed the first and second constructor argument of `ConfigDataCollector`
207208
* Removed `ConfigDataCollector::getApplicationName()`
208209
* Removed `ConfigDataCollector::getApplicationVersion()`
210+
* Removed `FilterControllerArgumentsEvent`, use `ControllerArgumentsEvent` instead
211+
* Removed `FilterControllerEvent`, use `ControllerEvent` instead
212+
* Removed `FilterResponseEvent`, use `ResponseEvent` instead
213+
* Removed `GetResponseEvent`, use `RequestEvent` instead
214+
* Removed `GetResponseForControllerResultEvent`, use `ViewEvent` instead
215+
* Removed `GetResponseForExceptionEvent`, use `ExceptionEvent` instead
216+
* Removed `PostResponseEvent`, use `TerminateEvent` instead
209217

210218
Messenger
211219
---------
@@ -262,6 +270,8 @@ Security
262270
* `SimpleAuthenticatorInterface`, `SimpleFormAuthenticatorInterface`, `SimplePreAuthenticatorInterface`,
263271
`SimpleAuthenticationProvider`, `SimpleAuthenticationHandler`, `SimpleFormAuthenticationListener` and
264272
`SimplePreAuthenticationListener` have been removed. Use Guard instead.
273+
* The `ListenerInterface` has been removed, turn your listeners into callables instead.
274+
* The `Firewall::handleRequest()` method has been removed, use `Firewall::callListeners()` instead.
265275
* `\Serializable` interface has been removed from `AbstractToken` and `AuthenticationException`,
266276
thus `serialize()` and `unserialize()` aren't available.
267277
Use `getState()` and `setState()` instead.

src/Symfony/Bridge/Doctrine/Tests/Form/EventListener/MergeDoctrineCollectionListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testOnSubmitDoNothing()
6363
$submittedData = ['test'];
6464
$event = new FormEvent($this->getForm(), $submittedData);
6565

66-
$this->dispatcher->dispatch(FormEvents::SUBMIT, $event);
66+
$this->dispatcher->dispatch($event, FormEvents::SUBMIT);
6767

6868
$this->assertTrue($this->collection->contains('test'));
6969
$this->assertSame(1, $this->collection->count());
@@ -74,7 +74,7 @@ public function testOnSubmitNullClearCollection()
7474
$submittedData = [];
7575
$event = new FormEvent($this->getForm(), $submittedData);
7676

77-
$this->dispatcher->dispatch(FormEvents::SUBMIT, $event);
77+
$this->dispatcher->dispatch($event, FormEvents::SUBMIT);
7878

7979
$this->assertTrue($this->collection->isEmpty());
8080
}

src/Symfony/Bridge/Monolog/Handler/ChromePhpHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* ChromePhpHandler.
2020
*
2121
* @author Christophe Coevoet <[email protected]>
22+
*
23+
* @final since Symfony 4.3
2224
*/
2325
class ChromePhpHandler extends BaseChromePhpHandler
2426
{

src/Symfony/Bridge/Monolog/Handler/FirePHPHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* FirePHPHandler.
2020
*
2121
* @author Jordi Boggiano <[email protected]>
22+
*
23+
* @final since Symfony 4.3
2224
*/
2325
class FirePHPHandler extends BaseFirePHPHandler
2426
{

src/Symfony/Bridge/Monolog/Handler/SwiftMailerHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Extended SwiftMailerHandler that flushes mail queue if necessary.
2020
*
2121
* @author Philipp Kräutli <[email protected]>
22+
*
23+
* @final since Symfony 4.3
2224
*/
2325
class SwiftMailerHandler extends BaseSwiftMailerHandler
2426
{

src/Symfony/Bridge/Monolog/Processor/WebProcessor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* WebProcessor override to read from the HttpFoundation's Request.
2121
*
2222
* @author Jordi Boggiano <[email protected]>
23+
*
24+
* @final since Symfony 4.3
2325
*/
2426
class WebProcessor extends BaseWebProcessor implements EventSubscriberInterface
2527
{

src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ public function testLogsFromListeners()
196196
});
197197

198198
$event = new ConsoleCommandEvent(new Command('foo'), $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(), $output);
199-
$dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
199+
$dispatcher->dispatch($event, ConsoleEvents::COMMAND);
200200
$this->assertContains('Before command message.', $out = $output->fetch());
201201
$this->assertContains('After command message.', $out);
202202

203203
$event = new ConsoleTerminateEvent(new Command('foo'), $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(), $output, 0);
204-
$dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
204+
$dispatcher->dispatch($event, ConsoleEvents::TERMINATE);
205205
$this->assertContains('Before terminate message.', $out = $output->fetch());
206206
$this->assertContains('After terminate message.', $out);
207207
}

src/Symfony/Bridge/Monolog/Tests/Processor/WebProcessorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Bridge\Monolog\Processor\WebProcessor;
1717
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpKernel\Event\RequestEvent;
1819

1920
class WebProcessorTest extends TestCase
2021
{
@@ -87,7 +88,7 @@ private function createRequestEvent($additionalServerParameters = []): array
8788
$request->server->replace($server);
8889
$request->headers->replace($server);
8990

90-
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
91+
$event = $this->getMockBuilder(RequestEvent::class)
9192
->disableOriginalConstructor()
9293
->getMock();
9394
$event->expects($this->any())

src/Symfony/Bridge/Monolog/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
"php": "^7.1.3",
2020
"monolog/monolog": "~1.19",
2121
"symfony/contracts": "^1.0",
22-
"symfony/http-kernel": "~3.4|~4.0"
22+
"symfony/http-kernel": "^4.3"
2323
},
2424
"require-dev": {
2525
"symfony/console": "~3.4|~4.0",
26-
"symfony/event-dispatcher": "~3.4|~4.0",
2726
"symfony/security-core": "~3.4|~4.0",
2827
"symfony/var-dumper": "~3.4|~4.0"
2928
},
@@ -34,7 +33,6 @@
3433
"suggest": {
3534
"symfony/http-kernel": "For using the debugging handlers together with the response life cycle of the HTTP kernel.",
3635
"symfony/console": "For the possibility to show log messages in console commands depending on verbosity settings.",
37-
"symfony/event-dispatcher": "Needed when using log messages in console commands.",
3836
"symfony/var-dumper": "For using the debugging handlers like the console handler or the log server handler."
3937
},
4038
"autoload": {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
66

7+
<parameters>
8+
<!-- this parameter is used at compile time in RegisterListenersPass -->
9+
<parameter key="event_dispatcher.event_aliases" type="collection">
10+
<parameter key="Symfony\Component\Console\Event\ConsoleCommandEvent">console.command</parameter>
11+
<parameter key="Symfony\Component\Console\Event\ConsoleErrorEvent">console.error</parameter>
12+
<parameter key="Symfony\Component\Console\Event\ConsoleTerminateEvent">console.terminate</parameter>
13+
<parameter key="Symfony\Component\Form\Event\PreSubmitEvent">form.pre_submit</parameter>
14+
<parameter key="Symfony\Component\Form\Event\SubmitEvent">form.submit</parameter>
15+
<parameter key="Symfony\Component\Form\Event\PostSubmitEvent">form.post_submit</parameter>
16+
<parameter key="Symfony\Component\Form\Event\PreSetDataEvent">form.pre_set_data</parameter>
17+
<parameter key="Symfony\Component\Form\Event\PostSetDataEvent">form.post_set_data</parameter>
18+
<parameter key="Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent">kernel.controller_arguments</parameter>
19+
<parameter key="Symfony\Component\HttpKernel\Event\ControllerEvent">kernel.controller</parameter>
20+
<parameter key="Symfony\Component\HttpKernel\Event\ResponseEvent">kernel.response</parameter>
21+
<parameter key="Symfony\Component\HttpKernel\Event\FinishRequestEvent">kernel.finish_request</parameter>
22+
<parameter key="Symfony\Component\HttpKernel\Event\RequestEvent">kernel.request</parameter>
23+
<parameter key="Symfony\Component\HttpKernel\Event\ViewEvent">kernel.view</parameter>
24+
<parameter key="Symfony\Component\HttpKernel\Event\ExceptionEvent">kernel.exception</parameter>
25+
<parameter key="Symfony\Component\HttpKernel\Event\TerminateEvent">kernel.terminate</parameter>
26+
<parameter key="Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent">security.authentication.success</parameter>
27+
<parameter key="Symfony\Component\Security\Core\Event\AuthenticationFailureEvent">security.authentication.failure</parameter>
28+
<parameter key="Symfony\Component\Security\Http\Event\InteractiveLoginEvent">security.interactive_login</parameter>
29+
<parameter key="Symfony\Component\Security\Http\Event\SwitchUserEvent">security.switch_user</parameter>
30+
<parameter key="Symfony\Component\Workflow\Event\GuardEvent">workflow.guard</parameter>
31+
<parameter key="Symfony\Component\Workflow\Event\LeaveEvent">workflow.leave</parameter>
32+
<parameter key="Symfony\Component\Workflow\Event\TransitionEvent">workflow.transition</parameter>
33+
<parameter key="Symfony\Component\Workflow\Event\EnterEvent">workflow.enter</parameter>
34+
<parameter key="Symfony\Component\Workflow\Event\EnteredEvent">workflow.entered</parameter>
35+
<parameter key="Symfony\Component\Workflow\Event\CompletedEvent">workflow.completed</parameter>
36+
<parameter key="Symfony\Component\Workflow\Event\AnnounceEvent">workflow.announce</parameter>
37+
</parameter>
38+
</parameters>
739
<services>
840
<defaults public="false" />
941

src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/ResolveControllerNameSubscriberTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber;
1616
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1717
use Symfony\Component\HttpFoundation\Request;
18-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18+
use Symfony\Component\HttpKernel\Event\RequestEvent;
1919
use Symfony\Component\HttpKernel\HttpKernelInterface;
2020

2121
class ResolveControllerNameSubscriberTest extends TestCase
@@ -33,7 +33,7 @@ public function testReplacesControllerAttribute()
3333
$request->attributes->set('_controller', 'AppBundle:Starting:format');
3434

3535
$subscriber = new ResolveControllerNameSubscriber($parser);
36-
$subscriber->onKernelRequest(new GetResponseEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
36+
$subscriber->onKernelRequest(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
3737
$this->assertEquals('App\\Final\\Format::methodName', $request->attributes->get('_controller'));
3838
}
3939

@@ -51,7 +51,7 @@ public function testSkipsOtherControllerFormats($controller)
5151
$request->attributes->set('_controller', $controller);
5252

5353
$subscriber = new ResolveControllerNameSubscriber($parser);
54-
$subscriber->onKernelRequest(new GetResponseEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
54+
$subscriber->onKernelRequest(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
5555
$this->assertEquals($controller, $request->attributes->get('_controller'));
5656
}
5757

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"symfony/config": "~4.2",
2323
"symfony/contracts": "^1.0.2",
2424
"symfony/dependency-injection": "^4.3",
25-
"symfony/event-dispatcher": "^4.1",
2625
"symfony/http-foundation": "^4.3",
2726
"symfony/http-kernel": "^4.3",
2827
"symfony/polyfill-mbstring": "~1.0",

src/Symfony/Bundle/SecurityBundle/Debug/TraceableFirewallListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Debug;
1313

1414
use Symfony\Bundle\SecurityBundle\EventListener\FirewallListener;
15-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15+
use Symfony\Component\HttpKernel\Event\RequestEvent;
1616

1717
/**
1818
* Firewall collecting called listeners.
@@ -28,11 +28,11 @@ public function getWrappedListeners()
2828
return $this->wrappedListeners;
2929
}
3030

31-
protected function handleRequest(GetResponseEvent $event, $listeners)
31+
protected function callListeners(RequestEvent $event, iterable $listeners)
3232
{
3333
foreach ($listeners as $listener) {
3434
$wrappedListener = new WrappedListener($listener);
35-
$wrappedListener->handle($event);
35+
$wrappedListener($event);
3636
$this->wrappedListeners[] = $wrappedListener->getInfo();
3737

3838
if ($event->hasResponse()) {

src/Symfony/Bundle/SecurityBundle/Debug/WrappedListener.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,32 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\Debug;
1313

14-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14+
use Symfony\Component\HttpKernel\Event\RequestEvent;
15+
use Symfony\Component\Security\Http\Firewall\LegacyListenerTrait;
1516
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
1617
use Symfony\Component\VarDumper\Caster\ClassStub;
1718

1819
/**
1920
* Wraps a security listener for calls record.
2021
*
2122
* @author Robin Chalas <[email protected]>
23+
*
24+
* @internal since Symfony 4.3
2225
*/
2326
final class WrappedListener implements ListenerInterface
2427
{
28+
use LegacyListenerTrait;
29+
2530
private $response;
2631
private $listener;
2732
private $time;
2833
private $stub;
2934
private static $hasVarDumper;
3035

31-
public function __construct(ListenerInterface $listener)
36+
/**
37+
* @param callable $listener
38+
*/
39+
public function __construct($listener)
3240
{
3341
$this->listener = $listener;
3442

@@ -40,10 +48,15 @@ public function __construct(ListenerInterface $listener)
4048
/**
4149
* {@inheritdoc}
4250
*/
43-
public function handle(GetResponseEvent $event)
51+
public function __invoke(RequestEvent $event)
4452
{
4553
$startTime = microtime(true);
46-
$this->listener->handle($event);
54+
if (\is_callable($this->listener)) {
55+
($this->listener)($event);
56+
} else {
57+
@trigger_error(sprintf('Calling the "%s::handle()" method from the firewall is deprecated since Symfony 4.3, implement "__invoke()" instead.', \get_class($this)), E_USER_DEPRECATED);
58+
$this->listener->handle($event);
59+
}
4760
$this->time = microtime(true) - $startTime;
4861
$this->response = $event->getResponse();
4962
}
@@ -56,7 +69,7 @@ public function __call($method, $arguments)
5669
return $this->listener->{$method}(...$arguments);
5770
}
5871

59-
public function getWrappedListener(): ListenerInterface
72+
public function getWrappedListener()
6073
{
6174
return $this->listener;
6275
}

src/Symfony/Bundle/SecurityBundle/EventListener/FirewallListener.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1616
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1717
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18+
use Symfony\Component\HttpKernel\KernelEvents;
1819
use Symfony\Component\Security\Http\Firewall;
1920
use Symfony\Component\Security\Http\FirewallMapInterface;
2021
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
@@ -35,7 +36,10 @@ public function __construct(FirewallMapInterface $map, EventDispatcherInterface
3536
parent::__construct($map, $dispatcher);
3637
}
3738

38-
public function onKernelRequest(GetResponseEvent $event)
39+
/**
40+
* @internal
41+
*/
42+
public function configureLogoutUrlGenerator(GetResponseEvent $event)
3943
{
4044
if (!$event->isMasterRequest()) {
4145
return;
@@ -44,10 +48,11 @@ public function onKernelRequest(GetResponseEvent $event)
4448
if ($this->map instanceof FirewallMap && $config = $this->map->getFirewallConfig($event->getRequest())) {
4549
$this->logoutUrlGenerator->setCurrentFirewall($config->getName(), $config->getContext());
4650
}
47-
48-
parent::onKernelRequest($event);
4951
}
5052

53+
/**
54+
* @internal since Symfony 4.3
55+
*/
5156
public function onKernelFinishRequest(FinishRequestEvent $event)
5257
{
5358
if ($event->isMasterRequest()) {
@@ -56,4 +61,18 @@ public function onKernelFinishRequest(FinishRequestEvent $event)
5661

5762
parent::onKernelFinishRequest($event);
5863
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public static function getSubscribedEvents()
69+
{
70+
return [
71+
KernelEvents::REQUEST => [
72+
['configureLogoutUrlGenerator', 8],
73+
['onKernelRequest', 8],
74+
],
75+
KernelEvents::FINISH_REQUEST => 'onKernelFinishRequest',
76+
];
77+
}
5978
}

0 commit comments

Comments
 (0)