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.

ServerContainer.php 5.1KB

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