servercontainer.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\DependencyInjection\DIContainer;
  24. use OC\AppFramework\Utility\SimpleContainer;
  25. use OCP\AppFramework\QueryException;
  26. /**
  27. * Class ServerContainer
  28. *
  29. * @package OC
  30. */
  31. class ServerContainer extends SimpleContainer {
  32. /** @var DIContainer[] */
  33. protected $appContainers;
  34. /**
  35. * ServerContainer constructor.
  36. */
  37. public function __construct() {
  38. parent::__construct();
  39. $this->appContainers = [];
  40. }
  41. /**
  42. * @param string $appName
  43. * @param DIContainer $container
  44. */
  45. public function registerAppContainer($appName, DIContainer $container) {
  46. $this->appContainers[$appName] = $container;
  47. }
  48. /**
  49. * @param string $appName
  50. * @return DIContainer
  51. */
  52. public function getAppContainer($appName) {
  53. if (isset($this->appContainers[$appName])) {
  54. return $this->appContainers[$appName];
  55. }
  56. return new DIContainer($appName);
  57. }
  58. /**
  59. * @param string $name name of the service to query for
  60. * @return mixed registered service for the given $name
  61. * @throws QueryException if the query could not be resolved
  62. */
  63. public function query($name) {
  64. $name = $this->sanitizeName($name);
  65. // In case the service starts with OCA\ we try to find the service in
  66. // the apps container first.
  67. if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
  68. $segments = explode('\\', $name);
  69. $appContainer = $this->getAppContainer(strtolower($segments[1]));
  70. try {
  71. return $appContainer->query($name);
  72. } catch (QueryException $e) {
  73. // Didn't find the service in the respective app container,
  74. // ignore it and fall back to the core container.
  75. }
  76. }
  77. return parent::query($name);
  78. }
  79. }