-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EventDispatcher] swap arguments of dispatch() to allow registering e…
…vents by FQCN
- Loading branch information
1 parent
1618e06
commit 1a30b07
Showing
11 changed files
with
287 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher; | ||
|
||
/** | ||
* An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch(). | ||
* | ||
* This class should be deprecated in Symfony 5.1 | ||
* | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
final class LegacyEventDispatcherProxy implements EventDispatcherInterface | ||
{ | ||
private $dispatcher; | ||
|
||
public static function decorate(?EventDispatcherInterface $dispatcher): ?EventDispatcherInterface | ||
{ | ||
if (null === $dispatcher) { | ||
return null; | ||
} | ||
$r = new \ReflectionMethod($dispatcher, 'dispatch'); | ||
$param2 = $r->getParameters()[1] ?? null; | ||
|
||
if (!$param2 || !$param2->hasType() || $param2->getType()->isBuiltin()) { | ||
return $dispatcher; | ||
} | ||
|
||
@trigger_error(sprintf('The signature of the "%s::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.', $r->class), E_USER_DEPRECATED); | ||
|
||
$self = new self(); | ||
$self->dispatcher = $dispatcher; | ||
|
||
return $self; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param string|null $eventName | ||
*/ | ||
public function dispatch($event/*, string $eventName = null*/) | ||
{ | ||
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null; | ||
|
||
if ($event instanceof Event) { | ||
$eventName = $eventName ?? \get_class($event); | ||
} else { | ||
@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); | ||
$swap = $event; | ||
$event = $eventName ?? new Event(); | ||
$eventName = $swap; | ||
|
||
if (!$event instanceof Event) { | ||
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))); | ||
} | ||
} | ||
|
||
$listeners = $this->getListeners($eventName); | ||
|
||
foreach ($listeners as $listener) { | ||
if ($event->isPropagationStopped()) { | ||
break; | ||
} | ||
$listener($event, $eventName, $this); | ||
} | ||
|
||
return $event; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addListener($eventName, $listener, $priority = 0) | ||
{ | ||
return $this->dispatcher->addListener($eventName, $listener, $priority); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addSubscriber(EventSubscriberInterface $subscriber) | ||
{ | ||
return $this->dispatcher->addSubscriber($subscriber); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function removeListener($eventName, $listener) | ||
{ | ||
return $this->dispatcher->removeListener($eventName, $listener); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function removeSubscriber(EventSubscriberInterface $subscriber) | ||
{ | ||
return $this->dispatcher->removeSubscriber($subscriber); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getListeners($eventName = null) | ||
{ | ||
return $this->dispatcher->getListeners($eventName); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getListenerPriority($eventName, $listener) | ||
{ | ||
return $this->dispatcher->getListenerPriority($eventName, $listener); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasListeners($eventName = null) | ||
{ | ||
return $this->dispatcher->hasListeners($eventName); | ||
} | ||
|
||
/** | ||
* Proxies all method calls to the original event dispatcher. | ||
*/ | ||
public function __call($method, $arguments) | ||
{ | ||
return $this->dispatcher->{$method}(...$arguments); | ||
} | ||
} |
Oops, something went wrong.