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

Skip to content

Commit 001dba6

Browse files
[EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN
1 parent bcff647 commit 001dba6

File tree

117 files changed

+1068
-304
lines changed

Some content is hidden

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

117 files changed

+1068
-304
lines changed

UPGRADE-4.3.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPGRADE FROM 4.2 to 4.3
2+
=======================
3+
4+
EventDispatcher
5+
---------------
6+
7+
* The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ EventDispatcher
6363
---------------
6464

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

6768
Finder
6869
------

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 = array('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 = array();
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/Bundle/FrameworkBundle/Resources/config/services.xml

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

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* @author Maxime Steinhausser <[email protected]>
24+
*
25+
* @final since Symfony 4.3
2426
*/
2527
class FirewallListener extends Firewall
2628
{

src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* This means that the WDT is never included in sub-requests or ESI requests.
3131
*
3232
* @author Fabien Potencier <[email protected]>
33+
*
34+
* @final since Symfony 4.3
3335
*/
3436
class WebDebugToolbarListener implements EventSubscriberInterface
3537
{

src/Symfony/Component/Console/Application.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use Symfony\Component\Debug\ErrorHandler;
4545
use Symfony\Component\Debug\Exception\FatalThrowableError;
4646
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
47+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
4748

4849
/**
4950
* An Application is the container for a collection of commands.
@@ -92,7 +93,7 @@ public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN
9293

9394
public function setDispatcher(EventDispatcherInterface $dispatcher)
9495
{
95-
$this->dispatcher = $dispatcher;
96+
$this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher);
9697
}
9798

9899
public function setCommandLoader(CommandLoaderInterface $commandLoader)
@@ -228,7 +229,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
228229
if (!($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) || 1 !== \count($alternatives = $e->getAlternatives()) || !$input->isInteractive()) {
229230
if (null !== $this->dispatcher) {
230231
$event = new ConsoleErrorEvent($input, $output, $e);
231-
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);
232+
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
232233

233234
if (0 === $event->getExitCode()) {
234235
return 0;
@@ -247,7 +248,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
247248
if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) {
248249
if (null !== $this->dispatcher) {
249250
$event = new ConsoleErrorEvent($input, $output, $e);
250-
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);
251+
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
251252

252253
return $event->getExitCode();
253254
}
@@ -906,7 +907,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
906907
$e = null;
907908

908909
try {
909-
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
910+
$this->dispatcher->dispatch($event, ConsoleEvents::COMMAND);
910911

911912
if ($event->commandShouldRun()) {
912913
$exitCode = $command->run($input, $output);
@@ -915,7 +916,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
915916
}
916917
} catch (\Throwable $e) {
917918
$event = new ConsoleErrorEvent($input, $output, $e, $command);
918-
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);
919+
$this->dispatcher->dispatch($event, ConsoleEvents::ERROR);
919920
$e = $event->getError();
920921

921922
if (0 === $exitCode = $event->getExitCode()) {
@@ -924,7 +925,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
924925
}
925926

926927
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
927-
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
928+
$this->dispatcher->dispatch($event, ConsoleEvents::TERMINATE);
928929

929930
if (null !== $e) {
930931
throw $e;

src/Symfony/Component/Console/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require-dev": {
2424
"symfony/config": "~3.4|~4.0",
25-
"symfony/event-dispatcher": "~3.4|~4.0",
25+
"symfony/event-dispatcher": "~4.2",
2626
"symfony/dependency-injection": "~3.4|~4.0",
2727
"symfony/lock": "~3.4|~4.0",
2828
"symfony/process": "~3.4|~4.0",
@@ -36,6 +36,7 @@
3636
},
3737
"conflict": {
3838
"symfony/dependency-injection": "<3.4",
39+
"symfony/event-dispatcher": "<4.2",
3940
"symfony/process": "<3.3"
4041
},
4142
"autoload": {

src/Symfony/Component/EventDispatcher/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated
8+
49
4.1.0
510
-----
611

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\Event;
1616
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1717
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1819
use Symfony\Component\Stopwatch\Stopwatch;
1920

2021
/**
@@ -36,7 +37,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
3637

3738
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
3839
{
39-
$this->dispatcher = $dispatcher;
40+
$this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher);
4041
$this->stopwatch = $stopwatch;
4142
$this->logger = $logger;
4243
$this->called = array();
@@ -122,11 +123,24 @@ public function hasListeners($eventName = null)
122123

123124
/**
124125
* {@inheritdoc}
126+
*
127+
* @param string|null $eventName
125128
*/
126-
public function dispatch($eventName, Event $event = null)
129+
public function dispatch($event/*, string $eventName = null*/)
127130
{
128-
if (null === $event) {
129-
$event = new Event();
131+
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
132+
133+
if ($event instanceof Event) {
134+
$eventName = $eventName ?? \get_class($event);
135+
} else {
136+
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
137+
$swap = $event;
138+
$event = $eventName ?? new Event();
139+
$eventName = $swap;
140+
141+
if (!$event instanceof Event) {
142+
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
143+
}
130144
}
131145

132146
if (null !== $this->logger && $event->isPropagationStopped()) {
@@ -138,7 +152,7 @@ public function dispatch($eventName, Event $event = null)
138152

139153
$e = $this->stopwatch->start($eventName, 'section');
140154

141-
$this->dispatcher->dispatch($eventName, $event);
155+
$this->dispatcher->dispatch($event, $eventName);
142156

143157
if ($e->isStarted()) {
144158
$e->stop();

src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function process(ContainerBuilder $container)
5252
return;
5353
}
5454

55+
if ($container->hasParameter('event_dispatcher.aliases')) {
56+
$aliases = $container->getParameter('event_dispatcher.aliases');
57+
$container->getParameterBag()->remove('event_dispatcher.aliases');
58+
} else {
59+
$aliases = array();
60+
}
5561
$definition = $container->findDefinition($this->dispatcherService);
5662

5763
foreach ($container->findTaggedServiceIds($this->listenerTag, true) as $id => $events) {
@@ -61,6 +67,7 @@ public function process(ContainerBuilder $container)
6167
if (!isset($event['event'])) {
6268
throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
6369
}
70+
$event['event'] = $aliases[$event['event']] ?? $event['event'];
6471

6572
if (!isset($event['method'])) {
6673
$event['method'] = 'on'.preg_replace_callback(array(
@@ -98,6 +105,7 @@ public function process(ContainerBuilder $container)
98105
}
99106
$class = $r->name;
100107

108+
ExtractingEventDispatcher::$aliases = $aliases;
101109
ExtractingEventDispatcher::$subscriber = $class;
102110
$extractingDispatcher->addSubscriber($extractingDispatcher);
103111
foreach ($extractingDispatcher->listeners as $args) {
@@ -109,6 +117,7 @@ public function process(ContainerBuilder $container)
109117
}
110118
}
111119
$extractingDispatcher->listeners = array();
120+
ExtractingEventDispatcher::$aliases = array();
112121
}
113122
}
114123
}
@@ -120,6 +129,7 @@ class ExtractingEventDispatcher extends EventDispatcher implements EventSubscrib
120129
{
121130
public $listeners = array();
122131

132+
public static $aliases = array();
123133
public static $subscriber;
124134

125135
public function addListener($eventName, $listener, $priority = 0)
@@ -129,8 +139,12 @@ public function addListener($eventName, $listener, $priority = 0)
129139

130140
public static function getSubscribedEvents()
131141
{
132-
$callback = array(self::$subscriber, 'getSubscribedEvents');
142+
$events = array();
143+
144+
foreach (array(self::$subscriber, 'getSubscribedEvents')() as $eventName => $params) {
145+
$events[self::$aliases[$eventName] ?? $eventName] = $params;
146+
}
133147

134-
return $callback();
148+
return $events;
135149
}
136150
}

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,24 @@ class EventDispatcher implements EventDispatcherInterface
3333

3434
/**
3535
* {@inheritdoc}
36+
*
37+
* @param string|null $eventName
3638
*/
37-
public function dispatch($eventName, Event $event = null)
39+
public function dispatch($event/*, string $eventName = null*/)
3840
{
39-
if (null === $event) {
40-
$event = new Event();
41+
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
42+
43+
if ($event instanceof Event) {
44+
$eventName = $eventName ?? \get_class($event);
45+
} else {
46+
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
47+
$swap = $event;
48+
$event = $eventName ?? new Event();
49+
$eventName = $swap;
50+
51+
if (!$event instanceof Event) {
52+
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
53+
}
4154
}
4255

4356
if ($listeners = $this->getListeners($eventName)) {

src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ interface EventDispatcherInterface
2323
/**
2424
* Dispatches an event to all registered listeners.
2525
*
26-
* @param string $eventName The name of the event to dispatch. The name of
27-
* the event is the name of the method that is
28-
* invoked on listeners.
2926
* @param Event $event The event to pass to the event handlers/listeners
30-
* If not supplied, an empty Event instance is created
27+
* @param string $eventName The name of the event to dispatch. If not supplied,
28+
* the class of $event should be used instead.
3129
*
3230
* @return Event
3331
*/
34-
public function dispatch($eventName, Event $event = null);
32+
public function dispatch($event/*, string $eventName = null*/);
3533

3634
/**
3735
* Adds an event listener that listens on the specified events.

0 commit comments

Comments
 (0)