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

Skip to content

Commit 3c3db2f

Browse files
[Contracts][EventDispatcher] add EventDispatcherInterface to symfony/contracts and use it where possible
1 parent 8585317 commit 3c3db2f

File tree

55 files changed

+196
-92
lines changed

Some content is hidden

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

55 files changed

+196
-92
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<tag name="container.hot_path" />
5050
</service>
5151
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" />
52+
<service id="Symfony\Contracts\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" />
5253

5354
<service id="http_kernel" class="Symfony\Component\HttpKernel\HttpKernel" public="true">
5455
<argument type="service" id="event_dispatcher" />

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/AutowiringTypes/AutowiredServices.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\Reader;
1515
use Psr\Cache\CacheItemPoolInterface;
16-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1717

1818
class AutowiredServices
1919
{

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-xml": "*",
2121
"symfony/cache": "~4.3",
2222
"symfony/config": "~4.2",
23-
"symfony/contracts": "^1.0.2",
23+
"symfony/contracts": "^1.1",
2424
"symfony/dependency-injection": "^4.3",
2525
"symfony/http-foundation": "^4.3",
2626
"symfony/http-kernel": "^4.3",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class FirewallListener extends Firewall
3030

3131
public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher, LogoutUrlGenerator $logoutUrlGenerator)
3232
{
33+
// the type-hint will be updated to the "EventDispatcherInterface" from symfony/contracts in 5.0
34+
3335
$this->map = $map;
3436
$this->logoutUrlGenerator = $logoutUrlGenerator;
3537

src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
1818
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
1919
use Symfony\Component\EventDispatcher\EventDispatcher;
20-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2120
use Symfony\Component\HttpFoundation\Request;
2221
use Symfony\Component\HttpFoundation\Response;
2322
use Symfony\Component\HttpKernel\Event\RequestEvent;
@@ -34,6 +33,7 @@
3433
use Symfony\Component\Security\Core\Role\SwitchUserRole;
3534
use Symfony\Component\Security\Http\FirewallMapInterface;
3635
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
36+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3737

3838
class SecurityDataCollectorTest extends TestCase
3939
{

src/Symfony/Component/Console/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN
9191
$this->defaultCommand = 'list';
9292
}
9393

94+
/**
95+
* @final since Symfony 4.3, the type-hint will be updated to the interface from symfony/contracts in 5.0
96+
*/
9497
public function setDispatcher(EventDispatcherInterface $dispatcher)
9598
{
9699
$this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher);

src/Symfony/Component/Console/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0",
20+
"symfony/contracts": "^1.1",
2121
"symfony/polyfill-mbstring": "~1.0",
2222
"symfony/polyfill-php73": "^1.8"
2323
},

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
226226
if ($parameter->isDefaultValueAvailable()) {
227227
$value = $parameter->getDefaultValue();
228228
} elseif (!$parameter->allowsNull()) {
229-
if (\function_exists('xdebug_disable')) {
230-
xdebug_disable();
231-
}
232-
try {
233-
throw new AutowiringFailedException($this->currentId, $failureMessage);
234-
} finally {
235-
if (\function_exists('xdebug_enable')) {
236-
xdebug_enable();
237-
}
238-
}
229+
throw new AutowiringFailedException($this->currentId, $failureMessage);
239230
}
240231
}
241232

