You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9 KiB

3 years ago
  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 function get_class;
  28. use OC\Broadcast\Events\BroadcastEvent;
  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\ILogger;
  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 ILogger */
  43. private $logger;
  44. public function __construct(SymfonyDispatcher $dispatcher,
  45. IServerContainer $container,
  46. ILogger $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. public function dispatch(string $eventName,
  71. Event $event): void {
  72. $this->dispatcher->dispatch($event, $eventName);
  73. if ($event instanceof ABroadcastedEvent && !$event->isPropagationStopped()) {
  74. // Propagate broadcast
  75. $this->dispatch(
  76. IBroadcastEvent::class,
  77. new BroadcastEvent($event)
  78. );
  79. }
  80. }
  81. public function dispatchTyped(Event $event): void {
  82. $this->dispatch(get_class($event), $event);
  83. }
  84. /**
  85. * @return SymfonyDispatcher
  86. * @deprecated 20.0.0
  87. */
  88. public function getSymfonyDispatcher(): SymfonyDispatcher {
  89. return $this->dispatcher;
  90. }
  91. }