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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC;
  24. use OC\AppFramework\App;
  25. use OC\AppFramework\DependencyInjection\DIContainer;
  26. use OC\AppFramework\Utility\SimpleContainer;
  27. use OCP\AppFramework\QueryException;
  28. /**
  29. * Class ServerContainer
  30. *
  31. * @package OC
  32. */
  33. class ServerContainer extends SimpleContainer {
  34. /** @var DIContainer[] */
  35. protected $appContainers;
  36. /** @var string[] */
  37. protected $hasNoAppContainer;
  38. /** @var string[] */
  39. protected $namespaces;
  40. /**
  41. * ServerContainer constructor.
  42. */
  43. public function __construct() {
  44. parent::__construct();
  45. $this->appContainers = [];
  46. $this->namespaces = [];
  47. $this->hasNoAppContainer = [];
  48. }
  49. /**
  50. * @param string $appName
  51. * @param string $appNamespace
  52. */
  53. public function registerNamespace(string $appName, string $appNamespace): void {
  54. // Cut of OCA\ and lowercase
  55. $appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
  56. $this->namespaces[$appNamespace] = $appName;
  57. }
  58. /**
  59. * @param string $appName
  60. * @param DIContainer $container
  61. */
  62. public function registerAppContainer(string $appName, DIContainer $container): void {
  63. $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
  64. }
  65. /**
  66. * @param string $appName
  67. * @return DIContainer
  68. * @throws QueryException
  69. */
  70. public function getRegisteredAppContainer(string $appName): DIContainer {
  71. if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) {
  72. return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))];
  73. }
  74. throw new QueryException();
  75. }
  76. /**
  77. * @param string $namespace
  78. * @param string $sensitiveNamespace
  79. * @return DIContainer
  80. * @throws QueryException
  81. */
  82. protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
  83. if (isset($this->appContainers[$namespace])) {
  84. return $this->appContainers[$namespace];
  85. }
  86. if (isset($this->namespaces[$namespace])) {
  87. if (!isset($this->hasNoAppContainer[$namespace])) {
  88. $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
  89. if (class_exists($applicationClassName)) {
  90. new $applicationClassName();
  91. if (isset($this->appContainers[$namespace])) {
  92. return $this->appContainers[$namespace];
  93. }
  94. }
  95. $this->hasNoAppContainer[$namespace] = true;
  96. }
  97. return new DIContainer($this->namespaces[$namespace]);
  98. }
  99. throw new QueryException();
  100. }
  101. /**
  102. * @param string $name name of the service to query for
  103. * @return mixed registered service for the given $name
  104. * @throws QueryException if the query could not be resolved
  105. */
  106. public function query($name) {
  107. $name = $this->sanitizeName($name);
  108. if (isset($this[$name])) {
  109. return $this[$name];
  110. }
  111. // In case the service starts with OCA\ we try to find the service in
  112. // the apps container first.
  113. if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
  114. $segments = explode('\\', $name);
  115. try {
  116. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  117. return $appContainer->queryNoFallback($name);
  118. } catch (QueryException $e) {
  119. // Didn't find the service or the respective app container,
  120. // ignore it and fall back to the core container.
  121. }
  122. } else if (strpos($name, 'OC\\Settings\\') === 0 && substr_count($name, '\\') >= 3) {
  123. $segments = explode('\\', $name);
  124. try {
  125. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  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. }
  132. return parent::query($name);
  133. }
  134. }