1
0

EventDispatcher.php 3.0 KB

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