SymfonyAdapter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\EventDispatcher;
  24. use function is_callable;
  25. use OCP\EventDispatcher\Event;
  26. use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. class SymfonyAdapter implements EventDispatcherInterface {
  30. /** @var EventDispatcher */
  31. private $eventDispatcher;
  32. public function __construct(EventDispatcher $eventDispatcher) {
  33. $this->eventDispatcher = $eventDispatcher;
  34. }
  35. /**
  36. * Dispatches an event to all registered listeners.
  37. *
  38. * @param string $eventName The name of the event to dispatch. The name of
  39. * the event is the name of the method that is
  40. * invoked on listeners.
  41. * @param SymfonyEvent|null $event The event to pass to the event handlers/listeners
  42. * If not supplied, an empty Event instance is created
  43. *
  44. * @return SymfonyEvent
  45. */
  46. public function dispatch($eventName, SymfonyEvent $event = null) {
  47. if ($event instanceof Event) {
  48. $this->eventDispatcher->dispatch($eventName, $event);
  49. } else {
  50. // Legacy event
  51. $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $event);
  52. }
  53. }
  54. /**
  55. * Adds an event listener that listens on the specified events.
  56. *
  57. * @param string $eventName The event to listen on
  58. * @param callable $listener The listener
  59. * @param int $priority The higher this value, the earlier an event
  60. * listener will be triggered in the chain (defaults to 0)
  61. */
  62. public function addListener($eventName, $listener, $priority = 0) {
  63. if (is_callable($listener)) {
  64. $this->eventDispatcher->addListener($eventName, $listener, $priority);
  65. } else {
  66. // Legacy listener
  67. $this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority);
  68. }
  69. }
  70. /**
  71. * Adds an event subscriber.
  72. *
  73. * The subscriber is asked for all the events it is
  74. * interested in and added as a listener for these events.
  75. */
  76. public function addSubscriber(EventSubscriberInterface $subscriber) {
  77. $this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber);
  78. }
  79. /**
  80. * Removes an event listener from the specified events.
  81. *
  82. * @param string $eventName The event to remove a listener from
  83. * @param callable $listener The listener to remove
  84. */
  85. public function removeListener($eventName, $listener) {
  86. $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
  87. }
  88. public function removeSubscriber(EventSubscriberInterface $subscriber) {
  89. $this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber);
  90. }
  91. /**
  92. * Gets the listeners of a specific event or all listeners sorted by descending priority.
  93. *
  94. * @param string|null $eventName The name of the event
  95. *
  96. * @return array The event listeners for the specified event, or all event listeners by event name
  97. */
  98. public function getListeners($eventName = null) {
  99. return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName);
  100. }
  101. /**
  102. * Gets the listener priority for a specific event.
  103. *
  104. * Returns null if the event or the listener does not exist.
  105. *
  106. * @param string $eventName The name of the event
  107. * @param callable $listener The listener
  108. *
  109. * @return int|null The event listener priority
  110. */
  111. public function getListenerPriority($eventName, $listener) {
  112. return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener);
  113. }
  114. /**
  115. * Checks whether an event has any registered listeners.
  116. *
  117. * @param string|null $eventName The name of the event
  118. *
  119. * @return bool true if the specified event has any listeners, false otherwise
  120. */
  121. public function hasListeners($eventName = null) {
  122. return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
  123. }
  124. }