]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use PSR container interface and deprecate our own abstraction 21809/head
authorChristoph Wurst <christoph@winzerhof-wurst.at>
Mon, 13 Jul 2020 09:18:14 +0000 (11:18 +0200)
committerChristoph Wurst <christoph@winzerhof-wurst.at>
Thu, 16 Jul 2020 11:35:45 +0000 (13:35 +0200)
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
3rdparty
lib/private/AppFramework/DependencyInjection/DIContainer.php
lib/private/AppFramework/Utility/SimpleContainer.php
lib/private/Server.php
lib/private/ServerContainer.php
lib/public/AppFramework/Bootstrap/IBootContext.php
lib/public/AppFramework/IAppContainer.php
lib/public/AppFramework/QueryException.php
lib/public/IContainer.php
lib/public/IServerContainer.php

index 8460f552fbf8ee468734225fa6a890cde4beb964..10acc20198442f3c04bc1abbf9ad540a3afbab40 160000 (submodule)
--- a/3rdparty
+++ b/3rdparty
@@ -1 +1 @@
-Subproject commit 8460f552fbf8ee468734225fa6a890cde4beb964
+Subproject commit 10acc20198442f3c04bc1abbf9ad540a3afbab40
index bda014838ed046a4ba92ea6c3a9af9ee5e7425e2..d45c90a720595af26e4d11363584314681e49d22 100644 (file)
@@ -69,7 +69,11 @@ use OCP\IServerContainer;
 use OCP\ISession;
 use OCP\IURLGenerator;
 use OCP\IUserSession;
+use Psr\Container\ContainerInterface;
 
+/**
+ * @deprecated 20.0.0
+ */
 class DIContainer extends SimpleContainer implements IAppContainer {
 
        /**
@@ -116,17 +120,17 @@ class DIContainer extends SimpleContainer implements IAppContainer {
                        return $this->getServer()->getUserFolder();
                });
 
-               $this->registerService(IAppData::class, function (SimpleContainer $c) {
-                       return $this->getServer()->getAppDataDir($c->query('AppName'));
+               $this->registerService(IAppData::class, function (ContainerInterface $c) {
+                       return $this->getServer()->getAppDataDir($c->get('AppName'));
                });
 
-               $this->registerService(IL10N::class, function ($c) {
-                       return $this->getServer()->getL10N($c->query('AppName'));
+               $this->registerService(IL10N::class, function (ContainerInterface $c) {
+                       return $this->getServer()->getL10N($c->get('AppName'));
                });
 
                // Log wrapper
-               $this->registerService(ILogger::class, function ($c) {
-                       return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName'));
+               $this->registerService(ILogger::class, function (ContainerInterface $c) {
+                       return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName'));
                });
 
                $this->registerService(IServerContainer::class, function () {
@@ -134,40 +138,41 @@ class DIContainer extends SimpleContainer implements IAppContainer {
                });
                $this->registerAlias('ServerContainer', IServerContainer::class);
 
-               $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
-                       return $c->query(Manager::class);
+               $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) {
+                       return $c->get(Manager::class);
                });
 
-               $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
+               $this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
                        return $c;
                });
+               $this->registerAlias(IAppContainer::class, ContainerInterface::class);
 
                // commonly used attributes
-               $this->registerService('UserId', function ($c) {
-                       return $c->query(IUserSession::class)->getSession()->get('user_id');
+               $this->registerService('UserId', function (ContainerInterface $c) {
+                       return $c->get(IUserSession::class)->getSession()->get('user_id');
                });
 
-               $this->registerService('WebRoot', function ($c) {
-                       return $c->query('ServerContainer')->getWebRoot();
+               $this->registerService('WebRoot', function (ContainerInterface $c) {
+                       return $c->get(IServerContainer::class)->getWebRoot();
                });
 
-               $this->registerService('OC_Defaults', function ($c) {
-                       return $c->getServer()->getThemingDefaults();
+               $this->registerService('OC_Defaults', function (ContainerInterface $c) {
+                       return $c->get(IServerContainer::class)->getThemingDefaults();
                });
 
-               $this->registerService('Protocol', function ($c) {
+               $this->registerService('Protocol', function (ContainerInterface $c) {
                        /** @var \OC\Server $server */
-                       $server = $c->query('ServerContainer');
+                       $server = $c->get(IServerContainer::class);
                        $protocol = $server->getRequest()->getHttpProtocol();
                        return new Http($_SERVER, $protocol);
                });
 
