1
0

SimpleContainer.php 6.5 KB

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