SymfonyAdapter.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\EventDispatcher;
  27. use OCP\EventDispatcher\Event;
  28. use Psr\Log\LoggerInterface;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. use Symfony\Component\EventDispatcher\GenericEvent;
  32. use function is_callable;
  33. use function is_object;
  34. use function is_string;
  35. /**
  36. * @deprecated 20.0.0 use \OCP\EventDispatcher\IEventDispatcher
  37. */
  38. class SymfonyAdapter implements EventDispatcherInterface {
  39. /** @var EventDispatcher */
  40. private $eventDispatcher;
  41. private LoggerInterface $logger;
  42. /**
  43. * @deprecated 20.0.0
  44. */
  45. public function __construct(EventDispatcher $eventDispatcher, LoggerInterface $logger) {
  46. $this->eventDispatcher = $eventDispatcher;
  47. $this->logger = $logger;
  48. }
  49. private static function detectEventAndName($a, $b) {
  50. if (is_object($a) && (is_string($b) || $b === null)) {
  51. // a is the event, the other one is the optional name
  52. return [$a, $b];
  53. }
  54. if (is_object($b) && (is_string($a) || $a === null)) {
  55. // b is the event, the other one is the optional name
  56. return [$b, $a];
  57. }
  58. if (is_string($a) && $b === null) {
  59. // a is a payload-less event
  60. return [null, $a];
  61. }
  62. if (is_string($b) && $a === null) {
  63. // b is a payload-less event
  64. return [null, $b];
  65. }
  66. // Anything else we can't detect
  67. return [$a, $b];
  68. }
  69. /**
  70. * Dispatches an event to all registered listeners.
  71. *
  72. * @param string $eventName The name of the event to dispatch. The name of
  73. * the event is the name of the method that is
  74. * invoked on listeners.
  75. * @param Event|null $event The event to pass to the event handlers/listeners
  76. * If not supplied, an empty Event instance is created
  77. *
  78. * @return object the emitted event
  79. * @deprecated 20.0.0
  80. */
  81. public function dispatch($eventName, $event = null): object {
  82. [$event, $eventName] = self::detectEventAndName($event, $eventName);
  83. // type hinting is not possible, due to usage of GenericEvent
  84. if ($event instanceof Event && $eventName === null) {
  85. $this->eventDispatcher->dispatchTyped($event);
  86. return $event;
  87. }
  88. if ($event instanceof Event) {
  89. $this->eventDispatcher->dispatch($eventName, $event);
  90. return $event;
  91. }
  92. if ($event instanceof GenericEvent && get_class($event) === GenericEvent::class) {
  93. $newEvent = new GenericEventWrapper($this->logger, $eventName, $event);
  94. } else {
  95. $newEvent = $event;
  96. // Legacy event
  97. $this->logger->debug(
  98. 'Deprecated event type for {name}: {class}',
  99. ['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null']
  100. );
  101. }
  102. // Event with no payload (object) need special handling
  103. if ($newEvent === null) {
  104. $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName);
  105. return new Event();
  106. }
  107. // Flip the argument order for Symfony to prevent a trigger_error
  108. return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($newEvent, $eventName);
  109. }
  110. /**
  111. * Adds an event listener that listens on the specified events.
  112. *
  113. * @param string $eventName The event to listen on
  114. * @param callable $listener The listener
  115. * @param int $priority The higher this value, the earlier an event
  116. * listener will be triggered in the chain (defaults to 0)
  117. * @deprecated 20.0.0
  118. */
  119. public function addListener($eventName, $listener, $priority = 0) {
  120. if (is_callable($listener)) {
  121. $this->eventDispatcher->addListener($eventName, $listener, $priority);
  122. } else {
  123. // Legacy listener
  124. $this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority);
  125. }
  126. }
  127. /**
  128. * Adds an event subscriber.
  129. *
  130. * The subscriber is asked for all the events it is
  131. * interested in and added as a listener for these events.
  132. * @deprecated 20.0.0
  133. */
  134. public function addSubscriber(EventSubscriberInterface $subscriber) {
  135. $this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber);
  136. }
  137. /**
  138. * Removes an event listener from the specified events.
  139. *
  140. * @param string $eventName The event to remove a listener from
  141. * @param callable $listener The listener to remove
  142. * @deprecated 20.0.0
  143. */
  144. public function removeListener($eventName, $listener) {
  145. $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
  146. }
  147. /**
  148. * @deprecated 20.0.0
  149. */
  150. public function removeSubscriber(EventSubscriberInterface $subscriber) {
  151. $this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber);
  152. }
  153. /**
  154. * Gets the listeners of a specific event or all listeners sorted by descending priority.
  155. *
  156. * @param string|null $eventName The name of the event
  157. *
  158. * @return array The event listeners for the specified event, or all event listeners by event name
  159. * @deprecated 20.0.0
  160. */
  161. public function getListeners($eventName = null) {
  162. return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName);
  163. }
  164. /**
  165. * Gets the listener priority for a specific event.
  166. *
  167. * Returns null if the event or the listener does not exist.
  168. *
  169. * @param string $eventName The name of the event
  170. * @param callable $listener The listener
  171. *
  172. * @return int|null The event listener priority
  173. * @deprecated 20.0.0
  174. */
  175. public function getListenerPriority($eventName, $listener) {
  176. return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener);
  177. }
  178. /**
  179. * Checks whether an event has any registered listeners.
  180. *
  181. * @param string|null $eventName The name of the event
  182. *
  183. * @return bool true if the specified event has any listeners, false otherwise
  184. * @deprecated 20.0.0
  185. */
  186. public function hasListeners($eventName = null) {
  187. return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
  188. }
  189. }