ServerContainer.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. try {
  125. return parent::query($name, false);
  126. } catch (QueryException $e) {
  127. // Continue with general autoloading then
  128. }
  129. // In case the service starts with OCA\ we try to find the service in
  130. // the apps container first.
  131. if (($appContainer = $this->getAppContainerForService($name)) !== null) {
  132. try {
  133. return $appContainer->queryNoFallback($name);
  134. } catch (QueryException $e) {
  135. // Didn't find the service or the respective app container,
  136. // ignore it and fall back to the core container.
  137. }
  138. } elseif (strpos($name, 'OC\\Settings\\') === 0 && substr_count($name, '\\') >= 3) {
  139. $segments = explode('\\', $name);
  140. try {
  141. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  142. return $appContainer->queryNoFallback($name);
  143. } catch (QueryException $e) {
  144. // Didn't find the service or the respective app container,
  145. // ignore it and fall back to the core container.
  146. }
  147. }
  148. return parent::query($name, $autoload);
  149. }
  150. /**
  151. * @internal
  152. * @param string $id
  153. * @return DIContainer|null
  154. */
  155. public function getAppContainerForService(string $id): ?DIContainer {
  156. if (strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
  157. return null;
  158. }
  159. try {
  160. [,$namespace,] = explode('\\', $id);
  161. return $this->getAppContainer(strtolower($namespace), $namespace);
  162. } catch (QueryException $e) {
  163. return null;
  164. }
  165. }
  166. }