ImmutableEventDispatcher.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  12. * A read-only proxy for an event dispatcher.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class ImmutableEventDispatcher implements EventDispatcherInterface
  17. {
  18. public function __construct(
  19. private EventDispatcherInterface $dispatcher,
  20. ) {
  21. }
  22. public function dispatch(object $event, ?string $eventName = null): object
  23. {
  24. return $this->dispatcher->dispatch($event, $eventName);
  25. }
  26. public function addListener(string $eventName, callable|array $listener, int $priority = 0): never
  27. {
  28. throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
  29. }
  30. public function addSubscriber(EventSubscriberInterface $subscriber): never
  31. {
  32. throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
  33. }
  34. public function removeListener(string $eventName, callable|array $listener): never
  35. {
  36. throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
  37. }
  38. public function removeSubscriber(EventSubscriberInterface $subscriber): never
  39. {
  40. throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
  41. }
  42. public function getListeners(?string $eventName = null): array
  43. {
  44. return $this->dispatcher->getListeners($eventName);
  45. }
  46. public function getListenerPriority(string $eventName, callable|array $listener): ?int
  47. {
  48. return $this->dispatcher->getListenerPriority($eventName, $listener);
  49. }
  50. public function hasListeners(?string $eventName = null): bool
  51. {
  52. return $this->dispatcher->hasListeners($eventName);
  53. }
  54. }