EventDispatcher.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  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 OC\Broadcast\Events\BroadcastEvent;
  28. use OC\Log;
  29. use OCP\Broadcast\Events\IBroadcastEvent;
  30. use OCP\EventDispatcher\ABroadcastedEvent;
  31. use OCP\EventDispatcher\Event;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\IServerContainer;
  34. use Psr\Log\LoggerInterface;
  35. use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
  36. use function get_class;
  37. class EventDispatcher implements IEventDispatcher {
  38. public function __construct(
  39. private SymfonyDispatcher $dispatcher,
  40. private IServerContainer $container,
  41. private LoggerInterface $logger,
  42. ) {
  43. // inject the event dispatcher into the logger
  44. // this is done here because there is a cyclic dependency between the event dispatcher and logger
  45. if ($this->logger instanceof Log || $this->logger instanceof Log\PsrLoggerAdapter) {
  46. $this->logger->setEventDispatcher($this);
  47. }
  48. }
  49. public function addListener(string $eventName,
  50. callable $listener,
  51. int $priority = 0): void {
  52. $this->dispatcher->addListener($eventName, $listener, $priority);
  53. }
  54. public function removeListener(string $eventName,
  55. callable $listener): void {
  56. $this->dispatcher->removeListener($eventName, $listener);
  57. }
  58. public function addServiceListener(string $eventName,
  59. string $className,
  60. int $priority = 0): void {
  61. $listener = new ServiceEventListener(
  62. $this->container,
  63. $className,
  64. $this->logger
  65. );
  66. $this->addListener($eventName, $listener, $priority);
  67. }
  68. public function hasListeners(string $eventName): bool {
  69. return $this->dispatcher->hasListeners($eventName);
  70. }
  71. /**
  72. * @deprecated
  73. */
  74. public function dispatch(string $eventName,
  75. Event $event): void {
  76. $this->dispatcher->dispatch($event, $eventName);
  77. if ($event instanceof ABroadcastedEvent && !$event->isPropagationStopped()) {
  78. // Propagate broadcast
  79. $this->dispatch(
  80. IBroadcastEvent::class,
  81. new BroadcastEvent($event)
  82. );
  83. }
  84. }
  85. public function dispatchTyped(Event $event): void {
  86. $this->dispatch(get_class($event), $event);
  87. }
  88. /**
  89. * @return SymfonyDispatcher
  90. * @deprecated 20.0.0
  91. */
  92. public function getSymfonyDispatcher(): SymfonyDispatcher {
  93. return $this->dispatcher;
  94. }
  95. }