From 7870ca06637453f4e72dbd67edbfb3603d813196 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 21 Jul 2020 14:36:40 +0200 Subject: [PATCH] Use the proper IAppContainer and IServerContainer type hints to know which code runs with which container Signed-off-by: Morris Jobke --- apps/admin_audit/lib/AppInfo/Application.php | 38 ++++++++----------- .../lib/AppInfo/Application.php | 8 ++-- .../lib/AppInfo/Application.php | 8 ++-- apps/settings/lib/AppInfo/Application.php | 30 +++++++-------- .../lib/AppInfo/Application.php | 6 +-- apps/user_ldap/lib/AppInfo/Application.php | 22 +++++------ .../AppFramework/Bootstrap/BootContext.php | 10 ++--- lib/public/AppFramework/App.php | 1 - .../AppFramework/Bootstrap/IBootContext.php | 9 ++--- 9 files changed, 62 insertions(+), 70 deletions(-) diff --git a/apps/admin_audit/lib/AppInfo/Application.php b/apps/admin_audit/lib/AppInfo/Application.php index 81aeada2aa..b63c361b51 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -37,7 +37,6 @@ use Closure; use OC\Files\Filesystem; use OC\Files\Node\File; use OC\Group\Manager; -use OC\User\Session; use OCA\AdminAudit\Actions\AppManagement; use OCA\AdminAudit\Actions\Auth; use OCA\AdminAudit\Actions\Console; @@ -59,11 +58,11 @@ use OCP\IConfig; use OCP\IGroupManager; use OCP\ILogger; use OCP\IPreview; +use OCP\IServerContainer; use OCP\IUserSession; use OCP\Log\ILogFactory; use OCP\Share; use OCP\Util; -use Psr\Container\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -108,44 +107,43 @@ class Application extends App implements IBootstrap { * Register hooks in order to log them */ private function registerHooks(ILogger $logger, - ContainerInterface $container) { - $this->userManagementHooks($logger, $container); - $this->groupHooks($logger, $container); + IServerContainer $serverContainer) { + $this->userManagementHooks($logger, $serverContainer->get(IUserSession::class)); + $this->groupHooks($logger, $serverContainer->get(IGroupManager::class)); $this->authHooks($logger); - $this->consoleHooks($logger, $container); - $this->appHooks($logger, $container); + /** @var EventDispatcherInterface $eventDispatcher */ + $eventDispatcher = $serverContainer->get(EventDispatcherInterface::class); + $this->consoleHooks($logger, $eventDispatcher); + $this->appHooks($logger, $eventDispatcher); $this->sharingHooks($logger); - $this->fileHooks($logger, $container); + $this->fileHooks($logger, $eventDispatcher); $this->trashbinHooks($logger); $this->versionsHooks($logger); - $this->securityHooks($logger, $container); + $this->securityHooks($logger, $eventDispatcher); } private function userManagementHooks(ILogger $logger, - ContainerInterface $container) { + IUserSession $userSession) { $userActions = new UserManagement($logger); Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); Util::connectHook('OC_User', 'changeUser', $userActions, 'change'); - /** @var IUserSession|Session $userSession */ - $userSession = $container->get(IUserSession::class); $userSession->listen('\OC\User', 'postSetPassword', [$userActions, 'setPassword']); $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']); $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']); } private function groupHooks(ILogger $logger, - ContainerInterface $container) { + IGroupManager $groupManager) { $groupActions = new GroupManagement($logger); /** @var IGroupManager|Manager $groupManager */ - $groupManager = $container->getGroupManager(IGroupManager::class); $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']); $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']); $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']); @@ -173,8 +171,7 @@ class Application extends App implements IBootstrap { } private function appHooks(ILogger $logger, - ContainerInterface $container) { - $eventDispatcher = $container->get(EventDispatcherInterface::class); + EventDispatcherInterface $eventDispatcher) { $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) { $appActions = new AppManagement($logger); $appActions->enableApp($event->getAppID()); @@ -190,8 +187,7 @@ class Application extends App implements IBootstrap { } private function consoleHooks(ILogger $logger, - ContainerInterface $container) { - $eventDispatcher = $container->get(EventDispatcherInterface::class); + EventDispatcherInterface $eventDispatcher) { $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) { $appActions = new Console($logger); $appActions->runCommand($event->getArguments()); @@ -199,9 +195,8 @@ class Application extends App implements IBootstrap { } private function fileHooks(ILogger $logger, - ContainerInterface $container) { + EventDispatcherInterface $eventDispatcher) { $fileActions = new Files($logger); - $eventDispatcher = $container->get(EventDispatcherInterface::class); $eventDispatcher->addListener( IPreview::EVENT, function (GenericEvent $event) use ($fileActions) { @@ -274,8 +269,7 @@ class Application extends App implements IBootstrap { } private function securityHooks(ILogger $logger, - ContainerInterface $container) { - $eventDispatcher = $container->get(EventDispatcherInterface::class); + EventDispatcherInterface $eventDispatcher) { $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) { $security = new Security($logger); $security->twofactorSuccess($event->getSubject(), $event->getArguments()); diff --git a/apps/federatedfilesharing/lib/AppInfo/Application.php b/apps/federatedfilesharing/lib/AppInfo/Application.php index 1afaa01a48..48907a8844 100644 --- a/apps/federatedfilesharing/lib/AppInfo/Application.php +++ b/apps/federatedfilesharing/lib/AppInfo/Application.php @@ -37,9 +37,9 @@ use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\IAppContainer; use OCP\Federation\ICloudFederationProviderManager; use OCP\Notification\IManager as INotifiactionManager; -use Psr\Container\ContainerInterface; class Application extends App implements IBootstrap { public function __construct() { @@ -56,11 +56,11 @@ class Application extends App implements IBootstrap { } private function registerCloudFederationProvider(ICloudFederationProviderManager $manager, - ContainerInterface $container): void { + IAppContainer $appContainer): void { $manager->addCloudFederationProvider('file', 'Federated Files Sharing', - function () use ($container) { - return $container->get(CloudFederationProviderFiles::class); + function () use ($appContainer) { + return $appContainer->get(CloudFederationProviderFiles::class); }); } diff --git a/apps/lookup_server_connector/lib/AppInfo/Application.php b/apps/lookup_server_connector/lib/AppInfo/Application.php index df5ddcbab8..cc51dff5cf 100644 --- a/apps/lookup_server_connector/lib/AppInfo/Application.php +++ b/apps/lookup_server_connector/lib/AppInfo/Application.php @@ -35,8 +35,8 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\EventDispatcher\IEventDispatcher; +use OCP\AppFramework\IAppContainer; use OCP\IUser; -use Psr\Container\ContainerInterface; use Symfony\Component\EventDispatcher\GenericEvent; class Application extends App implements IBootstrap { @@ -57,13 +57,13 @@ class Application extends App implements IBootstrap { * @todo move the OCP events and then move the registration to `register` */ private function registerEventListeners(IEventDispatcher $dispatcher, - ContainerInterface $container): void { - $dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($container) { + IAppContainer $appContainer): void { + $dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($appContainer) { /** @var IUser $user */ $user = $event->getSubject(); /** @var UpdateLookupServer $updateLookupServer */ - $updateLookupServer = $container->get(UpdateLookupServer::class); + $updateLookupServer = $appContainer->get(UpdateLookupServer::class); $updateLookupServer->userUpdated($user); }); } diff --git a/apps/settings/lib/AppInfo/Application.php b/apps/settings/lib/AppInfo/Application.php index 3ea4d6bc08..6e6eb26d01 100644 --- a/apps/settings/lib/AppInfo/Application.php +++ b/apps/settings/lib/AppInfo/Application.php @@ -51,6 +51,7 @@ use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\IAppContainer; use OCP\Defaults; use OCP\IGroup; use OCP\IGroupManager; @@ -59,7 +60,6 @@ use OCP\IServerContainer; use OCP\IUser; use OCP\Settings\IManager; use OCP\Util; -use Psr\Container\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -94,30 +94,30 @@ class Application extends App implements IBootstrap { } return $isSubAdmin; }); - $context->registerService('userCertificateManager', function (ContainerInterface $c) { + $context->registerService('userCertificateManager', function (IAppContainer $appContainer) { /** @var IServerContainer $serverContainer */ - $serverContainer = $c->get(IServerContainer::class); + $serverContainer = $appContainer->get(IServerContainer::class); return $serverContainer->getCertificateManager(); }, false); - $context->registerService('systemCertificateManager', function (ContainerInterface $c) { + $context->registerService('systemCertificateManager', function (IAppContainer $appContainer) { /** @var IServerContainer $serverContainer */ - $serverContainer = $c->query('ServerContainer'); + $serverContainer = $appContainer->query('ServerContainer'); return $serverContainer->getCertificateManager(null); }, false); - $context->registerService(IProvider::class, function (ContainerInterface $c) { + $context->registerService(IProvider::class, function (IAppContainer $appContainer) { /** @var IServerContainer $serverContainer */ - $serverContainer = $c->query(IServerContainer::class); + $serverContainer = $appContainer->query(IServerContainer::class); return $serverContainer->query(IProvider::class); }); - $context->registerService(IManager::class, function (ContainerInterface $c) { + $context->registerService(IManager::class, function (IAppContainer $appContainer) { /** @var IServerContainer $serverContainer */ - $serverContainer = $c->query(IServerContainer::class); + $serverContainer = $appContainer->query(IServerContainer::class); return $serverContainer->getSettingsManager(); }); - $context->registerService(NewUserMailHelper::class, function (ContainerInterface $c) { + $context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) { /** @var Server $server */ - $server = $c->query(IServerContainer::class); + $server = $appContainer->query(IServerContainer::class); /** @var Defaults $defaults */ $defaults = $server->query(Defaults::class); @@ -136,13 +136,13 @@ class Application extends App implements IBootstrap { } public function boot(IBootContext $context): void { - $context->injectFn(function (EventDispatcherInterface $dispatcher, ContainerInterface $container) { - $dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($container) { + $context->injectFn(function (EventDispatcherInterface $dispatcher, IAppContainer $appContainer) { + $dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($appContainer) { if (($token = $event->getSubject()) instanceof IToken) { /** @var IActivityManager $activityManager */ - $activityManager = $container->get(IActivityManager::class); + $activityManager = $appContainer->get(IActivityManager::class); /** @var ILogger $logger */ - $logger = $container->get(ILogger::class); + $logger = $appContainer->get(ILogger::class); $activity = $activityManager->generateEvent(); $activity->setApp('settings') diff --git a/apps/updatenotification/lib/AppInfo/Application.php b/apps/updatenotification/lib/AppInfo/Application.php index c369c95744..48f89de3c3 100644 --- a/apps/updatenotification/lib/AppInfo/Application.php +++ b/apps/updatenotification/lib/AppInfo/Application.php @@ -35,6 +35,7 @@ use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\IAppContainer; use OCP\AppFramework\QueryException; use OCP\IConfig; use OCP\IGroupManager; @@ -43,7 +44,6 @@ use OCP\IUser; use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; use OCP\Util; -use Psr\Container\ContainerInterface; class Application extends App implements IBootstrap { public function __construct() { @@ -59,7 +59,7 @@ class Application extends App implements IBootstrap { IUserSession $userSession, IAppManager $appManager, IGroupManager $groupManager, - ContainerInterface $container, + IAppContainer $appContainer, ILogger $logger) { if ($config->getSystemValue('updatechecker', true) !== true) { // Updater check is disabled @@ -78,7 +78,7 @@ class Application extends App implements IBootstrap { if (!$appManager->isEnabledForUser('notifications') && $groupManager->isAdmin($user->getUID())) { try { - $updateChecker = $container->get(UpdateChecker::class); + $updateChecker = $appContainer->get(UpdateChecker::class); } catch (QueryException $e) { $logger->logException($e); return; diff --git a/apps/user_ldap/lib/AppInfo/Application.php b/apps/user_ldap/lib/AppInfo/Application.php index fb501eedd7..7086bb08ee 100644 --- a/apps/user_ldap/lib/AppInfo/Application.php +++ b/apps/user_ldap/lib/AppInfo/Application.php @@ -42,13 +42,13 @@ use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\IAppContainer; use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\IServerContainer; use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; -use Psr\Container\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Application extends App implements IBootstrap { @@ -59,17 +59,17 @@ class Application extends App implements IBootstrap { /** * Controller */ - $container->registerService('RenewPasswordController', function (ContainerInterface $c) { + $container->registerService('RenewPasswordController', function (IAppContainer $appContainer) { /** @var IServerContainer $server */ - $server = $c->get(IServerContainer::class); + $server = $appContainer->get(IServerContainer::class); return new RenewPasswordController( - $c->get('AppName'), + $appContainer->get('AppName'), $server->getRequest(), - $c->get('UserManager'), + $appContainer->get('UserManager'), $server->getConfig(), - $c->get(IL10N::class), - $c->get('Session'), + $appContainer->get(IL10N::class), + $appContainer->get('Session'), $server->getURLGenerator() ); }); @@ -86,7 +86,7 @@ class Application extends App implements IBootstrap { $context->injectFn(function (IConfig $config, INotificationManager $notificationManager, IUserSession $userSession, - ContainerInterface $container, + IAppContainer $appContainer, EventDispatcherInterface $dispatcher, IGroupManager $groupManager) { $helper = new Helper($config); @@ -96,8 +96,8 @@ class Application extends App implements IBootstrap { $notificationManager->registerNotifierService(Notifier::class); - $userPluginManager = $container->get(UserPluginManager::class); - $groupPluginManager = $container->get(GroupPluginManager::class); + $userPluginManager = $appContainer->get(UserPluginManager::class); + $groupPluginManager = $appContainer->get(GroupPluginManager::class); $userBackend = new User_Proxy( $configPrefixes, $ldapWrapper, $config, $notificationManager, $userSession, $userPluginManager @@ -123,7 +123,7 @@ class Application extends App implements IBootstrap { ); } - private function registerBackendDependents(ContainerInterface $appContainer, EventDispatcherInterface $dispatcher) { + private function registerBackendDependents(IAppContainer $appContainer, EventDispatcherInterface $dispatcher) { $dispatcher->addListener( 'OCA\\Files_External::loadAdditionalBackends', function () use ($appContainer) { diff --git a/lib/private/AppFramework/Bootstrap/BootContext.php b/lib/private/AppFramework/Bootstrap/BootContext.php index 7c2582a726..45aaf167e7 100644 --- a/lib/private/AppFramework/Bootstrap/BootContext.php +++ b/lib/private/AppFramework/Bootstrap/BootContext.php @@ -26,23 +26,23 @@ declare(strict_types=1); namespace OC\AppFramework\Bootstrap; use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\IAppContainer; use OCP\IServerContainer; -use Psr\Container\ContainerInterface; class BootContext implements IBootContext { - /** @var ContainerInterface */ + /** @var IAppContainer */ private $appContainer; - public function __construct(ContainerInterface $appContainer) { + public function __construct(IAppContainer $appContainer) { $this->appContainer = $appContainer; } - public function getAppContainer(): ContainerInterface { + public function getAppContainer(): IAppContainer { return $this->appContainer; } - public function getServerContainer(): ContainerInterface { + public function getServerContainer(): IServerContainer { return $this->appContainer->get(IServerContainer::class); } diff --git a/lib/public/AppFramework/App.php b/lib/public/AppFramework/App.php index de1cbcb64e..2f55fd4514 100644 --- a/lib/public/AppFramework/App.php +++ b/lib/public/AppFramework/App.php @@ -120,7 +120,6 @@ class App { /** * @return IAppContainer * @since 6.0.0 - * @todo make this return a ContainerInterface as well */ public function getContainer(): IAppContainer { return $this->container; diff --git a/lib/public/AppFramework/Bootstrap/IBootContext.php b/lib/public/AppFramework/Bootstrap/IBootContext.php index 86232cef4a..80a4edf1cd 100644 --- a/lib/public/AppFramework/Bootstrap/IBootContext.php +++ b/lib/public/AppFramework/Bootstrap/IBootContext.php @@ -28,7 +28,6 @@ namespace OCP\AppFramework\Bootstrap; use OCP\AppFramework\IAppContainer; use OCP\IServerContainer; use Psr\Container\ContainerExceptionInterface; -use Psr\Container\ContainerInterface; use Throwable; /** @@ -41,20 +40,20 @@ interface IBootContext { * * Useful to register and query app-specific services * - * @return ContainerInterface|IAppContainer + * @return IAppContainer * @since 20.0.0 */ - public function getAppContainer(): ContainerInterface; + public function getAppContainer(): IAppContainer; /** * Get hold of the server DI container * * Useful to register and query system-wide services * - * @return ContainerInterface|IServerContainer + * @return IServerContainer * @since 20.0.0 */ - public function getServerContainer(): ContainerInterface; + public function getServerContainer(): IServerContainer; /** * Invoke the given callable and inject all parameters based on their types