Merge pull request #23742 from nextcloud/enhancement/eventdispatcher-typed-listener-callable

Type the event dispatcher listener callables with Psalm
This commit is contained in:
Morris Jobke 2020-10-30 09:54:48 +01:00 committed by GitHub
commit 1654826e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 8 deletions

View File

@ -59,6 +59,7 @@ use OCP\IServerContainer;
use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\Events\ShareCreatedEvent;
use OCP\Util; use OCP\Util;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
class Application extends App { class Application extends App {
@ -74,6 +75,7 @@ class Application extends App {
/** @var IEventDispatcher $dispatcher */ /** @var IEventDispatcher $dispatcher */
$dispatcher = $container->query(IEventDispatcher::class); $dispatcher = $container->query(IEventDispatcher::class);
$oldDispatcher = $container->getServer()->getEventDispatcher();
$mountProviderCollection = $server->getMountProviderCollection(); $mountProviderCollection = $server->getMountProviderCollection();
$notifications = $server->getNotificationManager(); $notifications = $server->getNotificationManager();
@ -124,7 +126,7 @@ class Application extends App {
$notifications->registerNotifierService(Notifier::class); $notifications->registerNotifierService(Notifier::class);
$this->registerMountProviders($mountProviderCollection); $this->registerMountProviders($mountProviderCollection);
$this->registerEventsScripts($dispatcher); $this->registerEventsScripts($dispatcher, $oldDispatcher);
$this->setupSharingMenus(); $this->setupSharingMenus();
/** /**
@ -138,7 +140,7 @@ class Application extends App {
$mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider'));
} }
protected function registerEventsScripts(IEventDispatcher $dispatcher) { protected function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher) {
// sidebar and files scripts // sidebar and files scripts
$dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
$dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class); $dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class);
@ -151,12 +153,12 @@ class Application extends App {
$dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class); $dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
// notifications api to accept incoming user shares // notifications api to accept incoming user shares
$dispatcher->addListener('OCP\Share::postShare', function (GenericEvent $event) { $oldDispatcher->addListener('OCP\Share::postShare', function (GenericEvent $event) {
/** @var Listener $listener */ /** @var Listener $listener */
$listener = $this->getContainer()->query(Listener::class); $listener = $this->getContainer()->query(Listener::class);
$listener->shareNotification($event); $listener->shareNotification($event);
}); });
$dispatcher->addListener(IGroup::class . '::postAddUser', function (GenericEvent $event) { $oldDispatcher->addListener(IGroup::class . '::postAddUser', function (GenericEvent $event) {
/** @var Listener $listener */ /** @var Listener $listener */
$listener = $this->getContainer()->query(Listener::class); $listener = $this->getContainer()->query(Listener::class);
$listener->userAddedToGroup($event); $listener->userAddedToGroup($event);

View File

@ -35,9 +35,9 @@ use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IAppContainer;
use OCP\IUser; use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
class Application extends App implements IBootstrap { class Application extends App implements IBootstrap {
@ -57,7 +57,7 @@ class Application extends App implements IBootstrap {
/** /**
* @todo move the OCP events and then move the registration to `register` * @todo move the OCP events and then move the registration to `register`
*/ */
private function registerEventListeners(IEventDispatcher $dispatcher, private function registerEventListeners(EventDispatcher $dispatcher,
IAppContainer $appContainer): void { IAppContainer $appContainer): void {
$dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($appContainer) { $dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($appContainer) {
/** @var IUser $user */ /** @var IUser $user */

View File

@ -73,7 +73,9 @@ class Application extends App {
$notificationManager->registerNotifierService(RemoveLinkSharesNotifier::class); $notificationManager->registerNotifierService(RemoveLinkSharesNotifier::class);
$notificationManager->registerNotifierService(AuthenticationNotifier::class); $notificationManager->registerNotifierService(AuthenticationNotifier::class);
$eventDispatcher->addListener(IDBConnection::CHECK_MISSING_INDEXES_EVENT, $oldEventDispatcher = $server->getEventDispatcher();
$oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_INDEXES_EVENT,
function (GenericEvent $event) use ($container) { function (GenericEvent $event) use ($container) {
/** @var MissingIndexInformation $subject */ /** @var MissingIndexInformation $subject */
$subject = $event->getSubject(); $subject = $event->getSubject();
@ -179,7 +181,7 @@ class Application extends App {
} }
); );
$eventDispatcher->addListener(IDBConnection::CHECK_MISSING_COLUMNS_EVENT, $oldEventDispatcher->addListener(IDBConnection::CHECK_MISSING_COLUMNS_EVENT,
function (GenericEvent $event) use ($container) { function (GenericEvent $event) use ($container) {
/** @var MissingColumnInformation $subject */ /** @var MissingColumnInformation $subject */
$subject = $event->getSubject(); $subject = $event->getSubject();

View File

@ -39,6 +39,7 @@ interface IEventDispatcher {
* @param string $eventName preferably the fully-qualified class name of the Event sub class * @param string $eventName preferably the fully-qualified class name of the Event sub class
* @psalm-param string|class-string<T> $eventName preferably the fully-qualified class name of the Event sub class * @psalm-param string|class-string<T> $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 callable $listener the object that is invoked when a matching event is dispatched
* @psalm-param callable(T):void $listener
* @param int $priority * @param int $priority
* *
* @since 17.0.0 * @since 17.0.0
@ -50,6 +51,7 @@ interface IEventDispatcher {
* @param string $eventName preferably the fully-qualified class name of the Event sub class * @param string $eventName preferably the fully-qualified class name of the Event sub class
* @psalm-param string|class-string<T> $eventName preferably the fully-qualified class name of the Event sub class * @psalm-param string|class-string<T> $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 callable $listener the object that is invoked when a matching event is dispatched
* @psalm-param callable(T):void $listener
* *
* @since 19.0.0 * @since 19.0.0
*/ */