SimpleContainer.php 5.0 KB

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