From 3174012adf3fde4de74efa12cfa7e480b874b295 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Tue, 28 May 2019 19:46:36 +0200 Subject: [PATCH] Add event dispatcher to OCP Signed-off-by: Christoph Wurst --- apps/dav/lib/HookManager.php | 6 +- apps/dav/tests/unit/CardDAV/ConverterTest.php | 14 +- apps/dav/tests/unit/DAV/HookManagerTest.php | 6 +- apps/files_trashbin/lib/Storage.php | 8 +- apps/files_trashbin/tests/StorageTest.php | 5 +- .../composer/composer/autoload_classmap.php | 1 - .../composer/composer/autoload_static.php | 1 - .../lib/AppInfo/Application.php | 36 +---- .../lib/Event/CodesGenerated.php | 3 +- .../lib/Listener/ActivityPublisher.php | 10 +- .../lib/Listener/ClearNotifications.php | 9 +- .../lib/Listener/ProviderDisabled.php | 10 +- .../lib/Listener/ProviderEnabled.php | 7 +- .../lib/Listener/RegistryUpdater.php | 8 +- .../lib/Service/BackupCodeStorage.php | 5 +- .../Unit/Listener/ActivityPublisherTest.php | 6 +- .../Unit/Listener/ClearNotificationsTest.php | 2 +- .../Unit/Listener/ProviderDisabledTest.php | 2 +- .../Unit/Listener/ProviderEnabledTest.php | 2 +- .../Unit/Listener/RegistryUpdaterTest.php | 2 +- .../Unit/Service/BackupCodeStorageTest.php | 6 +- lib/composer/composer/autoload_classmap.php | 6 + lib/composer/composer/autoload_static.php | 6 + lib/private/DB/Migrator.php | 8 +- .../EventDispatcher/EventDispatcher.php | 85 +++++++++++ .../EventDispatcher/ServiceEventListener.php | 78 ++++++++++ .../EventDispatcher/SymfonyAdapter.php | 140 ++++++++++++++++++ lib/private/Server.php | 12 +- lib/private/Share20/LegacyHooks.php | 11 +- lib/private/Share20/Manager.php | 8 +- lib/private/User/Database.php | 6 +- .../TwoFactorAuth/RegistryEvent.php | 3 +- lib/public/EventDispatcher/Event.php | 40 +++++ .../EventDispatcher/IEventDispatcher.php | 61 ++++++++ .../public/EventDispatcher/IEventListener.php | 21 ++- .../Controller/CheckSetupControllerTest.php | 6 +- tests/lib/Share20/ManagerTest.php | 7 +- 37 files changed, 524 insertions(+), 123 deletions(-) create mode 100644 lib/private/EventDispatcher/EventDispatcher.php create mode 100644 lib/private/EventDispatcher/ServiceEventListener.php create mode 100644 lib/private/EventDispatcher/SymfonyAdapter.php create mode 100644 lib/public/EventDispatcher/Event.php create mode 100644 lib/public/EventDispatcher/IEventDispatcher.php rename apps/twofactor_backupcodes/lib/Listener/IListener.php => lib/public/EventDispatcher/IEventListener.php (71%) diff --git a/apps/dav/lib/HookManager.php b/apps/dav/lib/HookManager.php index 27b6525be4..272c4cd1ad 100644 --- a/apps/dav/lib/HookManager.php +++ b/apps/dav/lib/HookManager.php @@ -32,7 +32,7 @@ use OCA\DAV\CardDAV\SyncService; use OCP\IUser; use OCP\IUserManager; use OCP\Util; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class HookManager { @@ -57,14 +57,14 @@ class HookManager { /** @var array */ private $addressBooksToDelete = []; - /** @var EventDispatcher */ + /** @var EventDispatcherInterface */ private $eventDispatcher; public function __construct(IUserManager $userManager, SyncService $syncService, CalDavBackend $calDav, CardDavBackend $cardDav, - EventDispatcher $eventDispatcher) { + EventDispatcherInterface $eventDispatcher) { $this->userManager = $userManager; $this->syncService = $syncService; $this->calDav = $calDav; diff --git a/apps/dav/tests/unit/CardDAV/ConverterTest.php b/apps/dav/tests/unit/CardDAV/ConverterTest.php index 757d69659e..dbb035a33d 100644 --- a/apps/dav/tests/unit/CardDAV/ConverterTest.php +++ b/apps/dav/tests/unit/CardDAV/ConverterTest.php @@ -32,7 +32,6 @@ use OCP\IDBConnection; use OCP\IImage; use OCP\IUser; use PHPUnit_Framework_MockObject_MockObject; -use Symfony\Component\EventDispatcher\EventDispatcher; use Test\TestCase; class ConverterTest extends TestCase { @@ -40,19 +39,10 @@ class ConverterTest extends TestCase { /** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */ private $accountManager; - /** @var EventDispatcher | PHPUnit_Framework_MockObject_MockObject */ - private $eventDispatcher; - - /** @var IDBConnection | PHPUnit_Framework_MockObject_MockObject */ - private $databaseConnection; - public function setUp() { parent::setUp(); - $this->databaseConnection = $this->getMockBuilder(IDBConnection::class)->getMock(); - $this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class) - ->disableOriginalConstructor()->getMock(); - $this->accountManager = $this->getMockBuilder(AccountManager::class) - ->disableOriginalConstructor()->getMock(); + + $this->accountManager = $this->createMock(AccountManager::class); } public function getAccountManager(IUser $user) { diff --git a/apps/dav/tests/unit/DAV/HookManagerTest.php b/apps/dav/tests/unit/DAV/HookManagerTest.php index 25219b7a8f..75baf8257d 100644 --- a/apps/dav/tests/unit/DAV/HookManagerTest.php +++ b/apps/dav/tests/unit/DAV/HookManagerTest.php @@ -36,19 +36,19 @@ use OCA\DAV\HookManager; use OCP\IL10N; use OCP\IUser; use OCP\IUserManager; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class HookManagerTest extends TestCase { /** @var IL10N */ private $l10n; - /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */ + /** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */ private $eventDispatcher; public function setUp() { parent::setUp(); - $this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class)->disableOriginalConstructor()->getMock(); + $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->l10n = $this->createMock(IL10N::class); $this->l10n ->expects($this->any()) diff --git a/apps/files_trashbin/lib/Storage.php b/apps/files_trashbin/lib/Storage.php index 0db634eeb9..04105dda6c 100644 --- a/apps/files_trashbin/lib/Storage.php +++ b/apps/files_trashbin/lib/Storage.php @@ -38,7 +38,7 @@ use OCP\Files\Mount\IMountPoint; use OCP\Files\Node; use OCP\ILogger; use OCP\IUserManager; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Storage extends Wrapper { /** @var IMountPoint */ @@ -50,7 +50,7 @@ class Storage extends Wrapper { /** @var ILogger */ private $logger; - /** @var EventDispatcher */ + /** @var EventDispatcherInterface */ private $eventDispatcher; /** @var IRootFolder */ @@ -66,7 +66,7 @@ class Storage extends Wrapper { * @param ITrashManager $trashManager * @param IUserManager|null $userManager * @param ILogger|null $logger - * @param EventDispatcher|null $eventDispatcher + * @param EventDispatcherInterface|null $eventDispatcher * @param IRootFolder|null $rootFolder */ public function __construct( @@ -74,7 +74,7 @@ class Storage extends Wrapper { ITrashManager $trashManager = null, IUserManager $userManager = null, ILogger $logger = null, - EventDispatcher $eventDispatcher = null, + EventDispatcherInterface $eventDispatcher = null, IRootFolder $rootFolder = null ) { $this->mountPoint = $parameters['mountPoint']; diff --git a/apps/files_trashbin/tests/StorageTest.php b/apps/files_trashbin/tests/StorageTest.php index 058d64d1aa..eff33f9b30 100644 --- a/apps/files_trashbin/tests/StorageTest.php +++ b/apps/files_trashbin/tests/StorageTest.php @@ -40,7 +40,7 @@ use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\ILogger; use OCP\IUserManager; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class Storage @@ -543,8 +543,7 @@ class StorageTest extends \Test\TestCase { $userManager->expects($this->any()) ->method('userExists')->willReturn($userExists); $logger = $this->getMockBuilder(ILogger::class)->getMock(); - $eventDispatcher = $this->getMockBuilder(EventDispatcher::class) - ->disableOriginalConstructor()->getMock(); + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); $rootFolder = $this->createMock(IRootFolder::class); $node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock(); $trashManager = $this->createMock(ITrashManager::class); diff --git a/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php b/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php index 2038421a3a..cf5eae2a5d 100644 --- a/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php +++ b/apps/twofactor_backupcodes/composer/composer/autoload_classmap.php @@ -16,7 +16,6 @@ return array( 'OCA\\TwoFactorBackupCodes\\Event\\CodesGenerated' => $baseDir . '/../lib/Event/CodesGenerated.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ActivityPublisher' => $baseDir . '/../lib/Listener/ActivityPublisher.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ClearNotifications' => $baseDir . '/../lib/Listener/ClearNotifications.php', - 'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => $baseDir . '/../lib/Listener/IListener.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderDisabled' => $baseDir . '/../lib/Listener/ProviderDisabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => $baseDir . '/../lib/Listener/ProviderEnabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => $baseDir . '/../lib/Listener/RegistryUpdater.php', diff --git a/apps/twofactor_backupcodes/composer/composer/autoload_static.php b/apps/twofactor_backupcodes/composer/composer/autoload_static.php index d1f124a407..3dc2127452 100644 --- a/apps/twofactor_backupcodes/composer/composer/autoload_static.php +++ b/apps/twofactor_backupcodes/composer/composer/autoload_static.php @@ -31,7 +31,6 @@ class ComposerStaticInitTwoFactorBackupCodes 'OCA\\TwoFactorBackupCodes\\Event\\CodesGenerated' => __DIR__ . '/..' . '/../lib/Event/CodesGenerated.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ActivityPublisher' => __DIR__ . '/..' . '/../lib/Listener/ActivityPublisher.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ClearNotifications' => __DIR__ . '/..' . '/../lib/Listener/ClearNotifications.php', - 'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => __DIR__ . '/..' . '/../lib/Listener/IListener.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderDisabled' => __DIR__ . '/..' . '/../lib/Listener/ProviderDisabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => __DIR__ . '/..' . '/../lib/Listener/ProviderEnabled.php', 'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => __DIR__ . '/..' . '/../lib/Listener/RegistryUpdater.php', diff --git a/apps/twofactor_backupcodes/lib/AppInfo/Application.php b/apps/twofactor_backupcodes/lib/AppInfo/Application.php index fc6c94d5b7..041af03706 100644 --- a/apps/twofactor_backupcodes/lib/AppInfo/Application.php +++ b/apps/twofactor_backupcodes/lib/AppInfo/Application.php @@ -29,18 +29,16 @@ use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; -use OCA\TwoFactorBackupCodes\Listener\IListener; use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled; use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled; use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; use OCA\TwoFactorBackupCodes\Notifications\Notifier; use OCP\AppFramework\App; use OCP\Authentication\TwoFactorAuth\IRegistry; -use OCP\Authentication\TwoFactorAuth\RegistryEvent; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IL10N; use OCP\Notification\IManager; use OCP\Util; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Application extends App { public function __construct() { @@ -62,32 +60,14 @@ class Application extends App { Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser'); $container = $this->getContainer(); - /** @var EventDispatcherInterface $eventDispatcher */ - $eventDispatcher = $container->query(EventDispatcherInterface::class); - $eventDispatcher->addListener(CodesGenerated::class, function (CodesGenerated $event) use ($container) { - /** @var IListener[] $listeners */ - $listeners = [ - $container->query(ActivityPublisher::class), - $container->query(RegistryUpdater::class), - $container->query(ClearNotifications::class), - ]; - foreach ($listeners as $listener) { - $listener->handle($event); - } - }); - - $eventDispatcher->addListener(IRegistry::EVENT_PROVIDER_ENABLED, function(RegistryEvent $event) use ($container) { - /** @var IListener $listener */ - $listener = $container->query(ProviderEnabled::class); - $listener->handle($event); - }); - - $eventDispatcher->addListener(IRegistry::EVENT_PROVIDER_DISABLED, function(RegistryEvent $event) use ($container) { - /** @var IListener $listener */ - $listener = $container->query(ProviderDisabled::class); - $listener->handle($event); - }); + /** @var IEventDispatcher $eventDispatcher */ + $eventDispatcher = $container->query(IEventDispatcher::class); + $eventDispatcher->addServiceListener(CodesGenerated::class, ActivityPublisher::class); + $eventDispatcher->addServiceListener(CodesGenerated::class, RegistryUpdater::class); + $eventDispatcher->addServiceListener(CodesGenerated::class, ClearNotifications::class); + $eventDispatcher->addServiceListener(IRegistry::EVENT_PROVIDER_ENABLED, ProviderEnabled::class); + $eventDispatcher->addServiceListener(IRegistry::EVENT_PROVIDER_DISABLED, ProviderDisabled::class); } public function registerNotification() { diff --git a/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php b/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php index c4ffcb3760..1724053281 100644 --- a/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php +++ b/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php @@ -24,8 +24,8 @@ declare(strict_types=1); namespace OCA\TwoFactorBackupCodes\Event; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; class CodesGenerated extends Event { @@ -33,6 +33,7 @@ class CodesGenerated extends Event { private $user; public function __construct(IUser $user) { + parent::__construct(); $this->user = $user; } diff --git a/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php b/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php index 31e2ac6e50..75a5937b5f 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php +++ b/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php @@ -27,10 +27,11 @@ namespace OCA\TwoFactorBackupCodes\Listener; use BadMethodCallException; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCP\Activity\IManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; use OCP\ILogger; -use Symfony\Component\EventDispatcher\Event; -class ActivityPublisher implements IListener { +class ActivityPublisher implements IEventListener { /** @var IManager */ private $activityManager; @@ -38,7 +39,8 @@ class ActivityPublisher implements IListener { /** @var ILogger */ private $logger; - public function __construct(IManager $activityManager, ILogger $logger) { + public function __construct(IManager $activityManager, + ILogger $logger) { $this->activityManager = $activityManager; $this->logger = $logger; } @@ -46,7 +48,7 @@ class ActivityPublisher implements IListener { /** * Push an event to the user's activity stream */ - public function handle(Event $event) { + public function handle(Event $event): void { if ($event instanceof CodesGenerated) { $activity = $this->activityManager->generateEvent(); $activity->setApp('twofactor_backupcodes') diff --git a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php index ad7fd188eb..eb0f7363aa 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php +++ b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php @@ -1,5 +1,7 @@ * @@ -25,10 +27,11 @@ declare(strict_types=1); namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; use OCP\Notification\IManager; -use Symfony\Component\EventDispatcher\Event; -class ClearNotifications implements IListener { +class ClearNotifications implements IEventListener { /** @var IManager */ private $manager; @@ -37,7 +40,7 @@ class ClearNotifications implements IListener { $this->manager = $manager; } - public function handle(Event $event) { + public function handle(Event $event): void { if (!($event instanceof CodesGenerated)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php b/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php index 835eb0394f..bf2899f391 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php +++ b/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php @@ -1,5 +1,7 @@ * @@ -22,16 +24,16 @@ declare(strict_types=1); * */ - namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; -use Symfony\Component\EventDispatcher\Event; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -class ProviderDisabled implements IListener { +class ProviderDisabled implements IEventListener { /** @var IRegistry */ private $registry; @@ -45,7 +47,7 @@ class ProviderDisabled implements IListener { $this->jobList = $jobList; } - public function handle(Event $event) { + public function handle(Event $event): void { if (!($event instanceof RegistryEvent)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php b/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php index 48cbef66f1..7c25821338 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php +++ b/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php @@ -28,9 +28,10 @@ use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; -use Symfony\Component\EventDispatcher\Event; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -class ProviderEnabled implements IListener { +class ProviderEnabled implements IEventListener { /** @var IRegistry */ private $registry; @@ -44,7 +45,7 @@ class ProviderEnabled implements IListener { $this->jobList = $jobList; } - public function handle(Event $event) { + public function handle(Event $event): void { if (!($event instanceof RegistryEvent)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php b/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php index 95b0db090e..93e4959b24 100644 --- a/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php +++ b/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php @@ -27,9 +27,10 @@ namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider; use OCP\Authentication\TwoFactorAuth\IRegistry; -use Symfony\Component\EventDispatcher\Event; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -class RegistryUpdater implements IListener { +class RegistryUpdater implements IEventListener { /** @var IRegistry */ private $registry; @@ -42,9 +43,10 @@ class RegistryUpdater implements IListener { $this->provider = $provider; } - public function handle(Event $event) { + public function handle(Event $event): void { if ($event instanceof CodesGenerated) { $this->registry->enableProviderFor($this->provider, $event->getUser()); } } + } diff --git a/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php b/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php index 74a032dcc0..88117c37ec 100644 --- a/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php +++ b/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php @@ -27,6 +27,7 @@ use OCA\TwoFactorBackupCodes\Db\BackupCode; use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCP\Activity\IManager; +use OCP\EventDispatcher\IEventDispatcher; use OCP\ILogger; use OCP\IUser; use OCP\Security\IHasher; @@ -46,13 +47,13 @@ class BackupCodeStorage { /** @var ISecureRandom */ private $random; - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ private $eventDispatcher; public function __construct(BackupCodeMapper $mapper, ISecureRandom $random, IHasher $hasher, - EventDispatcherInterface $eventDispatcher) { + IEventDispatcher $eventDispatcher) { $this->mapper = $mapper; $this->hasher = $hasher; $this->random = $random; diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php index a6c38701a3..44ea88cdb0 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ActivityPublisherTest.php @@ -28,15 +28,15 @@ use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; use OCP\Activity\IEvent; use OCP\Activity\IManager; +use OCP\EventDispatcher\Event; use OCP\ILogger; use OCP\IUser; -use PHPUnit_Framework_MockObject_MockObject; -use Symfony\Component\EventDispatcher\Event; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class ActivityPublisherTest extends TestCase { - /** @var IManager|PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|MockObject */ private $activityManager; /** @var ILogger */ diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php index 123c008cbb..21dde19d32 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ClearNotificationsTest.php @@ -26,10 +26,10 @@ namespace OCA\TwoFactorBackupCodes\Tests\Unit\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; +use OCP\EventDispatcher\Event; use OCP\IUser; use OCP\Notification\IManager; use OCP\Notification\INotification; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class ClearNotificationsTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php index 1bd5a7ccab..39df7a417f 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderDisabledTest.php @@ -30,8 +30,8 @@ use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class ProviderDisabledTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php index c824ad8e87..8a4ce56d7c 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/ProviderEnabledTest.php @@ -29,8 +29,8 @@ use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled; use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Authentication\TwoFactorAuth\RegistryEvent; use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class ProviderEnabledTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php b/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php index 2e75804661..7ae6eab335 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Listener/RegistryUpdaterTest.php @@ -28,8 +28,8 @@ use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider; use OCP\Authentication\TwoFactorAuth\IRegistry; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; use Test\TestCase; class RegistryUpdaterTest extends TestCase { diff --git a/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php b/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php index 2aa323a0a7..d962961c25 100644 --- a/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php @@ -26,11 +26,11 @@ use OCA\TwoFactorBackupCodes\Db\BackupCode; use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; use OCP\Security\IHasher; use OCP\Security\ISecureRandom; use PHPUnit_Framework_MockObject_MockObject; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class BackupCodeStorageTest extends TestCase { @@ -44,7 +44,7 @@ class BackupCodeStorageTest extends TestCase { /** @var IHasher|PHPUnit_Framework_MockObject_MockObject */ private $hasher; - /** @var EventDispatcherInterface|PHPUnit_Framework_MockObject_MockObject */ + /** @var IEventDispatcher|PHPUnit_Framework_MockObject_MockObject */ private $eventDispatcher; /** @var BackupCodeStorage */ @@ -56,7 +56,7 @@ class BackupCodeStorageTest extends TestCase { $this->mapper = $this->createMock(BackupCodeMapper::class); $this->random = $this->createMock(ISecureRandom::class); $this->hasher = $this->createMock(IHasher::class); - $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->storage = new BackupCodeStorage($this->mapper, $this->random, $this->hasher, $this->eventDispatcher); } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f49865ae6e..6d06593ddb 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -169,6 +169,9 @@ return array( 'OCP\\Encryption\\IFile' => $baseDir . '/lib/public/Encryption/IFile.php', 'OCP\\Encryption\\IManager' => $baseDir . '/lib/public/Encryption/IManager.php', 'OCP\\Encryption\\Keys\\IStorage' => $baseDir . '/lib/public/Encryption/Keys/IStorage.php', + 'OCP\\EventDispatcher\\Event' => $baseDir . '/lib/public/EventDispatcher/Event.php', + 'OCP\\EventDispatcher\\IEventDispatcher' => $baseDir . '/lib/public/EventDispatcher/IEventDispatcher.php', + 'OCP\\EventDispatcher\\IEventListener' => $baseDir . '/lib/public/EventDispatcher/IEventListener.php', 'OCP\\Federation\\Exceptions\\ActionNotSupportedException' => $baseDir . '/lib/public/Federation/Exceptions/ActionNotSupportedException.php', 'OCP\\Federation\\Exceptions\\AuthenticationFailedException' => $baseDir . '/lib/public/Federation/Exceptions/AuthenticationFailedException.php', 'OCP\\Federation\\Exceptions\\BadRequestException' => $baseDir . '/lib/public/Federation/Exceptions/BadRequestException.php', @@ -810,6 +813,9 @@ return array( 'OC\\Encryption\\Manager' => $baseDir . '/lib/private/Encryption/Manager.php', 'OC\\Encryption\\Update' => $baseDir . '/lib/private/Encryption/Update.php', 'OC\\Encryption\\Util' => $baseDir . '/lib/private/Encryption/Util.php', + 'OC\\EventDispatcher\\EventDispatcher' => $baseDir . '/lib/private/EventDispatcher/EventDispatcher.php', + 'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php', + 'OC\\EventDispatcher\\SymfonyAdapter' => $baseDir . '/lib/private/EventDispatcher/SymfonyAdapter.php', 'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => $baseDir . '/lib/private/Federation/CloudFederationProviderManager.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 5f204e4e23..19c4bf80d1 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -203,6 +203,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Encryption\\IFile' => __DIR__ . '/../../..' . '/lib/public/Encryption/IFile.php', 'OCP\\Encryption\\IManager' => __DIR__ . '/../../..' . '/lib/public/Encryption/IManager.php', 'OCP\\Encryption\\Keys\\IStorage' => __DIR__ . '/../../..' . '/lib/public/Encryption/Keys/IStorage.php', + 'OCP\\EventDispatcher\\Event' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/Event.php', + 'OCP\\EventDispatcher\\IEventDispatcher' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventDispatcher.php', + 'OCP\\EventDispatcher\\IEventListener' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventListener.php', 'OCP\\Federation\\Exceptions\\ActionNotSupportedException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/ActionNotSupportedException.php', 'OCP\\Federation\\Exceptions\\AuthenticationFailedException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/AuthenticationFailedException.php', 'OCP\\Federation\\Exceptions\\BadRequestException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/BadRequestException.php', @@ -844,6 +847,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Encryption\\Manager' => __DIR__ . '/../../..' . '/lib/private/Encryption/Manager.php', 'OC\\Encryption\\Update' => __DIR__ . '/../../..' . '/lib/private/Encryption/Update.php', 'OC\\Encryption\\Util' => __DIR__ . '/../../..' . '/lib/private/Encryption/Util.php', + 'OC\\EventDispatcher\\EventDispatcher' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/EventDispatcher.php', + 'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php', + 'OC\\EventDispatcher\\SymfonyAdapter' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/SymfonyAdapter.php', 'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationProviderManager.php', diff --git a/lib/private/DB/Migrator.php b/lib/private/DB/Migrator.php index 603f9259d6..a853ab1ea1 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 0000000000..8db2f3101b --- /dev/null +++ b/lib/private/EventDispatcher/EventDispatcher.php @@ -0,0 +1,85 @@ + + * + * @author 2019 Christoph Wurst + * + * @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 . + */ + +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 0000000000..f5b6cd9b79 --- /dev/null +++ b/lib/private/EventDispatcher/ServiceEventListener.php @@ -0,0 +1,78 @@ + + * + * @author 2019 Christoph Wurst + * + * @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 . + */ + +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 0000000000..f2f2fbf59f --- /dev/null +++ b/lib/private/EventDispatcher/SymfonyAdapter.php @@ -0,0 +1,140 @@ + + * + * @author 2019 Christoph Wurst + * + * @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 . + */ + +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 408b457ec3..2b08b0bab1 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 c3a4e9d999..4cc748aa41 100644 --- a/lib/private/Share20/LegacyHooks.php +++ b/lib/private/Share20/LegacyHooks.php @@ -1,4 +1,5 @@ * @@ -21,24 +22,26 @@ * along with this program. If not, see . * */ + 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 c2ea165955..e71b6f9832 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 27dcb2fc33..8c00060e19 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') { diff --git a/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php b/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php index 9a005c9cd5..eb6eb5ca9a 100644 --- a/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php +++ b/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php @@ -24,8 +24,8 @@ declare(strict_types=1); namespace OCP\Authentication\TwoFactorAuth; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; /** * @since 15.0.0 @@ -42,6 +42,7 @@ class RegistryEvent extends Event { * @since 15.0.0 */ public function __construct(IProvider $provider, IUser $user) { + parent::__construct(); $this->provider = $provider; $this->user = $user; } diff --git a/lib/public/EventDispatcher/Event.php b/lib/public/EventDispatcher/Event.php new file mode 100644 index 0000000000..7f8554d7a8 --- /dev/null +++ b/lib/public/EventDispatcher/Event.php @@ -0,0 +1,40 @@ + + * + * @author 2019 Christoph Wurst + * + * @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 . + */ + +namespace OCP\EventDispatcher; + +use Symfony\Component\EventDispatcher\GenericEvent; + +/** + * Base event class for the event dispatcher service + * + * Typically this class isn't instantiated directly but sub classed for specific + * event types + * + * @since 17.0.0 + */ +class Event extends GenericEvent { + +} diff --git a/lib/public/EventDispatcher/IEventDispatcher.php b/lib/public/EventDispatcher/IEventDispatcher.php new file mode 100644 index 0000000000..af4d5316a7 --- /dev/null +++ b/lib/public/EventDispatcher/IEventDispatcher.php @@ -0,0 +1,61 @@ + + * + * @author 2019 Christoph Wurst + * + * @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 . + */ + +namespace OCP\EventDispatcher; + +/** + * Event dispatcher service of Nextcloud + * + * @since 17.0.0 + */ +interface IEventDispatcher { + + /** + * @param string $eventName preferably the fully-qualified class name of the Event sub class + * @param callable $listener the object that is invoked when a matching event is dispatched + * @param int $priority + * + * @since 17.0.0 + */ + public function addListener(string $eventName, callable $listener, int $priority = 0): void; + + /** + * @param string $eventName preferably the fully-qualified class name of the Event sub class to listen for + * @param string $className fully qualified class name (or ::class notation) of a \OCP\EventDispatcher\IEventListener that can be built by the DI container + * @param int $priority + * + * @since 17.0.0 + */ + public function addServiceListener(string $eventName, string $className, int $priority = 0): void; + + /** + * @param string $eventName + * @param Event $event + * + * @since 17.0.0 + */ + public function dispatch(string $eventName, Event $event): void; + +} diff --git a/apps/twofactor_backupcodes/lib/Listener/IListener.php b/lib/public/EventDispatcher/IEventListener.php similarity index 71% rename from apps/twofactor_backupcodes/lib/Listener/IListener.php rename to lib/public/EventDispatcher/IEventListener.php index ec45de5c07..5ecf31abc4 100644 --- a/apps/twofactor_backupcodes/lib/Listener/IListener.php +++ b/lib/public/EventDispatcher/IEventListener.php @@ -3,7 +3,9 @@ declare(strict_types=1); /** - * @author Christoph Wurst + * @copyright 2019 Christoph Wurst + * + * @author 2019 Christoph Wurst * * @license GNU AGPL version 3 or any later version * @@ -19,15 +21,20 @@ declare(strict_types=1); * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - * */ -namespace OCA\TwoFactorBackupCodes\Listener; +namespace OCP\EventDispatcher; -use Symfony\Component\EventDispatcher\Event; +/** + * @since 17.0.0 + */ +interface IEventListener { -interface IListener { - - public function handle(Event $event); + /** + * @param Event $event + * + * @since 17.0.0 + */ + public function handle(Event $event): void; } diff --git a/tests/Settings/Controller/CheckSetupControllerTest.php b/tests/Settings/Controller/CheckSetupControllerTest.php index 173c0a1f85..deca75b4b2 100644 --- a/tests/Settings/Controller/CheckSetupControllerTest.php +++ b/tests/Settings/Controller/CheckSetupControllerTest.php @@ -40,7 +40,7 @@ use OCP\IURLGenerator; use OCP\Lock\ILockingProvider; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\ResponseInterface; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; use OC\IntegrityCheck\Checker; @@ -67,7 +67,7 @@ class CheckSetupControllerTest extends TestCase { private $logger; /** @var Checker|\PHPUnit_Framework_MockObject_MockObject */ private $checker; - /** @var EventDispatcher|\PHPUnit_Framework_MockObject_MockObject */ + /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */ private $dispatcher; /** @var Connection|\PHPUnit_Framework_MockObject_MockObject */ private $db; @@ -105,7 +105,7 @@ class CheckSetupControllerTest extends TestCase { ->will($this->returnCallback(function($message, array $replace) { return vsprintf($message, $replace); })); - $this->dispatcher = $this->getMockBuilder(EventDispatcher::class) + $this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class) ->disableOriginalConstructor()->getMock(); $this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker') ->disableOriginalConstructor()->getMock(); diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index ddbfe85722..43310f7e05 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -23,7 +23,6 @@ namespace Test\Share20; use OC\Files\Mount\MoveableMount; use OC\HintException; use OC\Share20\DefaultShareProvider; -use OCP\Defaults; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -52,7 +51,7 @@ use OCP\Security\ISecureRandom; use OCP\Security\IHasher; use OCP\Files\Mount\IMountManager; use OCP\IGroupManager; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; /** @@ -89,7 +88,7 @@ class ManagerTest extends \Test\TestCase { protected $userManager; /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */ protected $rootFolder; - /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */ + /** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */ protected $eventDispatcher; /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */ protected $mailer; @@ -108,7 +107,7 @@ class ManagerTest extends \Test\TestCase { $this->groupManager = $this->createMock(IGroupManager::class); $this->userManager = $this->createMock(IUserManager::class); $this->rootFolder = $this->createMock(IRootFolder::class); - $this->eventDispatcher = $this->createMock(EventDispatcher::class); + $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->mailer = $this->createMock(IMailer::class); $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->defaults = $this->createMock(\OC_Defaults::class);