ServerContainer.php 2.9 KB

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