-               $this->registerService('Dispatcher', function ($c) {
+               $this->registerService('Dispatcher', function (ContainerInterface $c) {
                        return new Dispatcher(
-                               $c['Protocol'],
-                               $c['MiddlewareDispatcher'],
-                               $c->query(IControllerMethodReflector::class),
-                               $c['Request']
+                               $c->get('Protocol'),
+                               $c->get(MiddlewareDispatcher::class),
+                               $c->get(IControllerMethodReflector::class),
+                               $c->get(IRequest::class)
                        );
                });
 
@@ -181,48 +186,49 @@ class DIContainer extends SimpleContainer implements IAppContainer {
                /**
                 * Middleware
                 */
-               $this->registerService('MiddlewareDispatcher', function (SimpleContainer $c) {
-                       $server =  $this->getServer();
+               $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class);
+               $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) {
+                       $server = $this->getServer();
 
                        $dispatcher = new MiddlewareDispatcher();
 
                        $dispatcher->registerMiddleware(
-                               $c->query(OC\AppFramework\Middleware\CompressionMiddleware::class)
+                               $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class)
                        );
 
-                       $dispatcher->registerMiddleware($c->query(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
+                       $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
 
                        $dispatcher->registerMiddleware(
-                               $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
+                               $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
                        );
 
                        $dispatcher->registerMiddleware(
                                new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
-                                       $c->query(IRequest::class),
-                                       $c->query(IControllerMethodReflector::class)
+                                       $c->get(IRequest::class),
+                                       $c->get(IControllerMethodReflector::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
                                new CORSMiddleware(
-                                       $c->query(IRequest::class),
-                                       $c->query(IControllerMethodReflector::class),
-                                       $c->query(IUserSession::class),
-                                       $c->query(OC\Security\Bruteforce\Throttler::class)
+                                       $c->get(IRequest::class),
+                                       $c->get(IControllerMethodReflector::class),
+                                       $c->get(IUserSession::class),
+                                       $c->get(OC\Security\Bruteforce\Throttler::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
                                new OCSMiddleware(
-                                       $c->query(IRequest::class)
+                                       $c->get(IRequest::class)
                                )
                        );
 
                        $securityMiddleware = new SecurityMiddleware(
-                               $c->query(IRequest::class),
-                               $c->query(IControllerMethodReflector::class),
-                               $c->query(INavigationManager::class),
-                               $c->query(IURLGenerator::class),
-                               $server->getLogger(),
-                               $c['AppName'],
+                               $c->get(IRequest::class),
+                               $c->get(IControllerMethodReflector::class),
+                               $c->get(INavigationManager::class),
+                               $c->get(IURLGenerator::class),
+                               $server->query(ILogger::class),
+                               $c->get('AppName'),
                                $server->getUserSession()->isLoggedIn(),
                                $server->getGroupManager()->isAdmin($this->getUserId()),
                                $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()),
@@ -242,71 +248,71 @@ class DIContainer extends SimpleContainer implements IAppContainer {
                        );
                        $dispatcher->registerMiddleware(
                                new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
-                                       $c->query(IControllerMethodReflector::class),
-                                       $c->query(ISession::class),
-                                       $c->query(IUserSession::class),
-                                       $c->query(ITimeFactory::class)
+                                       $c->get(IControllerMethodReflector::class),
+                                       $c->get(ISession::class),
+                                       $c->get(IUserSession::class),
+                                       $c->get(ITimeFactory::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
                                new TwoFactorMiddleware(
-                                       $c->query(OC\Authentication\TwoFactorAuth\Manager::class),
-                                       $c->query(IUserSession::class),
-                                       $c->query(ISession::class),
-                                       $c->query(IURLGenerator::class),
-                                       $c->query(IControllerMethodReflector::class),
-                                       $c->query(IRequest::class)
+                                       $c->get(OC\Authentication\TwoFactorAuth\Manager::class),
+                                       $c->get(IUserSession::class),
+                                       $c->get(ISession::class),
+                                       $c->get(IURLGenerator::class),
+                                       $c->get(IControllerMethodReflector::class),
+                                       $c->get(IRequest::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
                                new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
-                                       $c->query(IControllerMethodReflector::class),
-                                       $c->query(OC\Security\Bruteforce\Throttler::class),
-                                       $c->query(IRequest::class)
+                                       $c->get(IControllerMethodReflector::class),
+                                       $c->get(OC\Security\Bruteforce\Throttler::class),
+                                       $c->get(IRequest::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
                                new RateLimitingMiddleware(
-                                       $c->query(IRequest::class),
-                                       $c->query(IUserSession::class),
-                                       $c->query(IControllerMethodReflector::class),
-                                       $c->query(OC\Security\RateLimiting\Limiter::class)
+                                       $c->get(IRequest::class),
+                                       $c->get(IUserSession::class),
+                                       $c->get(IControllerMethodReflector::class),
+                                       $c->get(OC\Security\RateLimiting\Limiter::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
                                new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
-                                       $c->query(IRequest::class),
-                                       $c->query(ISession::class),
-                                       $c->query(\OCP\IConfig::class)
+                                       $c->get(IRequest::class),
+                                       $c->get(ISession::class),
+                                       $c->get(\OCP\IConfig::class)
                                )
                        );
                        $dispatcher->registerMiddleware(
-                               $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
+                               $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
                        );
 
                        foreach ($this->middleWares as $middleWare) {
-                               $dispatcher->registerMiddleware($c->query($middleWare));
+                               $dispatcher->registerMiddleware($c->get($middleWare));
                        }
 
                        $dispatcher->registerMiddleware(
                                new SessionMiddleware(
-                                       $c->query(IControllerMethodReflector::class),
-                                       $c->query(ISession::class)
+                                       $c->get(IControllerMethodReflector::class),
+                                       $c->get(ISession::class)
                                )
                        );
                        return $dispatcher;
                });
 
-               $this->registerService(IAppConfig::class, function (SimpleContainer $c) {
+               $this->registerService(IAppConfig::class, function (ContainerInterface $c) {
                        return new OC\AppFramework\Services\AppConfig(
-                               $c->query(IConfig::class),
-                               $c->query('AppName')
+                               $c->get(IConfig::class),
+                               $c->get('AppName')
                        );
                });
-               $this->registerService(IInitialState::class, function (SimpleContainer $c) {
+               $this->registerService(IInitialState::class, function (ContainerInterface $c) {
                        return new OC\AppFramework\Services\InitialState(
-                               $c->query(IInitialStateService::class),
-                               $c->query('AppName')
+                               $c->get(IInitialStateService::class),
+                               $c->get('AppName')
                        );
                });
        }
@@ -396,6 +402,18 @@ class DIContainer extends SimpleContainer implements IAppContainer {
                });
        }
 
+       public function has($id): bool {
+               if (parent::has($id)) {
+                       return true;
+               }
+
+               if ($this->server->has($id, true)) {
+                       return true;
+               }
+
+               return false;
+       }
+
        public function query(string $name, bool $autoload = true) {
                try {
                        return $this->queryNoFallback($name);
@@ -421,14 +439,12 @@ class DIContainer extends SimpleContainer implements IAppContainer {
 
                if ($this->offsetExists($name)) {
                        return parent::query($name);
-               } else {
-                       if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
-                               return parent::query($name);
-                       } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
-                               return parent::query($name);
-                       } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
-                               return parent::query($name);
-                       }
+               } elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
+                       return parent::query($name);
+               } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
+                       return parent::query($name);
+               } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
+                       return parent::query($name);
                }
 
                throw new QueryException('Could not resolve ' . $name . '!' .
index 60f674bbea17f67f10a07e843c8443ed7334edeb..c17578183acfe04a5e0a47a6b93c636724d5bcba 100644 (file)
 
 namespace OC\AppFramework\Utility;
 
+use ArrayAccess;
 use Closure;
 use OCP\AppFramework\QueryException;
 use OCP\IContainer;
 use Pimple\Container;
+use Psr\Container\ContainerInterface;
 use ReflectionClass;
 use ReflectionException;
+use ReflectionParameter;
+use function class_exists;
 
 /**
- * Class SimpleContainer
- *
- * SimpleContainer is a simple implementation of IContainer on basis of Pimple
+ * SimpleContainer is a simple implementation of a container on basis of Pimple
  */
-class SimpleContainer extends Container implements IContainer {
+class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
+
+       /** @var Container */
+       private $container;
+
+       public function __construct() {
+               $this->container = new Container();
+       }
+
+       public function get($id) {
+               return $this->query($id);
+       }
 
+       public function has($id): bool {
+               // If a service is no registered but is an existing class, we can probably load it
+               return isset($this->container[$id]) || class_exists($id);
+       }
 
        /**
         * @param ReflectionClass $class the class to instantiate
@@ -54,45 +71,37 @@ class SimpleContainer extends Container implements IContainer {
                $constructor = $class->getConstructor();
                if ($constructor === null) {
                        return $class->newInstance();
-               } else {
-                       $parameters = [];
-                       foreach ($constructor->getParameters() as $parameter) {
-                               $parameterClass = $parameter->getClass();
+               }
 
-                               // try to find out if it is a class or a simple parameter
-                               if ($parameterClass === null) {
-                                       $resolveName = $parameter->getName();
-                               } else {
-                                       $resolveName = $parameterClass->name;
+               return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) {
+                       $parameterClass = $parameter->getClass();
+
+                       // try to find out if it is a class or a simple parameter
+                       if ($parameterClass === null) {
+                               $resolveName = $parameter->getName();
+                       } else {
+                               $resolveName = $parameterClass->name;
+                       }
+
+                       try {
+                               $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin();
+                               return $this->query($resolveName, !$builtIn);
+                       } catch (QueryException $e) {
+                               // Service not found, use the default value when available
+                               if ($parameter->isDefaultValueAvailable()) {
+                                       return $parameter->getDefaultValue();
                                }
 
-                               try {
-                                       $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin();
-                                       $parameters[] = $this->query($resolveName, !$builtIn);
-                               } catch (QueryException $e) {
-                                       // Service not found, use the default value when available
-                                       if ($parameter->isDefaultValueAvailable()) {
-                                               $parameters[] = $parameter->getDefaultValue();
-                                       } elseif ($parameterClass !== null) {
-                                               $resolveName = $parameter->getName();
-                                               $parameters[] = $this->query($resolveName);
-                                       } else {
-                                               throw $e;
-                                       }
+                               if ($parameterClass !== null) {
+                                       $resolveName = $parameter->getName();
+                                       return $this->query($resolveName);
                                }
+
+                               throw $e;
                        }
-                       return $class->newInstanceArgs($parameters);
-               }
+               }, $constructor->getParameters()));
        }
 
-
-       /**
-        * If a parameter is not registered in the container try to instantiate it
-        * by using reflection to find out how to build the class
-        * @param string $name the class name to resolve
-        * @return \stdClass
-        * @throws QueryException if the class could not be found or instantiated
-        */
        public function resolve($name) {
                $baseMsg = 'Could not resolve ' . $name . '!';
                try {
@@ -110,15 +119,18 @@ class SimpleContainer extends Container implements IContainer {
 
        public function query(string $name, bool $autoload = true) {
                $name = $this->sanitizeName($name);
-               if ($this->offsetExists($name)) {
-                       return $this->offsetGet($name);
-               } elseif ($autoload) {
+               if (isset($this->container[$name])) {
+                       return $this->container[$name];
+               }
+
+               if ($autoload) {
                        $object = $this->resolve($name);
                        $this->registerService($name, function () use ($object) {
                                return $object;
                        });
                        return $object;
                }
+
                throw new QueryException('Could not resolve ' . $name . '!');
        }
 
@@ -140,14 +152,17 @@ class SimpleContainer extends Container implements IContainer {
         * @param bool $shared
         */
        public function registerService($name, Closure $closure, $shared = true) {
+               $wrapped = function () use ($closure) {
+                       return $closure($this);
+               };
                $name = $this->sanitizeName($name);
                if (isset($this[$name])) {
                        unset($this[$name]);
                }
                if ($shared) {
-                       $this[$name] = $closure;
+                       $this[$name] = $wrapped;
                } else {
-                       $this[$name] = parent::factory($closure);
+                       $this[$name] = $this->container->factory($wrapped);
                }
        }
 
@@ -159,8 +174,8 @@ class SimpleContainer extends Container implements IContainer {
         * @param string $target the target that should be resolved instead
         */
        public function registerAlias($alias, $target) {
-               $this->registerService($alias, function (IContainer $container) use ($target) {
-                       return $container->query($target);
+               $this->registerService($alias, function (ContainerInterface $container) use ($target) {
+                       return $container->get($target);
                }, false);
        }
 
@@ -174,4 +189,32 @@ class SimpleContainer extends Container implements IContainer {
                }
                return $name;
        }
+
+       /**
+        * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has
+        */
+       public function offsetExists($id) {
+               return $this->container->offsetExists($id);
+       }
+
+       /**
+        * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
+        */
+       public function offsetGet($id) {
+               return $this->container->offsetGet($id);
+       }
+
+       /**
+        * @deprecated 20.0.0 use \OCP\IContainer::registerService
+        */
+       public function offsetSet($id, $service) {
+               $this->container->offsetSet($id, $service);
+       }
+
+       /**
+        * @deprecated 20.0.0
+        */
+       public function offsetUnset($offset) {
+               $this->container->offsetUnset($offset);
+       }
 }
index 582718cd7c7b717724dd24b8e97207e317c49323..5769953a9e07e70ffc6e3da0281e1826bcdaa9f8 100644 (file)
@@ -139,7 +139,6 @@ use OCA\Theming\ThemingDefaults;
 use OCA\Theming\Util;
 use OCP\Accounts\IAccountManager;
 use OCP\App\IAppManager;
-use OCP\AppFramework\QueryException;
 use OCP\Authentication\LoginCredentials\IStore;
 use OCP\BackgroundJob\IJobList;
 use OCP\Collaboration\AutoComplete\IManager;
@@ -178,7 +177,6 @@ use OCP\IAppConfig;
 use OCP\IAvatarManager;
 use OCP\ICache;
 use OCP\ICacheFactory;
-use OCP\IContainer;
 use OCP\IDateTimeFormatter;
 use OCP\IDateTimeZone;
 use OCP\IDBConnection;
@@ -226,6 +224,8 @@ use OCP\User\Events\UserDeletedEvent;
 use OCP\User\Events\UserLoggedInEvent;
 use OCP\User\Events\UserLoggedInWithCookieEvent;
 use OCP\User\Events\UserLoggedOutEvent;
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\ContainerInterface;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\GenericEvent;
@@ -242,6 +242,7 @@ use OCA\Files_External\Service\BackendService;
  * TODO: hookup all manager classes
  */
 class Server extends ServerContainer implements IServerContainer {
+
        /** @var string */
        private $webRoot;
 
@@ -256,7 +257,10 @@ class Server extends ServerContainer implements IServerContainer {
                // To find out if we are running from CLI or not
                $this->registerParameter('isCLI', \OC::$CLI);
 
-               $this->registerService(\OCP\IServerContainer::class, function (IServerContainer $c) {
+               $this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
+                       return $c;
+               });
+               $this->registerService(\OCP\IServerContainer::class, function (ContainerInterface $c) {
                        return $c;
                });
 
@@ -2232,16 +2236,16 @@ class Server extends ServerContainer implements IServerContainer {
        }
 
        private function registerDeprecatedAlias(string $alias, string $target) {
-               $this->registerService($alias, function (IContainer $container) use ($target, $alias) {
+               $this->registerService($alias, function (ContainerInterface $container) use ($target, $alias) {
                        try {
                                /** @var ILogger $logger */
-                               $logger = $container->query(ILogger::class);
+                               $logger = $container->get(ILogger::class);
                                $logger->debug('The requested alias "' . $alias . '" is depreacted. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', ['app' => 'serverDI']);
-                       } catch (QueryException $e) {
+                       } catch (ContainerExceptionInterface $e) {
                                // Could not get logger. Continue
                        }
 
-                       return $container->query($target);
+                       return $container->get($target);
                }, false);
        }
 }
index effa57a3abfa0a68a2a0c4ba983744cfcd229dc7..eb6bda1e27b5408e173effdb66b0cfabb7a86464 100644 (file)
@@ -31,6 +31,8 @@ use OC\AppFramework\App;
 use OC\AppFramework\DependencyInjection\DIContainer;
 use OC\AppFramework\Utility\SimpleContainer;
 use OCP\AppFramework\QueryException;
+use function explode;
+use function strtolower;
 
 /**
  * Class ServerContainer
@@ -117,19 +119,21 @@ class ServerContainer extends SimpleContainer {
                throw new QueryException();
        }
 
+       public function has($id, bool $noRecursion = false): bool {
+               if (!$noRecursion && ($appContainer = $this->getAppContainerForService($id)) !== null) {
+                       return $appContainer->has($id);
+               }
+
+               return parent::has($id);
+       }
+
        public function query(string $name, bool $autoload = true) {
                $name = $this->sanitizeName($name);
 
-               if (isset($this[$name])) {
-                       return $this[$name];
-               }
-
                // In case the service starts with OCA\ we try to find the service in
                // the apps container first.
-               if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
-                       $segments = explode('\\', $name);
+               if (($appContainer = $this->getAppContainerForService($name)) !== null) {
                        try {
-                               $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
                                return $appContainer->queryNoFallback($name);
                        } catch (QueryException $e) {
                                // Didn't find the service or the respective app container,
@@ -148,4 +152,17 @@ class ServerContainer extends SimpleContainer {
 
                return parent::query($name, $autoload);
        }
+
+       private function getAppContainerForService(string $id): ?DIContainer {
+               if (strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
+                       return null;
+               }
+
+               try {
+                       [,$namespace,] = explode('\\', $id);
+                       return $this->getAppContainer(strtolower($namespace), $namespace);
+               } catch (QueryException $e) {
+                       return null;
+               }
+       }
 }
index 47b26528c6617ceba002af6e4dc8aefa344a8860..ee0bef6a5fad2cf2ec65f4cdf60f71c4f59fd42c 100644 (file)
@@ -26,8 +26,9 @@ declare(strict_types=1);
 namespace OCP\AppFramework\Bootstrap;
 
 use OCP\AppFramework\IAppContainer;
-use OCP\AppFramework\QueryException;
 use OCP\IServerContainer;
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\ContainerInterface;
 use Throwable;
 
 /**
@@ -40,7 +41,7 @@ interface IBootContext {
         *
         * Useful to register and query app-specific services
         *
-        * @return IAppContainer
+        * @return IAppContainer|ContainerInterface
         * @since 20.0.0
         */
        public function getAppContainer(): IAppContainer;
@@ -68,7 +69,7 @@ interface IBootContext {
         * Note: the app container will be queried
         *
         * @param callable $fn
-        * @throws QueryException if at least one of the parameter can't be resolved
+        * @throws ContainerExceptionInterface if at least one of the parameter can't be resolved
         * @throws Throwable any error the function invocation might cause
         * @return mixed|null the return value of the invoked function, if any
         * @since 20.0.0
index b749c9fd7a6e059d6866caa96629b7de088ee2f0..2a3689e7063de6e67498f78a8108ee8a7f947c72 100644 (file)
@@ -34,6 +34,7 @@ use OCP\IContainer;
  *
  * This container interface provides short cuts for app developers to access predefined app service.
  * @since 6.0.0
+ * @deprecated 20.0.0 use \Psr\Container\ContainerInterface
  */
 interface IAppContainer extends IContainer {
 
@@ -41,12 +42,14 @@ interface IAppContainer extends IContainer {
         * used to return the appname of the set application
         * @return string the name of your application
         * @since 6.0.0
+        * @deprecated 20.0.0
         */
        public function getAppName();
 
        /**
         * @return \OCP\IServerContainer
         * @since 6.0.0
+        * @deprecated 20.0.0
         */
        public function getServer();
 
@@ -54,6 +57,7 @@ interface IAppContainer extends IContainer {
         * @param string $middleWare
         * @return boolean
         * @since 6.0.0
+        * @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerMiddleware
         */
        public function registerMiddleWare($middleWare);
 
@@ -62,6 +66,7 @@ interface IAppContainer extends IContainer {
         *
         * @param string $serviceName e.g. 'OCA\Files\Capabilities'
         * @since 8.2.0
+        * @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerCapability
         */
        public function registerCapability($serviceName);
 }
index 1bb03b8f217a1db26e1d50ba0e6150a6ba0fecf1..c4f401532bee4318051a988fd29668965f2c22df 100644 (file)
@@ -1,4 +1,7 @@
 <?php
+
+declare(strict_types=1);
+
 /**
  * @copyright Copyright (c) 2016, ownCloud, Inc.
  *
 namespace OCP\AppFramework;
 
 use Exception;
+use Psr\Container\ContainerExceptionInterface;
 
 /**
  * Class QueryException
  *
+ * The class extends `NotFoundExceptionInterface` since 20.0.0
+ *
  * @package OCP\AppFramework
  * @since 8.1.0
+ * @deprecated 20.0.0 catch \Psr\Container\ContainerExceptionInterface
  */
-class QueryException extends Exception {
+class QueryException extends Exception implements ContainerExceptionInterface {
 }
index ccfe81534ec37fd76b280e9a6821e2a6aefe0a2e..929d374023921bdbe6fff21f453843fbeb037c6d 100644 (file)
@@ -37,6 +37,8 @@ namespace OCP;
 
 use Closure;
 use OCP\AppFramework\QueryException;
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\ContainerInterface;
 
 /**
  * Class IContainer
@@ -45,8 +47,9 @@ use OCP\AppFramework\QueryException;
  *
  * @package OCP
  * @since 6.0.0
+ * @deprecated 20.0.0 use \Psr\Container\ContainerInterface
  */
-interface IContainer {
+interface IContainer extends ContainerInterface {
 
        /**
         * If a parameter is not registered in the container try to instantiate it
@@ -54,6 +57,8 @@ interface IContainer {
         * @param string $name the class name to resolve
         * @return \stdClass
         * @since 8.2.0
+        * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
+        * @throws ContainerExceptionInterface if the class could not be found or instantiated
         * @throws QueryException if the class could not be found or instantiated
         */
        public function resolve($name);
@@ -64,8 +69,10 @@ interface IContainer {
         * @param string $name
         * @param bool $autoload Should we try to autoload the service. If we are trying to resolve built in types this makes no sense for example
         * @return mixed
+        * @throws ContainerExceptionInterface if the query could not be resolved
         * @throws QueryException if the query could not be resolved
         * @since 6.0.0
+        * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
         */
        public function query(string $name, bool $autoload = true);
 
@@ -76,6 +83,7 @@ interface IContainer {
         * @param mixed $value
         * @return void
         * @since 6.0.0
+        * @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerParameter
         */
        public function registerParameter($name, $value);
 
@@ -91,6 +99,7 @@ interface IContainer {
         * @param bool $shared
         * @return void
         * @since 6.0.0
+        * @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerService
         */
        public function registerService($name, Closure $closure, $shared = true);
 
@@ -101,6 +110,7 @@ interface IContainer {
         * @param string $alias the alias that should be registered
         * @param string $target the target that should be resolved instead
         * @since 8.2.0
+        * @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerServiceAlias
         */
        public function registerAlias($alias, $target);
 }
index 6d698e423e795c54a6f4fe12e21407b51ee0ff2b..084c161b47c12cf66a54c33fb62fb64a1a075dbd 100644 (file)
@@ -60,6 +60,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  *
  * This container holds all ownCloud services
  * @since 6.0.0
+ * @deprecated 20.0.0 use \Psr\Container\ContainerInterface
  */
 interface IServerContainer extends IContainer {