ServerContainer.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC;
  26. use OC\AppFramework\App;
  27. use OC\AppFramework\DependencyInjection\DIContainer;
  28. use OC\AppFramework\Utility\SimpleContainer;
  29. use OCP\AppFramework\QueryException;
  30. use function explode;
  31. use function strtolower;
  32. /**
  33. * Class ServerContainer
  34. *
  35. * @package OC
  36. */
  37. class ServerContainer extends SimpleContainer {
  38. /** @var DIContainer[] */
  39. protected $appContainers;
  40. /** @var string[] */
  41. protected $hasNoAppContainer;
  42. /** @var string[] */
  43. protected $namespaces;
  44. /**
  45. * ServerContainer constructor.
  46. */
  47. public function __construct() {
  48. parent::__construct();
  49. $this->appContainers = [];
  50. $this->namespaces = [];
  51. $this->hasNoAppContainer = [];
  52. }
  53. /**
  54. * @param string $appName
  55. * @param string $appNamespace
  56. */
  57. public function registerNamespace(string $appName, string $appNamespace): void {
  58. // Cut of OCA\ and lowercase
  59. $appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
  60. $this->namespaces[$appNamespace] = $appName;
  61. }
  62. /**
  63. * @param string $appName
  64. * @param DIContainer $container
  65. */
  66. public function registerAppContainer(string $appName, DIContainer $container): void {
  67. $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
  68. }
  69. /**
  70. * @param string $appName
  71. * @return DIContainer
  72. * @throws QueryException
  73. */
  74. public function getRegisteredAppContainer(string $appName): DIContainer {
  75. if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) {
  76. return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))];
  77. }
  78. throw new QueryException();
  79. }
  80. /**
  81. * @param string $namespace
  82. * @param string $sensitiveNamespace
  83. * @return DIContainer
  84. * @throws QueryException
  85. */
  86. protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
  87. if (isset($this->appContainers[$namespace])) {
  88. return $this->appContainers[$namespace];
  89. }
  90. if (isset($this->namespaces[$namespace])) {
  91. if (!isset($this->hasNoAppContainer[$namespace])) {
  92. $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
  93. if (class_exists($applicationClassName)) {
  94. $app = new $applicationClassName();
  95. if (isset($this->appContainers[$namespace])) {
  96. $this->appContainers[$namespace]->offsetSet($applicationClassName, $app);
  97. return $this->appContainers[$namespace];
  98. }
  99. }
  100. $this->hasNoAppContainer[$namespace] = true;
  101. }
  102. return new DIContainer($this->namespaces[$namespace]);
  103. }
  104. throw new QueryException();
  105. }
  106. public function has($id, bool $noRecursion = false): bool {
  107. if (!$noRecursion && ($appContainer = $this->getAppContainerForService($id)) !== null) {
  108. return $appContainer->has($id);
  109. }
  110. return parent::has($id);
  111. }
  112. /**
  113. * @template T
  114. * @param class-string<T>|string $name
  115. * @return T|mixed
  116. * @psalm-template S as class-string<T>|string
  117. * @psalm-param S $name
  118. * @psalm-return (S is class-string<T> ? T : mixed)
  119. * @throws QueryException
  120. * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
  121. */
  122. public function query(string $name, bool $autoload = true) {
  123. $name = $this->sanitizeName($name);
  124. if (str_starts_with($name, 'OCA\\')) {
  125. // Skip server container query for app namespace classes
  126. try {
  127. return parent::query($name, false);
  128. } catch (QueryException $e) {
  129. // Continue with general autoloading then
  130. }
  131. }
  132. // In case the service starts with OCA\ we try to find the service in
  133. // the apps container first.
  134. if (($appContainer = $this->getAppContainerForService($name)) !== null) {
  135. try {
  136. return $appContainer->queryNoFallback($name);
  137. } catch (QueryException $e) {
  138. // Didn't find the service or the respective app container,
  139. // ignore it and fall back to the core container.
  140. }
  141. } elseif (strpos($name, 'OC\\Settings\\') === 0 && substr_count($name, '\\') >= 3) {
  142. $segments = explode('\\', $name);
  143. try {
  144. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  145. return $appContainer->queryNoFallback($name);
  146. } catch (QueryException $e) {
  147. // Didn't find the service or the respective app container,
  148. // ignore it and fall back to the core container.
  149. }
  150. }
  151. return parent::query($name, $autoload);
  152. }
  153. /**
  154. * @internal
  155. * @param string $id
  156. * @return DIContainer|null
  157. */
  158. public function getAppContainerForService(string $id): ?DIContainer {
  159. if (strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
  160. return null;
  161. }
  162. try {
  163. [,$namespace,] = explode('\\', $id);
  164. return $this->getAppContainer(strtolower($namespace), $namespace);
  165. } catch (QueryException $e) {
  166. return null;
  167. }
  168. }
  169. }