@@ -45,175 +45,175 @@ |
||
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($id) { |
|
56 | - return $this->query($id); |
|
57 | - } |
|
58 | - |
|
59 | - public function has($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 | - return $this->query($resolveName); |
|
97 | - } |
|
98 | - |
|
99 | - throw $e; |
|
100 | - } |
|
101 | - }, $constructor->getParameters())); |
|
102 | - } |
|
103 | - |
|
104 | - public function resolve($name) { |
|
105 | - $baseMsg = 'Could not resolve ' . $name . '!'; |
|
106 | - try { |
|
107 | - $class = new ReflectionClass($name); |
|
108 | - if ($class->isInstantiable()) { |
|
109 | - return $this->buildClass($class); |
|
110 | - } else { |
|
111 | - throw new QueryException($baseMsg . |
|
112 | - ' Class can not be instantiated'); |
|
113 | - } |
|
114 | - } catch (ReflectionException $e) { |
|
115 | - throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - public function query(string $name, bool $autoload = true) { |
|
120 | - $name = $this->sanitizeName($name); |
|
121 | - if (isset($this->container[$name])) { |
|
122 | - return $this->container[$name]; |
|
123 | - } |
|
124 | - |
|
125 | - if ($autoload) { |
|
126 | - $object = $this->resolve($name); |
|
127 | - $this->registerService($name, function () use ($object) { |
|
128 | - return $object; |
|
129 | - }); |
|
130 | - return $object; |
|
131 | - } |
|
132 | - |
|
133 | - throw new QueryException('Could not resolve ' . $name . '!'); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $name |
|
138 | - * @param mixed $value |
|
139 | - */ |
|
140 | - public function registerParameter($name, $value) { |
|
141 | - $this[$name] = $value; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * The given closure is call the first time the given service is queried. |
|
146 | - * The closure has to return the instance for the given service. |
|
147 | - * Created instance will be cached in case $shared is true. |
|
148 | - * |
|
149 | - * @param string $name name of the service to register another backend for |
|
150 | - * @param Closure $closure the closure to be called on service creation |
|
151 | - * @param bool $shared |
|
152 | - */ |
|
153 | - public function registerService($name, Closure $closure, $shared = true) { |
|
154 | - $wrapped = function () use ($closure) { |
|
155 | - return $closure($this); |
|
156 | - }; |
|
157 | - $name = $this->sanitizeName($name); |
|
158 | - if (isset($this[$name])) { |
|
159 | - unset($this[$name]); |
|
160 | - } |
|
161 | - if ($shared) { |
|
162 | - $this[$name] = $wrapped; |
|
163 | - } else { |
|
164 | - $this[$name] = $this->container->factory($wrapped); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Shortcut for returning a service from a service under a different key, |
|
170 | - * e.g. to tell the container to return a class when queried for an |
|
171 | - * interface |
|
172 | - * @param string $alias the alias that should be registered |
|
173 | - * @param string $target the target that should be resolved instead |
|
174 | - */ |
|
175 | - public function registerAlias($alias, $target) { |
|
176 | - $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
177 | - return $container->get($target); |
|
178 | - }, false); |
|
179 | - } |
|
180 | - |
|
181 | - /* |
|
48 | + /** @var Container */ |
|
49 | + private $container; |
|
50 | + |
|
51 | + public function __construct() { |
|
52 | + $this->container = new Container(); |
|
53 | + } |
|
54 | + |
|
55 | + public function get($id) { |
|
56 | + return $this->query($id); |
|
57 | + } |
|
58 | + |
|
59 | + public function has($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 | + return $this->query($resolveName); |
|
97 | + } |
|
98 | + |
|
99 | + throw $e; |
|
100 | + } |
|
101 | + }, $constructor->getParameters())); |
|
102 | + } |
|
103 | + |
|
104 | + public function resolve($name) { |
|
105 | + $baseMsg = 'Could not resolve ' . $name . '!'; |
|
106 | + try { |
|
107 | + $class = new ReflectionClass($name); |
|
108 | + if ($class->isInstantiable()) { |
|
109 | + return $this->buildClass($class); |
|
110 | + } else { |
|
111 | + throw new QueryException($baseMsg . |
|
112 | + ' Class can not be instantiated'); |
|
113 | + } |
|
114 | + } catch (ReflectionException $e) { |
|
115 | + throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + public function query(string $name, bool $autoload = true) { |
|
120 | + $name = $this->sanitizeName($name); |
|
121 | + if (isset($this->container[$name])) { |
|
122 | + return $this->container[$name]; |
|
123 | + } |
|
124 | + |
|
125 | + if ($autoload) { |
|
126 | + $object = $this->resolve($name); |
|
127 | + $this->registerService($name, function () use ($object) { |
|
128 | + return $object; |
|
129 | + }); |
|
130 | + return $object; |
|
131 | + } |
|
132 | + |
|
133 | + throw new QueryException('Could not resolve ' . $name . '!'); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $name |
|
138 | + * @param mixed $value |
|
139 | + */ |
|
140 | + public function registerParameter($name, $value) { |
|
141 | + $this[$name] = $value; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * The given closure is call the first time the given service is queried. |
|
146 | + * The closure has to return the instance for the given service. |
|
147 | + * Created instance will be cached in case $shared is true. |
|
148 | + * |
|
149 | + * @param string $name name of the service to register another backend for |
|
150 | + * @param Closure $closure the closure to be called on service creation |
|
151 | + * @param bool $shared |
|
152 | + */ |
|
153 | + public function registerService($name, Closure $closure, $shared = true) { |
|
154 | + $wrapped = function () use ($closure) { |
|
155 | + return $closure($this); |
|
156 | + }; |
|
157 | + $name = $this->sanitizeName($name); |
|
158 | + if (isset($this[$name])) { |
|
159 | + unset($this[$name]); |
|
160 | + } |
|
161 | + if ($shared) { |
|
162 | + $this[$name] = $wrapped; |
|
163 | + } else { |
|
164 | + $this[$name] = $this->container->factory($wrapped); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Shortcut for returning a service from a service under a different key, |
|
170 | + * e.g. to tell the container to return a class when queried for an |
|
171 | + * interface |
|
172 | + * @param string $alias the alias that should be registered |
|
173 | + * @param string $target the target that should be resolved instead |
|
174 | + */ |
|
175 | + public function registerAlias($alias, $target) { |
|
176 | + $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
177 | + return $container->get($target); |
|
178 | + }, false); |
|
179 | + } |
|
180 | + |
|
181 | + /* |
|
182 | 182 | * @param string $name |
183 | 183 | * @return string |
184 | 184 | */ |
185 | - protected function sanitizeName($name) { |
|
186 | - if (isset($name[0]) && $name[0] === '\\') { |
|
187 | - return ltrim($name, '\\'); |
|
188 | - } |
|
189 | - return $name; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has |
|
194 | - */ |
|
195 | - public function offsetExists($id) { |
|
196 | - return $this->container->offsetExists($id); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
201 | - */ |
|
202 | - public function offsetGet($id) { |
|
203 | - return $this->container->offsetGet($id); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @deprecated 20.0.0 use \OCP\IContainer::registerService |
|
208 | - */ |
|
209 | - public function offsetSet($id, $service) { |
|
210 | - $this->container->offsetSet($id, $service); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @deprecated 20.0.0 |
|
215 | - */ |
|
216 | - public function offsetUnset($offset) { |
|
217 | - $this->container->offsetUnset($offset); |
|
218 | - } |
|
185 | + protected function sanitizeName($name) { |
|
186 | + if (isset($name[0]) && $name[0] === '\\') { |
|
187 | + return ltrim($name, '\\'); |
|
188 | + } |
|
189 | + return $name; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has |
|
194 | + */ |
|
195 | + public function offsetExists($id) { |
|
196 | + return $this->container->offsetExists($id); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get |
|
201 | + */ |
|
202 | + public function offsetGet($id) { |
|
203 | + return $this->container->offsetGet($id); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @deprecated 20.0.0 use \OCP\IContainer::registerService |
|
208 | + */ |
|
209 | + public function offsetSet($id, $service) { |
|
210 | + $this->container->offsetSet($id, $service); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @deprecated 20.0.0 |
|
215 | + */ |
|
216 | + public function offsetUnset($offset) { |
|
217 | + $this->container->offsetUnset($offset); |
|
218 | + } |
|
219 | 219 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | return $class->newInstance(); |
73 | 73 | } |
74 | 74 | |
75 | - return $class->newInstanceArgs(array_map(function (ReflectionParameter $parameter) { |
|
75 | + return $class->newInstanceArgs(array_map(function(ReflectionParameter $parameter) { |
|
76 | 76 | $parameterType = $parameter->getType(); |
77 | 77 | |
78 | 78 | $resolveName = $parameter->getName(); |
@@ -102,17 +102,17 @@ discard block |
||
102 | 102 | } |
103 | 103 | |
104 | 104 | public function resolve($name) { |
105 | - $baseMsg = 'Could not resolve ' . $name . '!'; |
|
105 | + $baseMsg = 'Could not resolve '.$name.'!'; |
|
106 | 106 | try { |
107 | 107 | $class = new ReflectionClass($name); |
108 | 108 | if ($class->isInstantiable()) { |
109 | 109 | return $this->buildClass($class); |
110 | 110 | } else { |
111 | - throw new QueryException($baseMsg . |
|
111 | + throw new QueryException($baseMsg. |
|
112 | 112 | ' Class can not be instantiated'); |
113 | 113 | } |
114 | 114 | } catch (ReflectionException $e) { |
115 | - throw new QueryException($baseMsg . ' ' . $e->getMessage()); |
|
115 | + throw new QueryException($baseMsg.' '.$e->getMessage()); |
|
116 | 116 | } |
117 | 117 | } |
118 | 118 | |
@@ -124,13 +124,13 @@ discard block |
||
124 | 124 | |
125 | 125 | if ($autoload) { |
126 | 126 | $object = $this->resolve($name); |
127 | - $this->registerService($name, function () use ($object) { |
|
127 | + $this->registerService($name, function() use ($object) { |
|
128 | 128 | return $object; |
129 | 129 | }); |
130 | 130 | return $object; |
131 | 131 | } |
132 | 132 | |
133 | - throw new QueryException('Could not resolve ' . $name . '!'); |
|
133 | + throw new QueryException('Could not resolve '.$name.'!'); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | * @param bool $shared |
152 | 152 | */ |
153 | 153 | public function registerService($name, Closure $closure, $shared = true) { |
154 | - $wrapped = function () use ($closure) { |
|
154 | + $wrapped = function() use ($closure) { |
|
155 | 155 | return $closure($this); |
156 | 156 | }; |
157 | 157 | $name = $this->sanitizeName($name); |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * @param string $target the target that should be resolved instead |
174 | 174 | */ |
175 | 175 | public function registerAlias($alias, $target) { |
176 | - $this->registerService($alias, function (ContainerInterface $container) use ($target) { |
|
176 | + $this->registerService($alias, function(ContainerInterface $container) use ($target) { |
|
177 | 177 | return $container->get($target); |
178 | 178 | }, false); |
179 | 179 | } |