You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.9 KiB

  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 OCP\ILogger;
  25. use function is_callable;
  26. use OCP\EventDispatcher\Event;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. class SymfonyAdapter implements EventDispatcherInterface {
  30. /** @var EventDispatcher */
  31. private $eventDispatcher;
  32. /** @var ILogger */
  33. private $logger;
  34. public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) {
  35. $this->eventDispatcher = $eventDispatcher;
  36. $this->logger = $logger;
  37. }
  38. /**
  39. * Dispatches an event to all registered listeners.
  40. *
  41. * @param string $eventName The name of the event to dispatch. The name of
  42. * the event is the name of the method that is
  43. * invoked on listeners.
  44. * @param Event|null $event The event to pass to the event handlers/listeners
  45. * If not supplied, an empty Event instance is created
  46. *
  47. * @return void
  48. */
  49. public function dispatch($eventName, $event = null) {
  50. // type hinting is not possible, due to usage of GenericEvent
  51. if ($event instanceof Event) {
  52. $this->eventDispatcher->dispatch($eventName, $event);
  53. } else {
  54. // Legacy event
  55. $this->logger->info(
  56. 'Deprecated event type for {name}: {class}',
  57. [ 'name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null' ]
  58. );
  59. $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $event);
  60. }
  61. }
  62. /**
  63. * Adds an event listener that listens on the specified events.
  64. *
  65. * @param string $eventName The event to listen on
  66. * @param callable $listener The listener
  67. * @param int $priority The higher this value, the earlier an event
  68. * listener will be triggered in the chain (defaults to 0)
  69. */
  70. public function addListener($eventName, $listener, $priority = 0) {
  71. if (is_callable($listener)) {
  72. $this->eventDispatcher->addListener($eventName, $listener, $priority);
  73. } else {
  74. // Legacy listener
  75. $this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority);
  76. }
  77. }
  78. /**
  79. * Adds an event subscriber.
  80. *
  81. * The subscriber is asked for all the events it is
  82. * interested in and added as a listener for these events.
  83. */
  84. public function addSubscriber(EventSubscriberInterface $subscriber) {
  85. $this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber);
  86. }
  87. /**
  88. * Removes an event listener from the specified events.
  89. *
  90. * @param string $eventName The event to remove a listener from
  91. * @param callable $listener The listener to remove
  92. */
  93. public function removeListener($eventName, $listener) {
  94. $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
  95. }
  96. public function removeSubscriber(EventSubscriberInterface $subscriber) {
  97. $this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber);
  98. }
  99. /**
  100. * Gets the listeners of a specific event or all listeners sorted by descending priority.
  101. *
  102. * @param string|null $eventName The name of the event
  103. *
  104. * @return array The event listeners for the specified event, or all event listeners by event name
  105. */
  106. public function getListeners($eventName = null) {
  107. return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName);
  108. }
  109. /**
  110. * Gets the listener priority for a specific event.
  111. *
  112. * Returns null if the event or the listener does not exist.
  113. *
  114. * @param string $eventName The name of the event
  115. * @param callable $listener The listener
  116. *
  117. * @return int|null The event listener priority
  118. */
  119. public function getListenerPriority($eventName, $listener) {
  120. return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener);
  121. }
  122. /**
  123. * Checks whether an event has any registered listeners.
  124. *
  125. * @param string|null $eventName The name of the event
  126. *
  127. * @return bool true if the specified event has any listeners, false otherwise
  128. */
  129. public function hasListeners($eventName = null) {
  130. return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
  131. }
  132. }