Use the proper IAppContainer and IServerContainer type hints to know which code runs with which container

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
This commit is contained in:
Morris Jobke 2020-07-21 14:36:40 +02:00
parent 91e7f12088
commit 7870ca0663
No known key found for this signature in database
GPG Key ID: FE03C3A163FEDE68
9 changed files with 62 additions and 70 deletions

View File

@ -37,7 +37,6 @@ use Closure;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\Node\File; use OC\Files\Node\File;
use OC\Group\Manager; use OC\Group\Manager;
use OC\User\Session;
use OCA\AdminAudit\Actions\AppManagement; use OCA\AdminAudit\Actions\AppManagement;
use OCA\AdminAudit\Actions\Auth; use OCA\AdminAudit\Actions\Auth;
use OCA\AdminAudit\Actions\Console; use OCA\AdminAudit\Actions\Console;
@ -59,11 +58,11 @@ use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\ILogger; use OCP\ILogger;
use OCP\IPreview; use OCP\IPreview;
use OCP\IServerContainer;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Log\ILogFactory; use OCP\Log\ILogFactory;
use OCP\Share; use OCP\Share;
use OCP\Util; use OCP\Util;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
@ -108,44 +107,43 @@ class Application extends App implements IBootstrap {
* Register hooks in order to log them * Register hooks in order to log them
*/ */
private function registerHooks(ILogger $logger, private function registerHooks(ILogger $logger,
ContainerInterface $container) { IServerContainer $serverContainer) {
$this->userManagementHooks($logger, $container); $this->userManagementHooks($logger, $serverContainer->get(IUserSession::class));
$this->groupHooks($logger, $container); $this->groupHooks($logger, $serverContainer->get(IGroupManager::class));
$this->authHooks($logger); $this->authHooks($logger);
$this->consoleHooks($logger, $container); /** @var EventDispatcherInterface $eventDispatcher */
$this->appHooks($logger, $container); $eventDispatcher = $serverContainer->get(EventDispatcherInterface::class);
$this->consoleHooks($logger, $eventDispatcher);
$this->appHooks($logger, $eventDispatcher);
$this->sharingHooks($logger); $this->sharingHooks($logger);
$this->fileHooks($logger, $container); $this->fileHooks($logger, $eventDispatcher);
$this->trashbinHooks($logger); $this->trashbinHooks($logger);
$this->versionsHooks($logger); $this->versionsHooks($logger);
$this->securityHooks($logger, $container); $this->securityHooks($logger, $eventDispatcher);
} }
private function userManagementHooks(ILogger $logger, private function userManagementHooks(ILogger $logger,
ContainerInterface $container) { IUserSession $userSession) {
$userActions = new UserManagement($logger); $userActions = new UserManagement($logger);
Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); Util::connectHook('OC_User', 'post_createUser', $userActions, 'create');
Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete');
Util::connectHook('OC_User', 'changeUser', $userActions, 'change'); 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', 'postSetPassword', [$userActions, 'setPassword']);
$userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']); $userSession->listen('\OC\User', 'assignedUserId', [$userActions, 'assign']);
$userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']); $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
} }
private function groupHooks(ILogger $logger, private function groupHooks(ILogger $logger,
ContainerInterface $container) { IGroupManager $groupManager) {
$groupActions = new GroupManagement($logger); $groupActions = new GroupManagement($logger);
/** @var IGroupManager|Manager $groupManager */ /** @var IGroupManager|Manager $groupManager */
$groupManager = $container->getGroupManager(IGroupManager::class);
$groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']); $groupManager->listen('\OC\Group', 'postRemoveUser', [$groupActions, 'removeUser']);
$groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']); $groupManager->listen('\OC\Group', 'postAddUser', [$groupActions, 'addUser']);
$groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']); $groupManager->listen('\OC\Group', 'postDelete', [$groupActions, 'deleteGroup']);
@ -173,8 +171,7 @@ class Application extends App implements IBootstrap {
} }
private function appHooks(ILogger $logger, private function appHooks(ILogger $logger,
ContainerInterface $container) { EventDispatcherInterface $eventDispatcher) {
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) { $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
$appActions = new AppManagement($logger); $appActions = new AppManagement($logger);
$appActions->enableApp($event->getAppID()); $appActions->enableApp($event->getAppID());
@ -190,8 +187,7 @@ class Application extends App implements IBootstrap {
} }
private function consoleHooks(ILogger $logger, private function consoleHooks(ILogger $logger,
ContainerInterface $container) { EventDispatcherInterface $eventDispatcher) {
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) { $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) {
$appActions = new Console($logger); $appActions = new Console($logger);
$appActions->runCommand($event->getArguments()); $appActions->runCommand($event->getArguments());
@ -199,9 +195,8 @@ class Application extends App implements IBootstrap {
} }
private function fileHooks(ILogger $logger, private function fileHooks(ILogger $logger,
ContainerInterface $container) { EventDispatcherInterface $eventDispatcher) {
$fileActions = new Files($logger); $fileActions = new Files($logger);
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$eventDispatcher->addListener( $eventDispatcher->addListener(
IPreview::EVENT, IPreview::EVENT,
function (GenericEvent $event) use ($fileActions) { function (GenericEvent $event) use ($fileActions) {
@ -274,8 +269,7 @@ class Application extends App implements IBootstrap {
} }
private function securityHooks(ILogger $logger, private function securityHooks(ILogger $logger,
ContainerInterface $container) { EventDispatcherInterface $eventDispatcher) {
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) { $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) {
$security = new Security($logger); $security = new Security($logger);
$security->twofactorSuccess($event->getSubject(), $event->getArguments()); $security->twofactorSuccess($event->getSubject(), $event->getArguments());

View File

@ -37,9 +37,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\AppFramework\IAppContainer;
use OCP\Federation\ICloudFederationProviderManager; use OCP\Federation\ICloudFederationProviderManager;
use OCP\Notification\IManager as INotifiactionManager; use OCP\Notification\IManager as INotifiactionManager;
use Psr\Container\ContainerInterface;
class Application extends App implements IBootstrap { class Application extends App implements IBootstrap {
public function __construct() { public function __construct() {
@ -56,11 +56,11 @@ class Application extends App implements IBootstrap {
} }
private function registerCloudFederationProvider(ICloudFederationProviderManager $manager, private function registerCloudFederationProvider(ICloudFederationProviderManager $manager,
ContainerInterface $container): void { IAppContainer $appContainer): void {
$manager->addCloudFederationProvider('file', $manager->addCloudFederationProvider('file',
'Federated Files Sharing', 'Federated Files Sharing',
function () use ($container) { function () use ($appContainer) {
return $container->get(CloudFederationProviderFiles::class); return $appContainer->get(CloudFederationProviderFiles::class);
}); });
} }

View File

@ -35,8 +35,8 @@ 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\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\IAppContainer;
use OCP\IUser; use OCP\IUser;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
class Application extends App implements IBootstrap { 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` * @todo move the OCP events and then move the registration to `register`
*/ */
private function registerEventListeners(IEventDispatcher $dispatcher, private function registerEventListeners(IEventDispatcher $dispatcher,
ContainerInterface $container): void { IAppContainer $appContainer): void {
$dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($container) { $dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($appContainer) {
/** @var IUser $user */ /** @var IUser $user */
$user = $event->getSubject(); $user = $event->getSubject();
/** @var UpdateLookupServer $updateLookupServer */ /** @var UpdateLookupServer $updateLookupServer */
$updateLookupServer = $container->get(UpdateLookupServer::class); $updateLookupServer = $appContainer->get(UpdateLookupServer::class);
$updateLookupServer->userUpdated($user); $updateLookupServer->userUpdated($user);
}); });
} }

View File

@ -51,6 +51,7 @@ 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\AppFramework\IAppContainer;
use OCP\Defaults; use OCP\Defaults;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
@ -59,7 +60,6 @@ use OCP\IServerContainer;
use OCP\IUser; use OCP\IUser;
use OCP\Settings\IManager; use OCP\Settings\IManager;
use OCP\Util; use OCP\Util;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
@ -94,30 +94,30 @@ class Application extends App implements IBootstrap {
} }
return $isSubAdmin; return $isSubAdmin;
}); });
$context->registerService('userCertificateManager', function (ContainerInterface $c) { $context->registerService('userCertificateManager', function (IAppContainer $appContainer) {
/** @var IServerContainer $serverContainer */ /** @var IServerContainer $serverContainer */
$serverContainer = $c->get(IServerContainer::class); $serverContainer = $appContainer->get(IServerContainer::class);
return $serverContainer->getCertificateManager(); return $serverContainer->getCertificateManager();
}, false); }, false);
$context->registerService('systemCertificateManager', function (ContainerInterface $c) { $context->registerService('systemCertificateManager', function (IAppContainer $appContainer) {
/** @var IServerContainer $serverContainer */ /** @var IServerContainer $serverContainer */
$serverContainer = $c->query('ServerContainer'); $serverContainer = $appContainer->query('ServerContainer');
return $serverContainer->getCertificateManager(null); return $serverContainer->getCertificateManager(null);
}, false); }, false);
$context->registerService(IProvider::class, function (ContainerInterface $c) { $context->registerService(IProvider::class, function (IAppContainer $appContainer) {
/** @var IServerContainer $serverContainer */ /** @var IServerContainer $serverContainer */
$serverContainer = $c->query(IServerContainer::class); $serverContainer = $appContainer->query(IServerContainer::class);
return $serverContainer->query(IProvider::class); return $serverContainer->query(IProvider::class);
}); });
$context->registerService(IManager::class, function (ContainerInterface $c) { $context->registerService(IManager::class, function (IAppContainer $appContainer) {
/** @var IServerContainer $serverContainer */ /** @var IServerContainer $serverContainer */
$serverContainer = $c->query(IServerContainer::class); $serverContainer = $appContainer->query(IServerContainer::class);
return $serverContainer->getSettingsManager(); return $serverContainer->getSettingsManager();
}); });
$context->registerService(NewUserMailHelper::class, function (ContainerInterface $c) { $context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
/** @var Server $server */ /** @var Server $server */
$server = $c->query(IServerContainer::class); $server = $appContainer->query(IServerContainer::class);
/** @var Defaults $defaults */ /** @var Defaults $defaults */
$defaults = $server->query(Defaults::class); $defaults = $server->query(Defaults::class);
@ -136,13 +136,13 @@ class Application extends App implements IBootstrap {
} }
public function boot(IBootContext $context): void { public function boot(IBootContext $context): void {
$context->injectFn(function (EventDispatcherInterface $dispatcher, ContainerInterface $container) { $context->injectFn(function (EventDispatcherInterface $dispatcher, IAppContainer $appContainer) {
$dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($container) { $dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($appContainer) {
if (($token = $event->getSubject()) instanceof IToken) { if (($token = $event->getSubject()) instanceof IToken) {
/** @var IActivityManager $activityManager */ /** @var IActivityManager $activityManager */
$activityManager = $container->get(IActivityManager::class); $activityManager = $appContainer->get(IActivityManager::class);
/** @var ILogger $logger */ /** @var ILogger $logger */
$logger = $container->get(ILogger::class); $logger = $appContainer->get(ILogger::class);
$activity = $activityManager->generateEvent(); $activity = $activityManager->generateEvent();
$activity->setApp('settings') $activity->setApp('settings')

View File

@ -35,6 +35,7 @@ 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\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException; use OCP\AppFramework\QueryException;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
@ -43,7 +44,6 @@ use OCP\IUser;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use OCP\Util; use OCP\Util;
use Psr\Container\ContainerInterface;
class Application extends App implements IBootstrap { class Application extends App implements IBootstrap {
public function __construct() { public function __construct() {
@ -59,7 +59,7 @@ class Application extends App implements IBootstrap {
IUserSession $userSession, IUserSession $userSession,
IAppManager $appManager, IAppManager $appManager,
IGroupManager $groupManager, IGroupManager $groupManager,
ContainerInterface $container, IAppContainer $appContainer,
ILogger $logger) { ILogger $logger) {
if ($config->getSystemValue('updatechecker', true) !== true) { if ($config->getSystemValue('updatechecker', true) !== true) {
// Updater check is disabled // Updater check is disabled
@ -78,7 +78,7 @@ class Application extends App implements IBootstrap {
if (!$appManager->isEnabledForUser('notifications') && if (!$appManager->isEnabledForUser('notifications') &&
$groupManager->isAdmin($user->getUID())) { $groupManager->isAdmin($user->getUID())) {
try { try {
$updateChecker = $container->get(UpdateChecker::class); $updateChecker = $appContainer->get(UpdateChecker::class);
} catch (QueryException $e) { } catch (QueryException $e) {
$logger->logException($e); $logger->logException($e);
return; return;

View File

@ -42,13 +42,13 @@ 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\AppFramework\IAppContainer;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IL10N; use OCP\IL10N;
use OCP\IServerContainer; use OCP\IServerContainer;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Application extends App implements IBootstrap { class Application extends App implements IBootstrap {
@ -59,17 +59,17 @@ class Application extends App implements IBootstrap {
/** /**
* Controller * Controller
*/ */
$container->registerService('RenewPasswordController', function (ContainerInterface $c) { $container->registerService('RenewPasswordController', function (IAppContainer $appContainer) {
/** @var IServerContainer $server */ /** @var IServerContainer $server */
$server = $c->get(IServerContainer::class); $server = $appContainer->get(IServerContainer::class);
return new RenewPasswordController( return new RenewPasswordController(
$c->get('AppName'), $appContainer->get('AppName'),
$server->getRequest(), $server->getRequest(),
$c->get('UserManager'), $appContainer->get('UserManager'),
$server->getConfig(), $server->getConfig(),
$c->get(IL10N::class), $appContainer->get(IL10N::class),
$c->get('Session'), $appContainer->get('Session'),
$server->getURLGenerator() $server->getURLGenerator()
); );
}); });
@ -86,7 +86,7 @@ class Application extends App implements IBootstrap {
$context->injectFn(function (IConfig $config, $context->injectFn(function (IConfig $config,
INotificationManager $notificationManager, INotificationManager $notificationManager,
IUserSession $userSession, IUserSession $userSession,
ContainerInterface $container, IAppContainer $appContainer,
EventDispatcherInterface $dispatcher, EventDispatcherInterface $dispatcher,
IGroupManager $groupManager) { IGroupManager $groupManager) {
$helper = new Helper($config); $helper = new Helper($config);
@ -96,8 +96,8 @@ class Application extends App implements IBootstrap {
$notificationManager->registerNotifierService(Notifier::class); $notificationManager->registerNotifierService(Notifier::class);
$userPluginManager = $container->get(UserPluginManager::class); $userPluginManager = $appContainer->get(UserPluginManager::class);
$groupPluginManager = $container->get(GroupPluginManager::class); $groupPluginManager = $appContainer->get(GroupPluginManager::class);
$userBackend = new User_Proxy( $userBackend = new User_Proxy(
$configPrefixes, $ldapWrapper, $config, $notificationManager, $userSession, $userPluginManager $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( $dispatcher->addListener(
'OCA\\Files_External::loadAdditionalBackends', 'OCA\\Files_External::loadAdditionalBackends',
function () use ($appContainer) { function () use ($appContainer) {

View File

@ -26,23 +26,23 @@ declare(strict_types=1);
namespace OC\AppFramework\Bootstrap; namespace OC\AppFramework\Bootstrap;
use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\IAppContainer;
use OCP\IServerContainer; use OCP\IServerContainer;
use Psr\Container\ContainerInterface;
class BootContext implements IBootContext { class BootContext implements IBootContext {
/** @var ContainerInterface */ /** @var IAppContainer */
private $appContainer; private $appContainer;
public function __construct(ContainerInterface $appContainer) { public function __construct(IAppContainer $appContainer) {
$this->appContainer = $appContainer; $this->appContainer = $appContainer;
} }
public function getAppContainer(): ContainerInterface { public function getAppContainer(): IAppContainer {
return $this->appContainer; return $this->appContainer;
} }
public function getServerContainer(): ContainerInterface { public function getServerContainer(): IServerContainer {
return $this->appContainer->get(IServerContainer::class); return $this->appContainer->get(IServerContainer::class);
} }

View File

@ -120,7 +120,6 @@ class App {
/** /**
* @return IAppContainer * @return IAppContainer
* @since 6.0.0 * @since 6.0.0
* @todo make this return a ContainerInterface as well
*/ */
public function getContainer(): IAppContainer { public function getContainer(): IAppContainer {
return $this->container; return $this->container;

View File

@ -28,7 +28,6 @@ namespace OCP\AppFramework\Bootstrap;
use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IAppContainer;
use OCP\IServerContainer; use OCP\IServerContainer;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Throwable; use Throwable;
/** /**
@ -41,20 +40,20 @@ interface IBootContext {
* *
* Useful to register and query app-specific services * Useful to register and query app-specific services
* *
* @return ContainerInterface|IAppContainer * @return IAppContainer
* @since 20.0.0 * @since 20.0.0
*/ */
public function getAppContainer(): ContainerInterface; public function getAppContainer(): IAppContainer;
/** /**
* Get hold of the server DI container * Get hold of the server DI container
* *
* Useful to register and query system-wide services * Useful to register and query system-wide services
* *
* @return ContainerInterface|IServerContainer * @return IServerContainer
* @since 20.0.0 * @since 20.0.0
*/ */
public function getServerContainer(): ContainerInterface; public function getServerContainer(): IServerContainer;
/** /**
* Invoke the given callable and inject all parameters based on their types * Invoke the given callable and inject all parameters based on their types