summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2019-05-28 19:46:36 +0200
committerChristoph Wurst <christoph@winzerhof-wurst.at>2019-06-25 10:02:27 +0200
commit3174012adf3fde4de74efa12cfa7e480b874b295 (patch)
treebc58b99dc45b5adcdeaf30269e9a6eab2c853773 /lib/private
parent817bdc47c804ae8511ad3423eae216f6ccdee6c6 (diff)
downloadnextcloud-server-3174012adf3fde4de74efa12cfa7e480b874b295.tar.gz
nextcloud-server-3174012adf3fde4de74efa12cfa7e480b874b295.zip
Add event dispatcher to OCP
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/DB/Migrator.php8
-rw-r--r--lib/private/EventDispatcher/EventDispatcher.php85
-rw-r--r--lib/private/EventDispatcher/ServiceEventListener.php78
-rw-r--r--lib/private/EventDispatcher/SymfonyAdapter.php140
-rw-r--r--lib/private/Server.php12
-rw-r--r--lib/private/Share20/LegacyHooks.php11
-rw-r--r--lib/private/Share20/Manager.php8
-rw-r--r--lib/private/User/Database.php6
8 files changed, 325 insertions, 23 deletions
diff --git a/lib/private/DB/Migrator.php b/lib/private/DB/Migrator.php
index 603f9259d6e..a853ab1ea1b 100644
--- a/lib/private/DB/Migrator.php
+++ b/lib/private/DB/Migrator.php
@@ -39,7 +39,7 @@ use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
-use Symfony\Component\EventDispatcher\EventDispatcher;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class Migrator {
@@ -53,7 +53,7 @@ class Migrator {
/** @var IConfig */
protected $config;
- /** @var EventDispatcher */
+ /** @var EventDispatcherInterface */
private $dispatcher;
/** @var bool */
@@ -63,12 +63,12 @@ class Migrator {
* @param \Doctrine\DBAL\Connection|Connection $connection
* @param ISecureRandom $random
* @param IConfig $config
- * @param EventDispatcher $dispatcher
+ * @param EventDispatcherInterface $dispatcher
*/
public function __construct(\Doctrine\DBAL\Connection $connection,
ISecureRandom $random,
IConfig $config,
- EventDispatcher $dispatcher = null) {
+ EventDispatcherInterface $dispatcher = null) {
$this->connection = $connection;
$this->random = $random;
$this->config = $config;
diff --git a/lib/private/EventDispatcher/EventDispatcher.php b/lib/private/EventDispatcher/EventDispatcher.php
new file mode 100644
index 00000000000..8db2f3101be
--- /dev/null
+++ b/lib/private/EventDispatcher/EventDispatcher.php
@@ -0,0 +1,85 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OC\EventDispatcher;
+
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\IContainer;
+use OCP\ILogger;
+use OCP\IServerContainer;
+use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
+
+class EventDispatcher implements IEventDispatcher {
+
+ /** @var SymfonyDispatcher */
+ private $dispatcher;
+
+ /** @var IContainer */
+ private $container;
+
+ /** @var ILogger */
+ private $logger;
+
+ public function __construct(SymfonyDispatcher $dispatcher,
+ IServerContainer $container,
+ ILogger $logger) {
+ $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);
+ }
+
+ 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 {
+
+ $this->dispatcher->dispatch($eventName, $event);
+ }
+
+ /**
+ * @return SymfonyDispatcher
+ */
+ public function getSymfonyDispatcher(): SymfonyDispatcher {
+ return $this->dispatcher;
+ }
+
+}
diff --git a/lib/private/EventDispatcher/ServiceEventListener.php b/lib/private/EventDispatcher/ServiceEventListener.php
new file mode 100644
index 00000000000..f5b6cd9b79d
--- /dev/null
+++ b/lib/private/EventDispatcher/ServiceEventListener.php
@@ -0,0 +1,78 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OC\EventDispatcher;
+
+use OCP\AppFramework\QueryException;
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use OCP\IContainer;
+use OCP\ILogger;
+
+/**
+ * Lazy service event listener
+ *
+ * Makes it possible to lazy-route a dispatched event to a service instance
+ * created by the service container
+ */
+final class ServiceEventListener {
+
+ /** @var IContainer */
+ private $container;
+
+ /** @var string */
+ private $class;
+
+ /** @var ILogger */
+ private $logger;
+
+ /** @var null|IEventListener */
+ private $service;
+
+ public function __construct(IContainer $container,
+ string $class,
+ ILogger $logger) {
+ $this->container = $container;
+ $this->class = $class;
+ $this->logger = $logger;
+ }
+
+ public function __invoke(Event $event) {
+ if ($this->service === null) {
+ try {
+ $this->service = $this->container->query($this->class);
+ } catch (QueryException $e) {
+ $this->logger->logException($e, [
+ 'level' => ILogger::ERROR,
+ 'message' => "Could not load event listener service " . $this->class,
+ ]);
+ return;
+ }
+ }
+
+ $this->service->handle($event);
+ }
+
+}
diff --git a/lib/private/EventDispatcher/SymfonyAdapter.php b/lib/private/EventDispatcher/SymfonyAdapter.php
new file mode 100644
index 00000000000..f2f2fbf59fd
--- /dev/null
+++ b/lib/private/EventDispatcher/SymfonyAdapter.php
@@ -0,0 +1,140 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OC\EventDispatcher;
+
+use function is_callable;
+use OCP\EventDispatcher\Event;
+use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class SymfonyAdapter implements EventDispatcherInterface {
+
+ /** @var EventDispatcher */
+ private $eventDispatcher;
+
+ public function __construct(EventDispatcher $eventDispatcher) {
+ $this->eventDispatcher = $eventDispatcher;
+ }
+
+ /**
+ * 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.
+ * @param SymfonyEvent|null $event The event to pass to the event handlers/listeners
+ * If not supplied, an empty Event instance is created
+ *
+ * @return SymfonyEvent
+ */
+ public function dispatch($eventName, SymfonyEvent $event = null) {
+ if ($event instanceof Event) {
+ $this->eventDispatcher->dispatch($eventName, $event);
+ } else {
+ // Legacy event
+ $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $event);
+ }
+ }
+
+ /**
+ * 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)
+ */
+ 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.
+ */
+ 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
+ */
+ public function removeListener($eventName, $listener) {
+ $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
+ }
+
+ 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
+ */
+ 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
+ */
+ 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
+ */
+ public function hasListeners($eventName = null) {
+ return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
+ }
+
+}
diff --git a/lib/private/Server.php b/lib/private/Server.php
index 408b457ec39..2b08b0bab15 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -128,7 +128,6 @@ use OCA\Theming\ThemingDefaults;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
-use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Collaboration\AutoComplete\IManager;
use OCP\Contacts\ContactsMenu\IContactsStore;
use OCP\Dashboard\IDashboardManager;
@@ -158,7 +157,6 @@ use OCP\Remote\IInstanceFactory;
use OCP\RichObjectStrings\IValidator;
use OCP\Security\IContentSecurityPolicyManager;
use OCP\Share\IShareHelper;
-use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -989,11 +987,9 @@ class Server extends ServerContainer implements IServerContainer {
$c->getLogger()
);
});
- $this->registerService(EventDispatcher::class, function () {
- return new EventDispatcher();
- });
- $this->registerAlias('EventDispatcher', EventDispatcher::class);
- $this->registerAlias(EventDispatcherInterface::class, EventDispatcher::class);
+ $this->registerAlias(\OCP\EventDispatcher\IEventDispatcher::class, \OC\EventDispatcher\EventDispatcher::class);
+ $this->registerAlias('EventDispatcher', \OC\EventDispatcher\SymfonyAdapter::class);
+ $this->registerAlias(EventDispatcherInterface::class, \OC\EventDispatcher\SymfonyAdapter::class);
$this->registerService('CryptoWrapper', function (Server $c) {
// FIXME: Instantiiated here due to cyclic dependency
@@ -1858,7 +1854,7 @@ class Server extends ServerContainer implements IServerContainer {
* @since 8.2.0
*/
public function getEventDispatcher() {
- return $this->query('EventDispatcher');
+ return $this->query(\OC\EventDispatcher\SymfonyAdapter::class);
}
/**
diff --git a/lib/private/Share20/LegacyHooks.php b/lib/private/Share20/LegacyHooks.php
index c3a4e9d999d..4cc748aa418 100644
--- a/lib/private/Share20/LegacyHooks.php
+++ b/lib/private/Share20/LegacyHooks.php
@@ -1,4 +1,5 @@
<?php
+
/**
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
*
@@ -21,24 +22,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
+
namespace OC\Share20;
use OCP\Files\File;
use OCP\Share\IShare;
-use Symfony\Component\EventDispatcher\EventDispatcher;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\Share;
class LegacyHooks {
- /** @var EventDispatcher */
+
+ /** @var EventDispatcherInterface */
private $eventDispatcher;
/**
* LegacyHooks constructor.
*
- * @param EventDispatcher $eventDispatcher
+ * @param EventDispatcherInterface $eventDispatcher
*/
- public function __construct(EventDispatcher $eventDispatcher) {
+ public function __construct(EventDispatcherInterface $eventDispatcher) {
$this->eventDispatcher = $eventDispatcher;
$this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php
index c2ea165955f..e71b6f98321 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -62,7 +62,7 @@ use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
-use Symfony\Component\EventDispatcher\EventDispatcher;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\Share\IShareProvider;
use OCP\Share;
@@ -96,7 +96,7 @@ class Manager implements IManager {
private $rootFolder;
/** @var CappedMemoryCache */
private $sharingDisabledForUsersCache;
- /** @var EventDispatcher */
+ /** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var LegacyHooks */
private $legacyHooks;
@@ -122,7 +122,7 @@ class Manager implements IManager {
* @param IProviderFactory $factory
* @param IUserManager $userManager
* @param IRootFolder $rootFolder
- * @param EventDispatcher $eventDispatcher
+ * @param EventDispatcherInterface $eventDispatcher
* @param IMailer $mailer
* @param IURLGenerator $urlGenerator
* @param \OC_Defaults $defaults
@@ -139,7 +139,7 @@ class Manager implements IManager {
IProviderFactory $factory,
IUserManager $userManager,
IRootFolder $rootFolder,
- EventDispatcher $eventDispatcher,
+ EventDispatcherInterface $eventDispatcher,
IMailer $mailer,
IURLGenerator $urlGenerator,
\OC_Defaults $defaults
diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php
index 27dcb2fc331..8c00060e196 100644
--- a/lib/private/User/Database.php
+++ b/lib/private/User/Database.php
@@ -67,7 +67,7 @@ use OCP\User\Backend\IGetDisplayNameBackend;
use OCP\User\Backend\IGetHomeBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
-use Symfony\Component\EventDispatcher\EventDispatcher;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
@@ -84,7 +84,7 @@ class Database extends ABackend
/** @var CappedMemoryCache */
private $cache;
- /** @var EventDispatcher */
+ /** @var EventDispatcherInterface */
private $eventDispatcher;
/** @var IDBConnection */
@@ -96,7 +96,7 @@ class Database extends ABackend
/**
* \OC\User\Database constructor.
*
- * @param EventDispatcher $eventDispatcher
+ * @param EventDispatcherInterface $eventDispatcher
* @param string $table
*/
public function __construct($eventDispatcher = null, $table = 'users') {