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 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 15:54:25 +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-05-11 10:27:17 +03:00
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2019-05-28 20:46:36 +03:00
|
|
|
use function is_callable;
|
|
|
|
use OCP\EventDispatcher\Event;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\ILogger;
|
2019-05-28 20:46:36 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
|
2020-07-15 14:57:35 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0 use \OCP\EventDispatcher\IEventDispatcher
|
|
|
|
*/
|
2019-05-28 20:46:36 +03:00
|
|
|
class SymfonyAdapter implements EventDispatcherInterface {
|
|
|
|
|
|
|
|
/** @var EventDispatcher */
|
|
|
|
private $eventDispatcher;
|
2019-10-16 13:36:03 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2019-05-28 20:46:36 +03:00
|
|
|
|
2020-07-15 14:57:35 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2019-10-16 13:36:03 +03:00
|
|
|
public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) {
|
2019-05-28 20:46:36 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2019-10-16 13:36:03 +03:00
|
|
|
$this->logger = $logger;
|
2019-05-28 20:46:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches an event to all registered listeners.
|
|
|
|
*
|
|
|
|
* @param string $eventName The name of the event to dispatch. The name of
|
|
|
|
* the event is the name of the method that is
|
|
|
|
* invoked on listeners.
|
2019-10-16 13:36:03 +03:00
|
|
|
* @param Event|null $event The event to pass to the event handlers/listeners
|
2019-05-28 20:46:36 +03:00
|
|
|
* If not supplied, an empty Event instance is created
|
|
|
|
*
|
2020-12-29 22:43:01 +03:00
|
|
|
* @return object the emitted event
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
2020-12-29 22:43:01 +03:00
|
|
|
public function dispatch($eventName, $event = null): object {
|
2019-10-16 13:36:03 +03:00
|
|
|
// type hinting is not possible, due to usage of GenericEvent
|
2019-05-28 20:46:36 +03:00
|
|
|
if ($event instanceof Event) {
|
|
|
|
$this->eventDispatcher->dispatch($eventName, $event);
|
2020-12-29 22:43:01 +03:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event instanceof GenericEvent && get_class($event) === GenericEvent::class) {
|
|
|
|
$newEvent = new GenericEventWrapper($this->logger, $eventName, $event);
|
2019-05-28 20:46:36 +03:00
|
|
|
} else {
|
2020-12-29 22:43:01 +03:00
|
|
|
$newEvent = $event;
|
2020-05-11 10:27:17 +03:00
|
|
|
|
2020-12-29 22:43:01 +03:00
|
|
|
// Legacy event
|
|
|
|
$this->logger->info(
|
|
|
|
'Deprecated event type for {name}: {class}',
|
|
|
|
['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null']
|
|
|
|
);
|
2019-05-28 20:46:36 +03:00
|
|
|
}
|
2020-12-29 22:43:01 +03:00
|
|
|
|
|
|
|
// Event with no payload (object) need special handling
|
|
|
|
if ($newEvent === null) {
|
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flip the argument order for Symfony to prevent a trigger_error
|
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($newEvent, $eventName);
|
2019-05-28 20:46:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an event listener that listens on the specified events.
|
|
|
|
*
|
|
|
|
* @param string $eventName The event to listen on
|
|
|
|
* @param callable $listener The listener
|
|
|
|
* @param int $priority The higher this value, the earlier an event
|
|
|
|
* listener will be triggered in the chain (defaults to 0)
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function addListener($eventName, $listener, $priority = 0) {
|
|
|
|
if (is_callable($listener)) {
|
|
|
|
$this->eventDispatcher->addListener($eventName, $listener, $priority);
|
|
|
|
} else {
|
|
|
|
// Legacy listener
|
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an event subscriber.
|
|
|
|
*
|
|
|
|
* The subscriber is asked for all the events it is
|
|
|
|
* interested in and added as a listener for these events.
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) {
|
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an event listener from the specified events.
|
|
|
|
*
|
|
|
|
* @param string $eventName The event to remove a listener from
|
|
|
|
* @param callable $listener The listener to remove
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function removeListener($eventName, $listener) {
|
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
|
|
|
|
}
|
|
|
|
|
2020-07-15 14:57:35 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2019-05-28 20:46:36 +03:00
|
|
|
public function removeSubscriber(EventSubscriberInterface $subscriber) {
|
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the listeners of a specific event or all listeners sorted by descending priority.
|
|
|
|
*
|
|
|
|
* @param string|null $eventName The name of the event
|
|
|
|
*
|
|
|
|
* @return array The event listeners for the specified event, or all event listeners by event name
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function getListeners($eventName = null) {
|
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the listener priority for a specific event.
|
|
|
|
*
|
|
|
|
* Returns null if the event or the listener does not exist.
|
|
|
|
*
|
|
|
|
* @param string $eventName The name of the event
|
|
|
|
* @param callable $listener The listener
|
|
|
|
*
|
|
|
|
* @return int|null The event listener priority
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function getListenerPriority($eventName, $listener) {
|
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether an event has any registered listeners.
|
|
|
|
*
|
|
|
|
* @param string|null $eventName The name of the event
|
|
|
|
*
|
|
|
|
* @return bool true if the specified event has any listeners, false otherwise
|
2020-07-15 14:57:35 +03:00
|
|
|
* @deprecated 20.0.0
|
2019-05-28 20:46:36 +03:00
|
|
|
*/
|
|
|
|
public function hasListeners($eventName = null) {
|
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
|
|
|
|
}
|
|
|
|
}
|