PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/private/servercontainer.php

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