Skip to content

Commit 88c5a76

Browse files
committed
feature #30691 [Contracts][EventDispatcher] add EventDispatcherInterface to symfony/contracts and use it where possible (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Contracts][EventDispatcher] add EventDispatcherInterface to symfony/contracts and use it where possible | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR adds a new `EventDispatcherInterface` in `Contracts`. This interface contains only one method: `dispatch($event, $eventName)`. That covers almost all use cases of the event dispatcher in components (some use add/removeListeners/Subscribers but they are a minority.) While doing so, it allows dispatching any objects as events - not only instances of `Event`. This allows decoupling e.g. `Messenger` from the `EventDispatcher` component. Next steps could be about planning to remove the base `Event` class from some concrete event classes and/or moving `EventSubscriberInterface` to `symfony/contracts`. It would allow decoupling e.g. `Workflow` from the component - but that's too far away for now, let's think about it in 5.1. Commits ------- 3c3db2f14a [Contracts][EventDispatcher] add EventDispatcherInterface to symfony/contracts and use it where possible
2 parents b080ee0 + 5fd7b10 commit 88c5a76

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added `HttpClient` namespace with contracts for implementing flexible HTTP clients
8+
* added `EventDispatcher\EventDispatcherInterface`
89

910
1.0.0
1011
-----
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\EventDispatcherInterface as PsrEventDispatcherInterface;
15+
16+
if (interface_exists(PsrEventDispatcherInterface::class)) {
17+
/**
18+
* Allows providing hooks on domain-specific lifecycles by dispatching events.
19+
*/
20+
interface EventDispatcherInterface extends PsrEventDispatcherInterface
21+
{
22+
/**
23+
* Dispatches an event to all registered listeners.
24+
*
25+
* For BC with Symfony 4, the $eventName argument is not declared explicitly on the
26+
* signature of the method. Implementations that are not bound by this BC contraint
27+
* MUST declare it explicitly, as allowed by PHP.
28+
*
29+
* @param object $event The event to pass to the event handlers/listeners
30+
* @param string|null $eventName The name of the event to dispatch. If not supplied,
31+
* the class of $event should be used instead.
32+
*
33+
* @return object The passed $event MUST be returned
34+
*/
35+
public function dispatch($event/*, string $eventName = null*/);
36+
}
37+
} else {
38+
/**
39+
* Allows providing hooks on domain-specific lifecycles by dispatching events.
40+
*/
41+
interface EventDispatcherInterface
42+
{
43+
/**
44+
* Dispatches an event to all registered listeners.
45+
*
46+
* For BC with Symfony 4, the $eventName argument is not declared explicitly on the
47+
* signature of the method. Implementations that are not bound by this BC contraint
48+
* MUST declare it explicitly, as allowed by PHP.
49+
*
50+
* @param object $event The event to pass to the event handlers/listeners
51+
* @param string|null $eventName The name of the event to dispatch. If not supplied,
52+
* the class of $event should be used instead.
53+
*
54+
* @return object The passed $event MUST be returned
55+
*/
56+
public function dispatch($event/*, string $eventName = null*/);
57+
}
58+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"psr/cache": "When using the Cache contracts",
2828
"psr/container": "When using the Service contracts",
2929
"symfony/cache-contracts-implementation": "",
30+
"symfony/event-dispatcher-implementation": "",
3031
"symfony/http-client-contracts-implementation": "",
3132
"symfony/service-contracts-implementation": "",
3233
"symfony/translation-contracts-implementation": ""

0 commit comments

Comments
 (0)