@@ -45,180 +45,180 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer { |
| 47 | 47 | |
| 48 | - /** @var Container */ |
|
| 49 | - private $container; |
|
| 50 | - |
|
| 51 | - public function __construct() { |
|
| 52 | - $this->container = new Container(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function get(string $id) { |
|
| 56 | - return $this->query($id); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function has(string $id): bool { |
|
| 60 | - // If a service is no registered but is an existing class, we can probably load it |
|
| 61 | - return isset($this->container[$id]) || class_exists($id); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param ReflectionClass $class the class to instantiate |
|
| 66 | - * @return \stdClass the created class |
|
| 67 | - * @suppress PhanUndeclaredClassInstanceof |
|
| 68 | - */ |
|
| 69 | - private function buildClass(ReflectionClass $class) { |
|
| 70 | - $constructor = $class->getConstructor(); |
|
| 71 | - if ($constructor === null) { |
|
| 72 | - return $class->newInstance(); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) { |
|
| 76 | - $parameterType = $parameter->getType(); |
|
| 77 | - |
|
| 78 | - $resolveName = $parameter->getName(); |
|
| 79 | - |
|
| 80 | - // try to find out if it is a class or a simple parameter |
|
| 81 | - if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
| 82 | - $resolveName = $parameterType->getName(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - try { |
|
| 86 | - $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin(); |
|
| 87 | - return $this->query($resolveName, !$builtIn); |
|
| 88 | - } catch (QueryException $e) { |
|
| 89 | - // Service not found, use the default value when available |
|
| 90 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 91 | - return $parameter->getDefaultValue(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
| 95 | - $resolveName = $parameter->getName(); |
|
| 96 | - try { |
|
| 97 | - return $this->query($resolveName); |
|
| 98 | - } catch (QueryException $e2) { |
|
| 99 | - // don't lose the error we got while trying to query by type |
|
| 100 | - throw new QueryException($e2->getMessage(), (int) $e2->getCode(), $e); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - throw $e; |
|
| 105 | - } |
|
| 106 | - }, $constructor->getParameters())); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function resolve($name) { |
|
| 110 | - $baseMsg = 'Could not resolve ' . $name . '!'; |
|
| 111 | - try { |
|
| 112 | - $class = new ReflectionClass($name); |
|
| 113 | - if ($class->isInstantiable()) { |
|
| 114 | - return $this->buildClass($class); |
|
| 115 | - } else { |
|
| 116 | - throw new QueryException($baseMsg . |
|
| 117 | - ' Class can not be instantiated'); |
|
| 118 | - } |
|
| 119 | - } catch (ReflectionException $e) { |
|
| 120 | - throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - public function query(string $name, bool $autoload = true) { |
|
| 125 | - $name = $this->sanitizeName($name); |
|
| 126 | - if (isset($this->container[$name])) { |
|
| 127 | - return $this->container[$name]; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($autoload) { |
|
| 131 | - $object = $this->resolve($name); |
|
| 132 | - $this->registerService($name, function () use ($object) { |
|
| 133 | - return $object; |
|
| 134 | - }); |
|
| 135 | - return $object; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - throw new QueryException('Could not resolve ' . $name . '!'); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param string $name |
|
| 143 | - * @param mixed $value |
|
| 144 | - */ |
|
| 145 | - public function registerParameter($name, $value) { |
|
| 146 | - $this[$name] = $value; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * The given closure is call the first time the given service is queried. |
|
| 151 | - * The closure has to return the instance for the given service. |
|
| 152 | - * Created instance will be cached in case $shared is true. |
|
| 153 | - * |
|
| 154 | - * @param string $name name of the service to register another backend for |
|
| 155 | - * @param Closure $closure the closure to be called on service creation |
|
| 156 | - * @param bool $shared |
|
| 157 | - */ |
|
| 158 | - public function registerService($name, Closure $closure, $shared = true) { |
|
| 159 | - $wrapped = function () use ($closure) { |
|
| 160 | - return $closure($this); |
|
| 161 | - }; |
|
| 162 | - $name = $this->sanitizeName($name); |
|
| 163 | - if (isset($this[$name])) { |
|
| 164 | - unset($this[$name]); |
|
| 165 | - } |
|
| 166 | - if ($shared) { |
|
| 167 | - $this[$name] = $wrapped; |
|
| 168 | - } else { |
|
| 169 | - $this[$name] = $this->container->factory($wrapped); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Shortcut for returning a service from a service under a different key, |
|
| 175 | - * e.g. to tell the container to return a class when queried for an |
|
| 176 | - * interface |
|
| 177 | - * @param string $alias the alias that should be registered |
|
| 178 | - * @param string $target the target that should be resolved instead |
|
| 179 | - */ |
|
| 180 | - public function registerAlias($alias, $target) { |
|
| 181 | - $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
| 182 | - return $container->get($target); |
|
| 183 | - }, false); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /* |
|
| 48 | + /** @var Container */ |
|
| 49 | + private $container; |
|
| 50 | + |
|
| 51 | + public function __construct() { |
|
| 52 | + $this->container = new Container(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function get(string $id) { |
|
| 56 | + return $this->query($id); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function has(string $id): bool { |
|
| 60 | + // If a service is no registered but is an existing class, we can probably load it |
|
| 61 | + return isset($this->container[$id]) || class_exists($id); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param ReflectionClass $class the class to instantiate |
|
| 66 | + * @return \stdClass the created class |
|
| 67 | + * @suppress PhanUndeclaredClassInstanceof |
|
| 68 | + */ |
|
| 69 | + private function buildClass(ReflectionClass $class) { |
|
| 70 | + $constructor = $class->getConstructor(); |
|
| 71 | + if ($constructor === null) { |
|
| 72 | + return $class->newInstance(); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) { |
|
| 76 | + $parameterType = $parameter->getType(); |
|
| 77 | + |
|
| 78 | + $resolveName = $parameter->getName(); |
|
| 79 | + |
|
| 80 | + // try to find out if it is a class or a simple parameter |
|
| 81 | + if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
| 82 | + $resolveName = $parameterType->getName(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + try { |
|
| 86 | + $builtIn = $parameter->hasType() && $parameter->getType()->isBuiltin(); |
|
| 87 | + return $this->query($resolveName, !$builtIn); |
|
| 88 | + } catch (QueryException $e) { |
|
| 89 | + // Service not found, use the default value when available |
|
| 90 | + if ($parameter->isDefaultValueAvailable()) { |
|
| 91 | + return $parameter->getDefaultValue(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if ($parameterType !== null && !$parameterType->isBuiltin()) { |
|
| 95 | + $resolveName = $parameter->getName(); |
|
| 96 | + try { |
|
| 97 | + return $this->query($resolveName); |
|
| 98 | + } catch (QueryException $e2) { |
|
| 99 | + // don't lose the error we got while trying to query by type |
|
| 100 | + throw new QueryException($e2->getMessage(), (int) $e2->getCode(), $e); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + throw $e; |
|
| 105 | + } |
|
| 106 | + }, $constructor->getParameters())); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function resolve($name) { |
|
| 110 | + $baseMsg = 'Could not resolve ' . $name . '!'; |
|
| 111 | + try { |
|
| 112 | + $class = new ReflectionClass($name); |
|
| 113 | + if ($class->isInstantiable()) { |
|
| 114 | + return $this->buildClass($class); |
|
| 115 | + } else { |
|
| 116 | + throw new QueryException($baseMsg . |
|
| 117 | + ' Class can not be instantiated'); |
|
| 118 | + } |
|
| 119 | + } catch (ReflectionException $e) { |
|
| 120 | + throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + public function query(string $name, bool $autoload = true) { |
|
| 125 | + $name = $this->sanitizeName($name); |
|
| 126 | + if (isset($this->container[$name])) { |
|
| 127 | + return $this->container[$name]; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($autoload) { |
|
| 131 | + $object = $this->resolve($name); |
|
| 132 | + $this->registerService($name, function () use ($object) { |
|
| 133 | + return $object; |
|
| 134 | + }); |
|
| 135 | + return $object; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + throw new QueryException('Could not resolve ' . $name . '!'); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param string $name |
|
| 143 | + * @param mixed $value |
|
| 144 | + */ |
|
| 145 | + public function registerParameter($name, $value) { |
|
| 146 | + $this[$name] = $value; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * The given closure is call the first time the given service is queried. |
|
| 151 | + * The closure has to return the instance for the given service. |
|
| 152 | + * Created instance will be cached in case $shared is true. |
|
| 153 | + * |
|
| 154 | + * @param string $name name of the service to register another backend for |
|
| 155 | + * @param Closure $closure the closure to be called on service creation |
|
| 156 | + * @param bool $shared |
|
| 157 | + */ |
|
| 158 | + public function registerService($name, Closure $closure, $shared = true) { |
|
| 159 | + $wrapped = function () use ($closure) { |
|
| 160 | + return $closure($this); |
|
| 161 | + }; |
|
| 162 | + $name = $this->sanitizeName($name); |
|
| 163 | + if (isset($this[$name])) { |
|
| 164 | + unset($this[$name]); |
|
| 165 | + } |
|
| 166 | + if ($shared) { |
|
| 167 | + $this[$name] = $wrapped; |
|
| 168 | + } else { |
|
| 169 | + $this[$name] = $this->container->factory($wrapped); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Shortcut for returning a service from a service under a different key, |
|
| 175 | + * e.g. to tell the container to return a class when queried for an |
|
| 176 | + * interface |
|
| 177 | + * @param string $alias the alias that should be registered |
|
| 178 | + * @param string $target the target that should be resolved instead |
|
| 179 | + */ |
|
| 180 | + public function registerAlias($alias, $target) { |
|
| 181 | + $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
| 182 | + return $container->get($target); |
|
| 183 | + }, false); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /* |
|
| 187 | 187 | * @param string $name |
| 188 | 188 | * @return string |
| 189 | 189 | */ |
| 190 | - protected function sanitizeName($name) { |
|
| 191 | - if (isset($name[0]) && $name[0] === '\\') { |
|
| 192 | - return ltrim($name, '\\'); |
|
| 193 | - } |
|
| 194 | - return $name; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has |
|
| 199 | - */ |
|
| 200 | - public function offsetExists($id) { |
|
| 201 | - return $this->container->offsetExists($id); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
| 206 | - */ |
|
| 207 | - public function offsetGet($id) { |
|
| 208 | - return $this->container->offsetGet($id); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @deprecated 20.0.0 use \OCP\IContainer::registerService |
|
| 213 | - */ |
|
| 214 | - public function offsetSet($id, $service) { |
|
| 215 | - $this->container->offsetSet($id, $service); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @deprecated 20.0.0 |
|
| 220 | - */ |
|
| 221 | - public function offsetUnset($offset) { |
|
| 222 | - $this->container->offsetUnset($offset); |
|
| 223 | - } |
|
| 190 | + protected function sanitizeName($name) { |
|
| 191 | + if (isset($name[0]) && $name[0] === '\\') { |
|
| 192 | + return ltrim($name, '\\'); |
|
| 193 | + } |
|
| 194 | + return $name; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has |
|
| 199 | + */ |
|
| 200 | + public function offsetExists($id) { |
|
| 201 | + return $this->container->offsetExists($id); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
| 206 | + */ |
|
| 207 | + public function offsetGet($id) { |
|
| 208 | + return $this->container->offsetGet($id); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @deprecated 20.0.0 use \OCP\IContainer::registerService |
|
| 213 | + */ |
|
| 214 | + public function offsetSet($id, $service) { |
|
| 215 | + $this->container->offsetSet($id, $service); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @deprecated 20.0.0 |
|
| 220 | + */ |
|
| 221 | + public function offsetUnset($offset) { |
|
| 222 | + $this->container->offsetUnset($offset); |
|
| 223 | + } |
|
| 224 | 224 | } |