Sen descrición

LegacyEventDispatcherProxy.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher;
  11. use Psr\EventDispatcher\StoppableEventInterface;
  12. use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
  14. /**
  15. * A 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. public static function decorate(?ContractsEventDispatcherInterface $dispatcher): ?ContractsEventDispatcherInterface
  25. {
  26. if (null === $dispatcher) {
  27. return null;
  28. }
  29. $r = new \ReflectionMethod($dispatcher, 'dispatch');
  30. $param2 = $r->getParameters()[1] ?? null;
  31. if (!$param2 || !$param2->hasType() || $param2->getType()->isBuiltin()) {
  32. return $dispatcher;
  33. }
  34. @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);
  35. $self = new self();
  36. $self->dispatcher = $dispatcher;
  37. return $self;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * @param string|null $eventName
  43. *
  44. * @return object
  45. */
  46. public function dispatch($event/*, string $eventName = null*/)
  47. {
  48. $eventName = 1 < \func_num_args() ? func_get_arg(1) : null;
  49. if (\is_object($event)) {
  50. $eventName = $eventName ?? \get_class($event);
  51. } elseif (\is_string($event) && (null === $eventName || $eventName instanceof ContractsEvent || $eventName instanceof Event)) {
  52. @trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.', ContractsEventDispatcherInterface::class), \E_USER_DEPRECATED);
  53. $swap = $event;
  54. $event = $eventName ?? new Event();
  55. $eventName = $swap;
  56. } else {
  57. throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, "%s" given.', ContractsEventDispatcherInterface::class, \is_object($event) ? \get_class($event) : \gettype($event)));
  58. }
  59. $listeners = $this->getListeners($eventName);
  60. $stoppable = $event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
  61. foreach ($listeners as $listener) {
  62. if ($stoppable && $event->isPropagationStopped()) {
  63. break;
  64. }
  65. $listener($event, $eventName, $this);
  66. }
  67. return $event;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function addListener($eventName, $listener, $priority = 0)
  73. {
  74. return $this->dispatcher->addListener($eventName, $listener, $priority);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function addSubscriber(EventSubscriberInterface $subscriber)
  80. {
  81. return $this->dispatcher->addSubscriber($subscriber);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function removeListener($eventName, $listener)
  87. {
  88. return $this->dispatcher->removeListener($eventName, $listener);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function removeSubscriber(EventSubscriberInterface $subscriber)
  94. {
  95. return $this->dispatcher->removeSubscriber($subscriber);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getListeners($eventName = null): array
  101. {
  102. return $this->dispatcher->getListeners($eventName);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getListenerPriority($eventName, $listener): ?int
  108. {
  109. return $this->dispatcher->getListenerPriority($eventName, $listener);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function hasListeners($eventName = null): bool
  115. {
  116. return $this->dispatcher->hasListeners($eventName);
  117. }
  118. /**
  119. * Proxies all method calls to the original event dispatcher.
  120. */
  121. public function __call($method, $arguments)
  122. {
  123. return $this->dispatcher->{$method}(...$arguments);
  124. }
  125. }