Skip to content

Commit 3edb79f

Browse files
committed
feature #30772 [Contracts][EventDispatcher] move the Event class to symfony/contracts (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Contracts][EventDispatcher] move the Event class to symfony/contracts | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Will allow decoupling more components in 5.0 Commits ------- a4ce08e [Contracts][EventDispatcher] move the Event class to symfony/contracts
2 parents f4176b0 + a4ce08e commit 3edb79f

File tree

11 files changed

+114
-31
lines changed

11 files changed

+114
-31
lines changed

UPGRADE-4.3.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ EventDispatcher
4242
---------------
4343
4444
* The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated
45+
* The `Event` class has been deprecated, use `Symfony\Contracts\EventDispatcher\Event` instead
4546

4647
Form
4748
----

UPGRADE-5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ EventDispatcher
7171

7272
* The `TraceableEventDispatcherInterface` has been removed.
7373
* The signature of the `EventDispatcherInterface::dispatch()` method has been updated to `dispatch($event, string $eventName = null)`
74+
* The `Event` class has been removed, use `Symfony\Contracts\EventDispatcher\Event` instead
7475

7576
DependencyInjection
7677
-------------------

src/Symfony/Component/EventDispatcher/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated
8+
* deprecated the `Event` class, use `Symfony\Contracts\EventDispatcher\Event` instead
89

910
4.1.0
1011
-----

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1919
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
2020
use Symfony\Component\Stopwatch\Stopwatch;
21+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
2122

2223
/**
2324
* Collects some data about event listeners.
@@ -147,7 +148,7 @@ public function dispatch($event/*, string $eventName = null*/)
147148
}
148149
}
149150

