SymfonyAdapter.php 5.6 KB

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