EventDispatcher.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\IContainer;
  34. use OCP\IServerContainer;
  35. use Psr\Log\LoggerInterface;
  36. use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
  37. use function get_class;
  38. class EventDispatcher implements IEventDispatcher {
  39. /** @var SymfonyDispatcher */
  40. private $dispatcher;
  41. /** @var IContainer */
  42. private $container;
  43. /** @var LoggerInterface */
  44. private $logger;
  45. public function __construct(SymfonyDispatcher $dispatcher,
  46. IServerContainer $container,
  47. LoggerInterface $logger) {
  48. $this->dispatcher = $dispatcher;
  49. $this->container = $container;
  50. $this->logger = $logger;
  51. // inject the event dispatcher into the logger
  52. // this is done here because there is a cyclic dependency between the event dispatcher and logger
  53. if ($this->logger instanceof Log || $this->logger instanceof Log\PsrLoggerAdapter) {
  54. $this->logger->setEventDispatcher($this);
  55. }
  56. }
  57. public function addListener(string $eventName,
  58. callable $listener,
  59. int $priority = 0): void {
  60. $this->dispatcher->addListener($eventName, $listener, $priority);
  61. }
  62. public function removeListener(string $eventName,
  63. callable $listener): void {
  64. $this->dispatcher->removeListener($eventName, $listener);
  65. }
  66. public function addServiceListener(string $eventName,
  67. string $className,
  68. int $priority = 0): void {
  69. $listener = new ServiceEventListener(
  70. $this->container,
  71. $className,
  72. $this->logger
  73. );
  74. $this->addListener($eventName, $listener, $priority);
  75. }
  76. /**
  77. * @deprecated
  78. */
  79. public function dispatch(string $eventName,
  80. Event $event): void {
  81. $this->dispatcher->dispatch($event, $eventName);
  82. if ($event instanceof ABroadcastedEvent && !$event->isPropagationStopped()) {
  83. // Propagate broadcast
  84. $this->dispatch(
  85. IBroadcastEvent::class,
  86. new BroadcastEvent($event)
  87. );
  88. }
  89. }
  90. public function dispatchTyped(Event $event): void {
  91. $this->dispatch(get_class($event), $event);
  92. }
  93. /**
  94. * @return SymfonyDispatcher
  95. * @deprecated 20.0.0
  96. */
  97. public function getSymfonyDispatcher(): SymfonyDispatcher {
  98. return $this->dispatcher;
  99. }
  100. }