|
| 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\Component\EventDispatcher; |
| 13 | + |
| 14 | +/** |
| 15 | + * An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch(). |
| 16 | + * |
| 17 | + * This class should be deprecated in Symfony 5.1 |
| 18 | + * |
| 19 | + * @author Nicolas Grekas <p@tchwork.com> |
| 20 | + */ |
| 21 | +final class LegacyEventDispatcherProxy implements EventDispatcherInterface |
| 22 | +{ |
| 23 | + private $dispatcher; |
| 24 | + |
| 25 | + public static function decorate(?EventDispatcherInterface $dispatcher): ?EventDispatcherInterface |
| 26 | + { |
| 27 | + if (null === $dispatcher) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + $r = new \ReflectionMethod($dispatcher, 'dispatch'); |
| 31 | + $param2 = $r->getParameters()[1] ?? null; |
| 32 | + |
| 33 | + if (!$param2 || !$param2->hasType() || $param2->getType()->isBuiltin()) { |
| 34 | + return $dispatcher; |
| 35 | + } |
| 36 | + |
| 37 | + @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); |
| 38 | + |
| 39 | + $self = new self(); |
| 40 | + $self->dispatcher = $dispatcher; |
| 41 | + |
| 42 | + return $self; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + * |
| 48 | + * @param string|null $eventName |
| 49 | + */ |
| 50 | + public function dispatch($event/*, string $eventName = null*/) |
| 51 | + { |
| 52 | + $eventName = 1 < \func_num_args() ? \func_get_arg(1) : null; |
| 53 | + |
| 54 | + if ($event instanceof Event) { |
| 55 | + $eventName = $eventName ?? \get_class($event); |
| 56 | + } else { |
| 57 | + @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); |
| 58 | + $swap = $event; |
| 59 | + $event = $eventName ?? new Event(); |
| 60 | + $eventName = $swap; |
| 61 | + |
| 62 | + if (!$event instanceof Event) { |
| 63 | + 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))); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $listeners = $this->getListeners($eventName); |
| 68 | + |
| 69 | + foreach ($listeners as $listener) { |
| 70 | + if ($event->isPropagationStopped()) { |
| 71 | + break; |
| 72 | + } |
| 73 | + $listener($event, $eventName, $this); |
| 74 | + } |
| 75 | + |
| 76 | + return $event; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritdoc} |
| 81 | + */ |
| 82 | + public function addListener($eventName, $listener, $priority = 0) |
| 83 | + { |
| 84 | + return $this->dispatcher->addListener($eventName, $listener, $priority); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function addSubscriber(EventSubscriberInterface $subscriber) |
| 91 | + { |
| 92 | + return $this->dispatcher->addSubscriber($subscriber); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + */ |
| 98 | + public function removeListener($eventName, $listener) |
| 99 | + { |
| 100 | + return $this->dispatcher->removeListener($eventName, $listener); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * {@inheritdoc} |
| 105 | + */ |
| 106 | + public function removeSubscriber(EventSubscriberInterface $subscriber) |
| 107 | + { |
| 108 | + return $this->dispatcher->removeSubscriber($subscriber); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * {@inheritdoc} |
| 113 | + */ |
| 114 | + public function getListeners($eventName = null) |
| 115 | + { |
| 116 | + return $this->dispatcher->getListeners($eventName); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * {@inheritdoc} |
| 121 | + */ |
| 122 | + public function getListenerPriority($eventName, $listener) |
| 123 | + { |
| 124 | + return $this->dispatcher->getListenerPriority($eventName, $listener); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * {@inheritdoc} |
| 129 | + */ |
| 130 | + public function hasListeners($eventName = null) |
| 131 | + { |
| 132 | + return $this->dispatcher->hasListeners($eventName); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Proxies all method calls to the original event dispatcher. |
| 137 | + */ |
| 138 | + public function __call($method, $arguments) |
| 139 | + { |
| 140 | + return $this->dispatcher->{$method}(...$arguments); |
| 141 | + } |
| 142 | +} |
0 commit comments