2019-05-28 20:46:36 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-05-28 20:46:36 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\EventDispatcher;
|
|
|
|
|
2020-10-08 17:48:03 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-11-22 22:52:10 +03:00
|
|
|
use function get_class;
|
2019-09-16 19:09:43 +03:00
|
|
|
use OC\Broadcast\Events\BroadcastEvent;
|
|
|
|
use OCP\Broadcast\Events\IBroadcastEvent;
|
|
|
|
use OCP\EventDispatcher\ABroadcastedEvent;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\EventDispatcher\Event;
|
2019-05-28 20:46:36 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
|
|
|
use OCP\IContainer;
|
|
|
|
use OCP\IServerContainer;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
|
|
|
|
|
|
|
|
class EventDispatcher implements IEventDispatcher {
|
|
|
|
|
|
|
|
/** @var SymfonyDispatcher */
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
/** @var IContainer */
|
|
|
|
private $container;
|
|
|
|
|
2020-10-08 17:48:03 +03:00
|
|
|
/** @var LoggerInterface */
|
2019-05-28 20:46:36 +03:00
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct(SymfonyDispatcher $dispatcher,
|
|
|
|
IServerContainer $container,
|
2020-10-08 17:48:03 +03:00
|
|
|
LoggerInterface $logger) {
|
2019-05-28 20:46:36 +03:00
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$this->container = $container;
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addListener(string $eventName,
|
|
|
|
callable $listener,
|
|
|
|
int $priority = 0): void {
|
|
|
|
$this->dispatcher->addListener($eventName, $listener, $priority);
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:01:29 +03:00
|
|
|
public function removeListener(string $eventName,
|
|
|
|
callable $listener): void {
|
|
|
|
$this->dispatcher->removeListener($eventName, $listener);
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:46:36 +03:00
|
|
|
public function addServiceListener(string $eventName,
|
|
|
|
string $className,
|
|
|
|
int $priority = 0): void {
|
|
|
|
$listener = new ServiceEventListener(
|
|
|
|
$this->container,
|
|
|
|
$className,
|
|
|
|
$this->logger
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addListener($eventName, $listener, $priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dispatch(string $eventName,
|
|
|
|
Event $event): void {
|
2019-09-10 12:26:55 +03:00
|
|
|
$this->dispatcher->dispatch($event, $eventName);
|
2019-09-16 19:09:43 +03:00
|
|
|
|
|
|
|
if ($event instanceof ABroadcastedEvent && !$event->isPropagationStopped()) {
|
|
|
|
// Propagate broadcast
|
|
|
|
$this->dispatch(
|
|
|
|
IBroadcastEvent::class,
|
|
|
|
new BroadcastEvent($event)
|
|
|
|
);
|
|
|
|
}
|
2019-05-28 20:46:36 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 21:02:50 +03:00
|
|
|
public function dispatchTyped(Event $event): void {
|
|
|
|
$this->dispatch(get_class($event), $event);
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:46:36 +03:00
|
|
|
/**
|
|
|
|
* @return SymfonyDispatcher
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function getSymfonyDispatcher(): SymfonyDispatcher {
|
|
|
|
return $this->dispatcher;
|
|
|
|
}
|
|
|
|
}
|