You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleContainer.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\AppFramework\Utility;
  29. use ReflectionClass;
  30. use ReflectionException;
  31. use Closure;
  32. use Pimple\Container;
  33. use OCP\AppFramework\QueryException;
  34. use OCP\IContainer;
  35. /**
  36. * Class SimpleContainer
  37. *
  38. * SimpleContainer is a simple implementation of IContainer on basis of Pimple
  39. */
  40. class SimpleContainer extends Container implements IContainer {
  41. /**
  42. * @param ReflectionClass $class the class to instantiate
  43. * @return \stdClass the created class
  44. * @suppress PhanUndeclaredClassInstanceof
  45. */
  46. private function buildClass(ReflectionClass $class) {
  47. $constructor = $class->getConstructor();
  48. if ($constructor === null) {
  49. return $class->newInstance();
  50. } else {
  51. $parameters = [];
  52. foreach ($constructor->getParameters() as $parameter) {
  53. $parameterClass = $parameter->getClass();
  54. // try to find out if it is a class or a simple parameter
  55. if ($parameterClass === null) {
  56. $resolveName = $parameter->getName();
  57. } else {
  58. $resolveName = $parameterClass->name;
  59. }
  60. try {
  61. $parameters[] = $this->query($resolveName);
  62. } catch (QueryException $e) {
  63. // Service not found, use the default value when available
  64. if ($parameter->isDefaultValueAvailable()) {
  65. $parameters[] = $parameter->getDefaultValue();
  66. } else if ($parameterClass !== null) {
  67. $resolveName = $parameter->getName();
  68. $parameters[] = $this->query($resolveName);
  69. } else {
  70. throw $e;
  71. }
  72. }
  73. }
  74. return $class->newInstanceArgs($parameters);
  75. }
  76. }
  77. /**
  78. * If a parameter is not registered in the container try to instantiate it
  79. * by using reflection to find out how to build the class
  80. * @param string $name the class name to resolve
  81. * @return \stdClass
  82. * @throws QueryException if the class could not be found or instantiated
  83. */
  84. public function resolve($name) {
  85. $baseMsg = 'Could not resolve ' . $name . '!';
  86. try {
  87. $class = new ReflectionClass($name);
  88. if ($class->isInstantiable()) {
  89. return $this->buildClass($class);
  90. } else {
  91. throw new QueryException($baseMsg .
  92. ' Class can not be instantiated');
  93. }
  94. } catch(ReflectionException $e) {
  95. throw new QueryException($baseMsg . ' ' . $e->getMessage());
  96. }
  97. }
  98. /**
  99. * @param string $name name of the service to query for
  100. * @return mixed registered service for the given $name
  101. * @throws QueryException if the query could not be resolved
  102. */
  103. public function query($name) {
  104. $name = $this->sanitizeName($name);
  105. if ($this->offsetExists($name)) {
  106. return $this->offsetGet($name);
  107. } else {
  108. $object = $this->resolve($name);
  109. $this->registerService($name, function () use ($object) {
  110. return $object;
  111. });
  112. return $object;
  113. }
  114. }
  115. /**
  116. * @param string $name
  117. * @param mixed $value
  118. */
  119. public function registerParameter($name, $value) {
  120. $this[$name] = $value;
  121. }
  122. /**
  123. * The given closure is call the first time the given service is queried.
  124. * The closure has to return the instance for the given service.
  125. * Created instance will be cached in case $shared is true.
  126. *
  127. * @param string $name name of the service to register another backend for
  128. * @param Closure $closure the closure to be called on service creation
  129. * @param bool $shared
  130. */
  131. public function registerService($name, Closure $closure, $shared = true) {
  132. $name = $this->sanitizeName($name);
  133. if (isset($this[$name])) {
  134. unset($this[$name]);
  135. }
  136. if ($shared) {
  137. $this[$name] = $closure;
  138. } else {
  139. $this[$name] = parent::factory($closure);
  140. }
  141. }
  142. /**
  143. * Shortcut for returning a service from a service under a different key,
  144. * e.g. to tell the container to return a class when queried for an
  145. * interface
  146. * @param string $alias the alias that should be registered
  147. * @param string $target the target that should be resolved instead
  148. */
  149. public function registerAlias($alias, $target) {
  150. $this->registerService($alias, function (IContainer $container) use ($target) {
  151. return $container->query($target);
  152. }, false);
  153. }
  154. /*
  155. * @param string $name
  156. * @return string
  157. */
  158. protected function sanitizeName($name) {
  159. if (isset($name[0]) && $name[0] === '\\') {
  160. return ltrim($name, '\\');
  161. }
  162. return $name;
  163. }
  164. }