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