You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.0 KiB

3 years ago
  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. * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
  114. */
  115. public function query(string $name, bool $autoload = true) {
  116. $name = $this->sanitizeName($name);
  117. // In case the service starts with OCA\ we try to find the service in
  118. // the apps container first.
  119. if (($appContainer = $this->getAppContainerForService($name)) !== null) {
  120. try {
  121. return $appContainer->queryNoFallback($name);
  122. } catch (QueryException $e) {
  123. // Didn't find the service or the respective app container,
  124. // ignore it and fall back to the core container.
  125. }
  126. } elseif (strpos($name, 'OC\\Settings\\') === 0 && substr_count($name, '\\') >= 3) {
  127. $segments = explode('\\', $name);
  128. try {
  129. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  130. return $appContainer->queryNoFallback($name);
  131. } catch (QueryException $e) {
  132. // Didn't find the service or the respective app container,
  133. // ignore it and fall back to the core container.
  134. }
  135. }
  136. return parent::query($name, $autoload);
  137. }
  138. /**
  139. * @internal
  140. * @param string $id
  141. * @return DIContainer|null
  142. */
  143. public function getAppContainerForService(string $id): ?DIContainer {
  144. if (strpos($id, 'OCA\\') !== 0 || substr_count($id, '\\') < 2) {
  145. return null;
  146. }
  147. try {
  148. [,$namespace,] = explode('\\', $id);
  149. return $this->getAppContainer(strtolower($namespace), $namespace);
  150. } catch (QueryException $e) {
  151. return null;
  152. }
  153. }
  154. }