src/Symfony/Component/DependencyInjection/Exception/AutowiringFailedException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function __construct(string $serviceId, $message = '', int $code = 0, \Ex
2323
{
2424
$this->serviceId = $serviceId;
2525

26+
if ($message instanceof \Closure && \function_exists('xdebug_is_enabled') && xdebug_is_enabled()) {
27+
$message = $message();
28+
}
29+
2630
if (!$message instanceof \Closure) {
2731
parent::__construct($message, $code, $previous);
2832

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\EventDispatcher\Debug;
1313

14+
use Psr\EventDispatcher\StoppableEventInterface;
1415
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\EventDispatcher\Event;
1617
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -133,7 +134,7 @@ public function dispatch($event/*, string $eventName = null*/)
133134

134135
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
135136

136-
if ($event instanceof Event) {
137+
if (\is_object($event)) {
137138
$eventName = $eventName ?? \get_class($event);
138139
} else {
139140
@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);
@@ -146,13 +147,13 @@ public function dispatch($event/*, string $eventName = null*/)
146147
}
147148
}
148149

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

153154
$this->preProcess($eventName);
154155
try {
155-
$this->preDispatch($eventName, $event);
156+
$this->beforeDispatch($eventName, $event);
156157
try {
157158
$e = $this->stopwatch->start($eventName, 'section');
158159
try {
@@ -163,7 +164,7 @@ public function dispatch($event/*, string $eventName = null*/)
163164
}
164165
}
165166
} finally {
166-
$this->postDispatch($eventName, $event);
167+
$this->afterDispatch($eventName, $event);
167168
}
168169
} finally {
169170
$this->postProcess($eventName);
@@ -262,18 +263,32 @@ public function __call($method, $arguments)
262263
/**
263264
* Called before dispatching the event.
264265
*
265-
* @param string $eventName The event name
266-
* @param Event $event The event
266+
* @param object $event
267267
*/
268-
protected function preDispatch($eventName, Event $event)
268+
protected function beforeDispatch(string $eventName, $event)
269269
{
270+
$this->preDispatch($eventName, $event);
270271
}
271272

272273
/**
273274
* Called after dispatching the event.
274275
*
275-
* @param string $eventName The event name
276-
* @param Event $event The event
276+
* @param object $event
277+
*/
278+
protected function afterDispatch(string $eventName, $event)
279+
{
280+
$this->postDispatch($eventName, $event);
281+
}
282+
283+
/**
284+
* @deprecated since Symfony 4.3, will be removed in 5.0, use beforeDispatch instead
285+
*/
286+
protected function preDispatch($eventName, Event $event)
287+
{
288+
}
289+
290+
/**
291+
* @deprecated since Symfony 4.3, will be removed in 5.0, use afterDispatch instead
277292
*/
278293
protected function postDispatch($eventName, Event $event)
279294
{

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

1212
namespace Symfony\Component\EventDispatcher\Debug;
1313

14+
use Psr\EventDispatcher\StoppableEventInterface;
1415
use Symfony\Component\EventDispatcher\Event;
1516
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1617
use Symfony\Component\Stopwatch\Stopwatch;
1718
use Symfony\Component\VarDumper\Caster\ClassStub;
1819

1920
/**
2021
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @final since Symfony 4.3: the "Event" type-hint on __invoke() will be replaced by "object" in 5.0
2124
*/
2225
class WrappedListener
2326
{
@@ -120,7 +123,7 @@ public function __invoke(Event $event, $eventName, EventDispatcherInterface $dis
120123
$e->stop();
121124
}
122125

123-
if ($event->isPropagationStopped()) {
126+
if (($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
124127
$this->stoppedPropagation = true;
125128
}
126129
}

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\EventDispatcher;
1313

14+
use Psr\EventDispatcher\StoppableEventInterface;
15+
1416
/**
1517
* The EventDispatcherInterface is the central point of Symfony's event listener system.
1618
*
@@ -48,7 +50,7 @@ public function dispatch($event/*, string $eventName = null*/)
4850
{
4951
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
5052

51-
if ($event instanceof Event) {
53+
if (\is_object($event)) {
5254
$eventName = $eventName ?? \get_class($event);
5355
} else {
5456
@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);
@@ -223,12 +225,22 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
223225
*
224226
* @param callable[] $listeners The event listeners
225227
* @param string $eventName The name of the event to dispatch
226-
* @param Event $event The event object to pass to the event handlers/listeners
228+
* @param object $event The event object to pass to the event handlers/listeners
229+
*/
230+
protected function callListeners(iterable $listeners, string $eventName, $event)
231+
{
232+
$this->doDispatch($listeners, $eventName, $event);
233+
}
234+
235+
/**
236+
* @deprecated since Symfony 4.3, use callListeners() instead
227237
*/
228238
protected function doDispatch($listeners, $eventName, Event $event)
229239
{
240+
$stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;
241+
230242
foreach ($listeners as $listener) {
231-
if ($event->isPropagationStopped()) {
243+
if ($stoppable && $event->isPropagationStopped()) {
232244
break;
233245
}
234246
$listener($event, $eventName, $this);

src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,17 @@
1111

1212
namespace Symfony\Component\EventDispatcher;
1313

14+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
15+
1416
/**
1517
* The EventDispatcherInterface is the central point of Symfony's event listener system.
1618
* Listeners are registered on the manager and events are dispatched through the
1719
* manager.
1820
*
1921
* @author Bernhard Schussek <[email protected]>
2022
*/
21-
interface EventDispatcherInterface
23+
interface EventDispatcherInterface extends ContractsEventDispatcherInterface
2224
{
23-
/**
24-
* Dispatches an event to all registered listeners.
25-
*
26-
* @param Event $event The event to pass to the event handlers/listeners
27-
* @param string|null $eventName The name of the event to dispatch. If not supplied,
28-
* the class of $event should be used instead.
29-
*
30-
* @return Event
31-
*/
32-
public function dispatch($event/*, string $eventName = null*/);
33-
3425
/**
3526
* Adds an event listener that listens on the specified events.
3627
*

src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\EventDispatcher;
1313

14+
use Psr\EventDispatcher\StoppableEventInterface;
15+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
16+
1417
/**
1518
* An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch().
1619
*
@@ -51,7 +54,7 @@ public function dispatch($event/*, string $eventName = null*/)
5154
{
5255
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
5356

54-
if ($event instanceof Event) {
57+
if (\is_object($event)) {
5558
$eventName = $eventName ?? \get_class($event);
5659
} else {
5760
@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);
@@ -65,9 +68,10 @@ public function dispatch($event/*, string $eventName = null*/)
6568
}
6669

6770
$listeners = $this->getListeners($eventName);
71+
$stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;
6872

6973
foreach ($listeners as $listener) {
70-
if ($event->isPropagationStopped()) {
74+
if ($stoppable && $event->isPropagationStopped()) {
7175
break;
7276
}
7377
$listener($event, $eventName, $this);

src/Symfony/Component/EventDispatcher/composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0"
20+
"symfony/contracts": "^1.1"
2121
},
2222
"require-dev": {
2323
"symfony/dependency-injection": "~3.4|~4.0",
@@ -29,6 +29,10 @@
2929
"conflict": {
3030
"symfony/dependency-injection": "<3.4"
3131
},
32+
"provide": {
33+
"psr/event-dispatcher-implementation": "1.0",
34+
"symfony/event-dispatcher-implementation": "1.1"
35+
},
3236
"suggest": {
3337
"symfony/dependency-injection": "",
3438
"symfony/http-kernel": ""

src/Symfony/Component/Form/Test/TypeTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Test;
1313

14-
use Symfony\Component\EventDispatcher\EventDispatcher;
14+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1515
use Symfony\Component\Form\FormBuilder;
1616
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
1717

@@ -25,15 +25,15 @@ abstract class TypeTestCase extends FormIntegrationTestCase
2525
protected $builder;
2626

2727
/**
28-
* @var EventDispatcher
28+
* @var EventDispatcherInterface
2929
*/
3030
protected $dispatcher;
3131

3232
private function doSetUp()
3333
{
3434
parent::setUp();
3535

36-
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
36+
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
3737
$this->builder = new FormBuilder('', null, $this->dispatcher, $this->factory);
3838
}
3939

src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
1515
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
16-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1716
use Symfony\Component\HttpFoundation\Request;
1817
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1919
use Symfony\Contracts\Service\ResetInterface;
2020

2121
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TraceableEventDispatcher extends BaseTraceableEventDispatcher
2727
/**
2828
* {@inheritdoc}
2929
*/
30-
protected function preDispatch($eventName, Event $event)
30+
protected function beforeDispatch(string $eventName, $event)
3131
{
3232
switch ($eventName) {
3333
case KernelEvents::REQUEST:
@@ -58,7 +58,7 @@ protected function preDispatch($eventName, Event $event)
5858
/**
5959
* {@inheritdoc}
6060
*/
61-
protected function postDispatch($eventName, Event $event)
61+
protected function afterDispatch(string $eventName, $event)
6262
{
6363
switch ($eventName) {
6464
case KernelEvents::CONTROLLER_ARGUMENTS:

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function logKernelException(GetResponseForExceptionEvent $event)
5454
$this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
5555
}
5656

57+
/**
58+
* @param string $eventName
59+
* @param EventDispatcherInterface $eventDispatcher
60+
*/
5761
public function onKernelException(GetResponseForExceptionEvent $event)
5862
{
5963
if (null === $this->controller) {

0 commit comments

Comments
 (0)