150-
if (null !== $this->logger && ($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
151+
if (null !== $this->logger && ($event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
151152
$this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
152153
}
153154

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1717
use Symfony\Component\Stopwatch\Stopwatch;
1818
use Symfony\Component\VarDumper\Caster\ClassStub;
19+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
1920

2021
/**
2122
* @author Fabien Potencier <fabien@symfony.com>
@@ -123,7 +124,7 @@ public function __invoke(Event $event, $eventName, EventDispatcherInterface $dis
123124
$e->stop();
124125
}
125126

126-
if (($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
127+
if (($event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
127128
$this->stoppedPropagation = true;
128129
}
129130
}

src/Symfony/Component/EventDispatcher/Event.php

+4-24
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,24 @@
1212
namespace Symfony\Component\EventDispatcher;
1313

1414
/**
15-
* Event is the base class for classes containing event data.
16-
*
17-
* This class contains no event data. It is used by events that do not pass
18-
* state information to an event handler when an event is raised.
19-
*
20-
* You can call the method stopPropagation() to abort the execution of
21-
* further listeners in your event listener.
22-
*
23-
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
24-
* @author Jonathan Wage <jonwage@gmail.com>
25-
* @author Roman Borschel <roman@code-factory.org>
26-
* @author Bernhard Schussek <bschussek@gmail.com>
15+
* @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
2716
*/
2817
class Event
2918
{
30-
/**
31-
* @var bool Whether no further event listeners should be triggered
32-
*/
3319
private $propagationStopped = false;
3420

3521
/**
36-
* Returns whether further event listeners should be triggered.
37-
*
38-
* @see Event::stopPropagation()
39-
*
4022
* @return bool Whether propagation was already stopped for this event
23+
*
24+
* @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
4125
*/
4226
public function isPropagationStopped()
4327
{
4428
return $this->propagationStopped;
4529
}
4630

4731
/**
48-
* Stops the propagation of the event to further event listeners.
49-
*
50-
* If multiple event listeners are connected to the same event, no
51-
* further event listener will be triggered once any trigger calls
52-
* stopPropagation().
32+
* @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
5333
*/
5434
public function stopPropagation()
5535
{

src/Symfony/Component/EventDispatcher/EventDispatcher.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\EventDispatcher;
1313

1414
use Psr\EventDispatcher\StoppableEventInterface;
15+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
1516

1617
/**
1718
* The EventDispatcherInterface is the central point of Symfony's event listener system.
@@ -237,7 +238,7 @@ protected function callListeners(iterable $listeners, string $eventName, $event)
237238
*/
238239
protected function doDispatch($listeners, $eventName, Event $event)
239240
{
240-
$stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;
241+
$stoppable = $event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
241242

242243
foreach ($listeners as $listener) {
243244
if ($stoppable && $event->isPropagationStopped()) {

src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\EventDispatcher;
1313

1414
use Psr\EventDispatcher\StoppableEventInterface;
15+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
1516
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1617

1718
/**
@@ -68,7 +69,7 @@ public function dispatch($event/*, string $eventName = null*/)
6869
}
6970

7071
$listeners = $this->getListeners($eventName);
71-
$stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;
72+
$stoppable = $event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
7273

7374
foreach ($listeners as $listener) {
7475
if ($stoppable && $event->isPropagationStopped()) {

src/Symfony/Component/EventDispatcher/Tests/EventTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\EventDispatcher\Event;
1616

1717
/**
18-
* Test class for Event.
18+
* @group legacy
1919
*/
2020
class EventTest extends TestCase
2121
{

src/Symfony/Contracts/CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ CHANGELOG
55
-----
66

77
* added `HttpClient` namespace with contracts for implementing flexible HTTP clients
8-
* added `EventDispatcher\EventDispatcherInterface`
9-
* added `ServiceProviderInterface`
8+
* added `EventDispatcherInterface` and `Event` in namespace `EventDispatcher`
9+
* added `ServiceProviderInterface` in namespace `Service`
1010

1111
1.0.0
1212
-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Contracts\EventDispatcher;
13+
14+
use Psr\EventDispatcher\StoppableEventInterface;
15+
16+
if (interface_exists(StoppableEventInterface::class)) {
17+
/**
18+
* Event is the base class for classes containing event data.
19+
*
20+
* This class contains no event data. It is used by events that do not pass
21+
* state information to an event handler when an event is raised.
22+
*
23+
* You can call the method stopPropagation() to abort the execution of
24+
* further listeners in your event listener.
25+
*
26+
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
27+
* @author Jonathan Wage <jonwage@gmail.com>
28+
* @author Roman Borschel <roman@code-factory.org>
29+
* @author Bernhard Schussek <bschussek@gmail.com>
30+
* @author Nicolas Grekas <p@tchwork.com>
31+
*/
32+
class Event implements StoppableEventInterface
33+
{
34+
private $propagationStopped = false;
35+
36+
/**
37+
* Returns whether further event listeners should be triggered.
38+
*/
39+
public function isPropagationStopped(): bool
40+
{
41+
return $this->propagationStopped;
42+
}
43+
44+
/**
45+
* Stops the propagation of the event to further event listeners.
46+
*
47+
* If multiple event listeners are connected to the same event, no
48+
* further event listener will be triggered once any trigger calls
49+
* stopPropagation().
50+
*/
51+
public function stopPropagation(): void
52+
{
53+
$this->propagationStopped = true;
54+
}
55+
}
56+
} else {
57+
/**
58+
* Event is the base class for classes containing event data.
59+
*
60+
* This class contains no event data. It is used by events that do not pass
61+
* state information to an event handler when an event is raised.
62+
*
63+
* You can call the method stopPropagation() to abort the execution of
64+
* further listeners in your event listener.
65+
*
66+
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
67+
* @author Jonathan Wage <jonwage@gmail.com>
68+
* @author Roman Borschel <roman@code-factory.org>
69+
* @author Bernhard Schussek <bschussek@gmail.com>
70+
* @author Nicolas Grekas <p@tchwork.com>
71+
*/
72+
class Event
73+
{
74+
private $propagationStopped = false;
75+
76+
/**
77+
* Returns whether further event listeners should be triggered.
78+
*/
79+
public function isPropagationStopped(): bool
80+
{
81+
return $this->propagationStopped;
82+
}
83+
84+
/**
85+
* Stops the propagation of the event to further event listeners.
86+
*
87+
* If multiple event listeners are connected to the same event, no
88+
* further event listener will be triggered once any trigger calls
89+
* stopPropagation().
90+
*/
91+
public function stopPropagation(): void
92+
{
93+
$this->propagationStopped = true;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)