ServerContainer.php 2.4 KB

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