ServerContainer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public function query(string $name, bool $autoload = true) {
  102. $name = $this->sanitizeName($name);
  103. if (isset($this[$name])) {
  104. return $this[$name];
  105. }
  106. // In case the service starts with OCA\ we try to find the service in
  107. // the apps container first.
  108. if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
  109. $segments = explode('\\', $name);
  110. try {
  111. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  112. return $appContainer->queryNoFallback($name);
  113. } catch (QueryException $e) {
  114. // Didn't find the service or the respective app container,
  115. // ignore it and fall back to the core container.
  116. }
  117. } else if (strpos($name, 'OC\\Settings\\') === 0 && substr_count($name, '\\') >= 3) {
  118. $segments = explode('\\', $name);
  119. try {
  120. $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
  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. }
  127. return parent::query($name, $autoload);
  128. }
  129. }