@@ -39,155 +39,155 @@ |
||
| 39 | 39 | * @package OC |
| 40 | 40 | */ |
| 41 | 41 | class ServerContainer extends SimpleContainer { |
| 42 | - /** @var DIContainer[] */ |
|
| 43 | - protected $appContainers; |
|
| 44 | - |
|
| 45 | - /** @var string[] */ |
|
| 46 | - protected $hasNoAppContainer; |
|
| 47 | - |
|
| 48 | - /** @var string[] */ |
|
| 49 | - protected $namespaces; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * ServerContainer constructor. |
|
| 53 | - */ |
|
| 54 | - public function __construct() { |
|
| 55 | - parent::__construct(); |
|
| 56 | - $this->appContainers = []; |
|
| 57 | - $this->namespaces = []; |
|
| 58 | - $this->hasNoAppContainer = []; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param string $appName |
|
| 63 | - * @param string $appNamespace |
|
| 64 | - */ |
|
| 65 | - public function registerNamespace(string $appName, string $appNamespace): void { |
|
| 66 | - // Cut of OCA\ and lowercase |
|
| 67 | - $appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1)); |
|
| 68 | - $this->namespaces[$appNamespace] = $appName; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @param string $appName |
|
| 73 | - * @param DIContainer $container |
|
| 74 | - */ |
|
| 75 | - public function registerAppContainer(string $appName, DIContainer $container): void { |
|
| 76 | - $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param string $appName |
|
| 81 | - * @return DIContainer |
|
| 82 | - * @throws QueryException |
|
| 83 | - */ |
|
| 84 | - public function getRegisteredAppContainer(string $appName): DIContainer { |
|
| 85 | - if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) { |
|
| 86 | - return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))]; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - throw new QueryException(); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param string $namespace |
|
| 94 | - * @param string $sensitiveNamespace |
|
| 95 | - * @return DIContainer |
|
| 96 | - * @throws QueryException |
|
| 97 | - */ |
|
| 98 | - protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer { |
|
| 99 | - if (isset($this->appContainers[$namespace])) { |
|
| 100 | - return $this->appContainers[$namespace]; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if (isset($this->namespaces[$namespace])) { |
|
| 104 | - if (!isset($this->hasNoAppContainer[$namespace])) { |
|
| 105 | - $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application'; |
|
| 106 | - if (class_exists($applicationClassName)) { |
|
| 107 | - $app = new $applicationClassName(); |
|
| 108 | - if (isset($this->appContainers[$namespace])) { |
|
| 109 | - $this->appContainers[$namespace]->offsetSet($applicationClassName, $app); |
|
| 110 | - return $this->appContainers[$namespace]; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - $this->hasNoAppContainer[$namespace] = true; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return new DIContainer($this->namespaces[$namespace]); |
|
| 117 | - } |
|
| 118 | - throw new QueryException(); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function has($id, bool $noRecursion = false): bool { |
|
| 122 | - if (!$noRecursion && ($appContainer = $this->getAppContainerForService($id)) !== null) { |
|
| 123 | - return $appContainer->has($id); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return parent::has($id); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @template T |
|
| 131 | - * @param class-string<T>|string $name |
|
| 132 | - * @return T|mixed |
|
| 133 | - * @psalm-template S as class-string<T>|string |
|
| 134 | - * @psalm-param S $name |
|
| 135 | - * @psalm-return (S is class-string<T> ? T : mixed) |
|
| 136 | - * @throws QueryException |
|
| 137 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
| 138 | - */ |
|
| 139 | - public function query(string $name, bool $autoload = true) { |
|
| 140 | - $name = $this->sanitizeName($name); |
|
| 141 | - |
|
| 142 | - if (str_starts_with($name, 'OCA\\')) { |
|
| 143 | - // Skip server container query for app namespace classes |
|
| 144 | - try { |
|
| 145 | - return parent::query($name, false); |
|
| 146 | - } catch (QueryException $e) { |
|
| 147 | - // Continue with general autoloading then |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // In case the service starts with OCA\ we try to find the service in |
|
| 152 | - // the apps container first. |
|
| 153 | - if (($appContainer = $this->getAppContainerForService($name)) !== null) { |
|
| 154 | - try { |
|
| 155 | - return $appContainer->queryNoFallback($name); |
|
| 156 | - } catch (QueryException $e) { |
|
| 157 | - // Didn't find the service or the respective app container |
|
| 158 | - // In this case the service won't be part of the core container, |
|
| 159 | - // so we can throw directly |
|
| 160 | - throw $e; |
|
| 161 | - } |
|
| 162 | - } elseif (str_starts_with($name, 'OC\\Settings\\') && substr_count($name, '\\') >= 3) { |
|
| 163 | - $segments = explode('\\', $name); |
|
| 164 | - try { |
|
| 165 | - $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]); |
|
| 166 | - return $appContainer->queryNoFallback($name); |
|
| 167 | - } catch (QueryException $e) { |
|
| 168 | - // Didn't find the service or the respective app container, |
|
| 169 | - // ignore it and fall back to the core container. |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return parent::query($name, $autoload); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @internal |
|
| 178 | - * @param string $id |
|
| 179 | - * @return DIContainer|null |
|
| 180 | - */ |
|
| 181 | - public function getAppContainerForService(string $id): ?DIContainer { |
|
| 182 | - if (!str_starts_with($id, 'OCA\\') || substr_count($id, '\\') < 2) { |
|
| 183 | - return null; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - try { |
|
| 187 | - [,$namespace,] = explode('\\', $id); |
|
| 188 | - return $this->getAppContainer(strtolower($namespace), $namespace); |
|
| 189 | - } catch (QueryException $e) { |
|
| 190 | - return null; |
|
| 191 | - } |
|
| 192 | - } |
|
| 42 | + /** @var DIContainer[] */ |
|
| 43 | + protected $appContainers; |
|
| 44 | + |
|
| 45 | + /** @var string[] */ |
|
| 46 | + protected $hasNoAppContainer; |
|
| 47 | + |
|
| 48 | + /** @var string[] */ |
|
| 49 | + protected $namespaces; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * ServerContainer constructor. |
|
| 53 | + */ |
|
| 54 | + public function __construct() { |
|
| 55 | + parent::__construct(); |
|
| 56 | + $this->appContainers = []; |
|
| 57 | + $this->namespaces = []; |
|
| 58 | + $this->hasNoAppContainer = []; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param string $appName |
|
| 63 | + * @param string $appNamespace |
|
| 64 | + */ |
|
| 65 | + public function registerNamespace(string $appName, string $appNamespace): void { |
|
| 66 | + // Cut of OCA\ and lowercase |
|
| 67 | + $appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1)); |
|
| 68 | + $this->namespaces[$appNamespace] = $appName; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @param string $appName |
|
| 73 | + * @param DIContainer $container |
|
| 74 | + */ |
|
| 75 | + public function registerAppContainer(string $appName, DIContainer $container): void { |
|
| 76 | + $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param string $appName |
|
| 81 | + * @return DIContainer |
|
| 82 | + * @throws QueryException |
|
| 83 | + */ |
|
| 84 | + public function getRegisteredAppContainer(string $appName): DIContainer { |
|
| 85 | + if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) { |
|
| 86 | + return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))]; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + throw new QueryException(); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param string $namespace |
|
| 94 | + * @param string $sensitiveNamespace |
|
| 95 | + * @return DIContainer |
|
| 96 | + * @throws QueryException |
|
| 97 | + */ |
|
| 98 | + protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer { |
|
| 99 | + if (isset($this->appContainers[$namespace])) { |
|
| 100 | + return $this->appContainers[$namespace]; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if (isset($this->namespaces[$namespace])) { |
|
| 104 | + if (!isset($this->hasNoAppContainer[$namespace])) { |
|
| 105 | + $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application'; |
|
| 106 | + if (class_exists($applicationClassName)) { |
|
| 107 | + $app = new $applicationClassName(); |
|
| 108 | + if (isset($this->appContainers[$namespace])) { |
|
| 109 | + $this->appContainers[$namespace]->offsetSet($applicationClassName, $app); |
|
| 110 | + return $this->appContainers[$namespace]; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + $this->hasNoAppContainer[$namespace] = true; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return new DIContainer($this->namespaces[$namespace]); |
|
| 117 | + } |
|
| 118 | + throw new QueryException(); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function has($id, bool $noRecursion = false): bool { |
|
| 122 | + if (!$noRecursion && ($appContainer = $this->getAppContainerForService($id)) !== null) { |
|
| 123 | + return $appContainer->has($id); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return parent::has($id); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @template T |
|
| 131 | + * @param class-string<T>|string $name |
|
| 132 | + * @return T|mixed |
|
| 133 | + * @psalm-template S as class-string<T>|string |
|
| 134 | + * @psalm-param S $name |
|
| 135 | + * @psalm-return (S is class-string<T> ? T : mixed) |
|
| 136 | + * @throws QueryException |
|
| 137 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
| 138 | + */ |
|
| 139 | + public function query(string $name, bool $autoload = true) { |
|
| 140 | + $name = $this->sanitizeName($name); |
|
| 141 | + |
|
| 142 | + if (str_starts_with($name, 'OCA\\')) { |
|
| 143 | + // Skip server container query for app namespace classes |
|
| 144 | + try { |
|
| 145 | + return parent::query($name, false); |
|
| 146 | + } catch (QueryException $e) { |
|
| 147 | + // Continue with general autoloading then |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // In case the service starts with OCA\ we try to find the service in |
|
| 152 | + // the apps container first. |
|
| 153 | + if (($appContainer = $this->getAppContainerForService($name)) !== null) { |
|
| 154 | + try { |
|
| 155 | + return $appContainer->queryNoFallback($name); |
|
| 156 | + } catch (QueryException $e) { |
|
| 157 | + // Didn't find the service or the respective app container |
|
| 158 | + // In this case the service won't be part of the core container, |
|
| 159 | + // so we can throw directly |
|
| 160 | + throw $e; |
|
| 161 | + } |
|
| 162 | + } elseif (str_starts_with($name, 'OC\\Settings\\') && substr_count($name, '\\') >= 3) { |
|
| 163 | + $segments = explode('\\', $name); |
|
| 164 | + try { |
|
| 165 | + $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]); |
|
| 166 | + return $appContainer->queryNoFallback($name); |
|
| 167 | + } catch (QueryException $e) { |
|
| 168 | + // Didn't find the service or the respective app container, |
|
| 169 | + // ignore it and fall back to the core container. |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return parent::query($name, $autoload); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @internal |
|
| 178 | + * @param string $id |
|
| 179 | + * @return DIContainer|null |
|
| 180 | + */ |
|
| 181 | + public function getAppContainerForService(string $id): ?DIContainer { |
|
| 182 | + if (!str_starts_with($id, 'OCA\\') || substr_count($id, '\\') < 2) { |
|
| 183 | + return null; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + try { |
|
| 187 | + [,$namespace,] = explode('\\', $id); |
|
| 188 | + return $this->getAppContainer(strtolower($namespace), $namespace); |
|
| 189 | + } catch (QueryException $e) { |
|
| 190 | + return null; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | 193 | } |