123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- declare(strict_types=1);
- namespace OC;
- 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 extends SimpleContainer {
-
- protected $appContainers;
-
- protected $hasNoAppContainer;
-
- protected $namespaces;
-
- public function __construct() {
- parent::__construct();
- $this->appContainers = [];
- $this->namespaces = [];
- $this->hasNoAppContainer = [];
- }
-
- public function registerNamespace(string $appName, string $appNamespace): void {
-
- $appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
- $this->namespaces[$appNamespace] = $appName;
- }
-
- public function registerAppContainer(string $appName, DIContainer $container): void {
- $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
- }
-
- public function getRegisteredAppContainer(string $appName): DIContainer {
- if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) {
- return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))];
- }
- throw new QueryException();
- }
-
- protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
- if (isset($this->appContainers[$namespace])) {
- return $this->appContainers[$namespace];
- }
- if (isset($this->namespaces[$namespace])) {
- if (!isset($this->hasNoAppContainer[$namespace])) {
- $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
- if (class_exists($applicationClassName)) {
- $app = new $applicationClassName();
- if (isset($this->appContainers[$namespace])) {
- $this->appContainers[$namespace]->offsetSet($applicationClassName, $app);
- return $this->appContainers[$namespace];
- }
- }
- $this->hasNoAppContainer[$namespace] = true;
- }
- return new DIContainer($this->namespaces[$namespace]);
- }
- 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 (str_starts_with($name, 'OCA\\')) {
-
- try {
- return parent::query($name, false);
- } catch (QueryException $e) {
-
- }
- }
-
-
- if (($appContainer = $this->getAppContainerForService($name)) !== null) {
- try {
- return $appContainer->queryNoFallback($name);
- } catch (QueryException $e) {
-
-
-
- throw $e;
- }
- } elseif (str_starts_with($name, 'OC\\Settings\\') && substr_count($name, '\\') >= 3) {
- $segments = explode('\\', $name);
- try {
- $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
- return $appContainer->queryNoFallback($name);
- } catch (QueryException $e) {
-
-
- }
- }
- return parent::query($name, $autoload);
- }
-
- public function getAppContainerForService(string $id): ?DIContainer {
- if (!str_starts_with($id, 'OCA\\') || substr_count($id, '\\') < 2) {
- return null;
- }
- try {
- [,$namespace,] = explode('\\', $id);
- return $this->getAppContainer(strtolower($namespace), $namespace);
- } catch (QueryException $e) {
- return null;
- }
- }
- }
|