Total Complexity | 77 |
Total Lines | 465 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Container extends NetteContainer implements FactoryInterface |
||
50 | { |
||
51 | /** @var object[] service name => instance */ |
||
52 | private $instances = []; |
||
53 | |||
54 | /** @var array circular reference detector */ |
||
55 | private $creating; |
||
56 | |||
57 | /** @var array */ |
||
58 | private $methods; |
||
59 | |||
60 | /** |
||
61 | * Provide psr container interface in order to proxy get and has requests. |
||
62 | * |
||
63 | * @param array $params |
||
64 | */ |
||
65 | public function __construct(array $params = []) |
||
66 | { |
||
67 | $this->parameters = $params; |
||
68 | $this->methods = $this->getServiceMethods(\get_class_methods($this)); |
||
69 | |||
70 | parent::__construct($params); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function has($id) |
||
77 | { |
||
78 | return $this->hasService($id); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | * |
||
84 | * @return object |
||
85 | */ |
||
86 | public function get($id) |
||
87 | { |
||
88 | try { |
||
89 | return $this->make($id); |
||
90 | } catch (Throwable $e) { |
||
91 | throw new NotFoundServiceException(\sprintf('Service [%s] is not found in container', $id), 0, $e); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function getParameter(string $name) |
||
99 | { |
||
100 | return Builder::arrayGet($this->parameters, $name); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function addService(string $name, $service) |
||
107 | { |
||
108 | $service = null === $service ? $name : $service; |
||
109 | $name = $this->aliases[$name] ?? $name; |
||
110 | |||
111 | // Create an instancer |
||
112 | if (\is_string($service) && \class_exists($service)) { |
||
113 | $service = $this->createInstance($service); |
||
114 | } |
||
115 | |||
116 | // Report exception if name already exists. |
||
117 | if (isset($this->instances[$name])) { |
||
118 | throw new ContainerResolutionException("Service [$name] already exists."); |
||
119 | } |
||
120 | |||
121 | if (!\is_object($service)) { |
||
122 | throw new ContainerResolutionException( |
||
123 | \sprintf("Service '%s' must be a object, %s given.", $name, \gettype($name)) |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | // Resolving the closure of the service to return it's type hint or class. |
||
128 | $type = $this->parseServiceType($service); |
||
129 | |||
130 | // Resolving wiring so we could call the service parent classes and interfaces. |
||
131 | if (!$service instanceof Closure) { |
||
132 | $this->resolveWiring($name, $type); |
||
133 | } |
||
134 | |||
135 | // Resolving the method calls. |
||
136 | $this->resolveMethod($name, self::getMethodName($name), $type); |
||
137 | |||
138 | if ($service instanceof Closure) { |
||
139 | // Get the method binding for the given method. |
||
140 | $this->methods[self::getMethodName($name)] = $service; |
||
141 | $this->types[$name] = $type; |
||
142 | } else { |
||
143 | $this->instances[$name] = $service; |
||
144 | } |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function removeService(string $name): void |
||
153 | { |
||
154 | $name = $this->aliases[$name] ?? $name; |
||
155 | unset($this->instances[$name]); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function getService(string $name) |
||
162 | { |
||
163 | if (!isset($this->instances[$name])) { |
||
164 | if (isset($this->aliases[$name])) { |
||
165 | return $this->getService($this->aliases[$name]); |
||
166 | } |
||
167 | $this->instances[$name] = $this->createService($name); |
||
168 | } |
||
169 | |||
170 | return $this->instances[$name]; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function getServiceType(string $name): string |
||
177 | { |
||
178 | $method = self::getMethodName($name); |
||
179 | |||
180 | if (isset($this->aliases[$name])) { |
||
181 | return $this->getServiceType($this->aliases[$name]); |
||
182 | } |
||
183 | |||
184 | if (isset($this->types[$name])) { |
||
185 | return $this->types[$name]; |
||
186 | } |
||
187 | |||
188 | if ($this->hasMethodBinding($method)) { |
||
189 | /** @var ReflectionMethod $type */ |
||
190 | $type = $this->parseBindMethod([$this, $method]); |
||
191 | |||
192 | return $type ? $type->getName() : ''; |
||
|
|||
193 | } |
||
194 | |||
195 | throw new MissingServiceException("Service '$name' not found."); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function hasService(string $name): bool |
||
202 | { |
||
203 | $name = $this->aliases[$name] ?? $name; |
||
204 | |||
205 | return $this->hasMethodBinding(self::getMethodName($name)) || isset($this->instances[$name]); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function isCreated(string $name): bool |
||
212 | { |
||
213 | if (!$this->hasService($name)) { |
||
214 | throw new MissingServiceException("Service '$name' not found."); |
||
215 | } |
||
216 | $name = $this->aliases[$name] ?? $name; |
||
217 | |||
218 | return isset($this->instances[$name]); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function createService(string $name, array $args = []) |
||
225 | { |
||
226 | $name = $this->aliases[$name] ?? $name; |
||
227 | $method = self::getMethodName($name); |
||
228 | $cb = $this->methods[$method] ?? null; |
||
229 | |||
230 | if (isset($this->creating[$name])) { |
||
231 | throw new ContainerResolutionException( |
||
232 | \sprintf( |
||
233 | 'Circular reference detected for services: %s.', |
||
234 | \implode(', ', \array_keys($this->creating)) |
||
235 | ) |
||
236 | ); |
||
237 | } |
||
238 | |||
239 | if ($cb === null) { |
||
240 | throw new MissingServiceException("Service '$name' not found."); |
||
241 | } |
||
242 | |||
243 | try { |
||
244 | $this->creating[$name] = true; |
||
245 | $service = $cb instanceof Closure ? $cb(...$args) : $this->$method(...$args); |
||
246 | } finally { |
||
247 | unset($this->creating[$name]); |
||
248 | } |
||
249 | |||
250 | if (!\is_object($service)) { |
||
251 | throw new UnexpectedValueException( |
||
252 | "Unable to create service '$name', value returned by " . |
||
253 | ($cb instanceof Closure ? 'closure' : "method $method()") . ' is not object.' |
||
254 | ); |
||
255 | } |
||
256 | |||
257 | return $service; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function getByType(string $type, bool $throw = true) |
||
264 | { |
||
265 | $type = Helpers::normalizeClass($type); |
||
266 | |||
267 | if (!empty($this->wiring[$type][0])) { |
||
268 | if (\count($names = $this->wiring[$type][0]) === 1) { |
||
269 | return $this->getService($names[0]); |
||
270 | } |
||
271 | \natsort($names); |
||
272 | |||
273 | throw new MissingServiceException( |
||
274 | "Multiple services of type $type found: " . \implode(', ', $names) . '.' |
||
275 | ); |
||
276 | } |
||
277 | |||
278 | if ($throw) { |
||
279 | if (!\class_exists($type) && !\interface_exists($type)) { |
||
280 | throw new MissingServiceException( |
||
281 | "Service of type '$type' not found. Check class name because it cannot be found." |
||
282 | ); |
||
283 | } |
||
284 | |||
285 | foreach ($this->methods as $method => $foo) { |
||
286 | $methodType = $this->parseBindMethod([\get_class($this), $method])->getName(); |
||
287 | |||
288 | if (\is_a($methodType, $type, true)) { |
||
289 | throw new MissingServiceException( |
||
290 | "Service of type $type is not autowired or is missing in di › export › types." |
||
291 | ); |
||
292 | } |
||
293 | } |
||
294 | |||
295 | throw new MissingServiceException( |
||
296 | "Service of type $type not found. Did you add it to configuration file?" |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | return null; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function createInstance(string $class, array $args = []) |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * {@inheritdoc} |
||
357 | */ |
||
358 | public function runScope(array $bindings, callable $scope) |
||
359 | { |
||
360 | $cleanup = $previous = []; |
||
361 | |||
362 | foreach ($bindings as $alias => $resolver) { |
||
363 | if ($this->has($alias)) { |
||
364 | $previous[$alias] = $this->get($alias); |
||
365 | |||
366 | continue; |
||
367 | } |
||
368 | |||
369 | $cleanup[] = $alias; |
||
370 | $this->addService($alias, $resolver); |
||
371 | } |
||
372 | |||
373 | try { |
||
374 | return $scope(...[&$this]); |
||
375 | } finally { |
||
376 | foreach (\array_reverse($previous) as $alias => $resolver) { |
||
377 | $this->instances[$alias] = $resolver; |
||
378 | } |
||
379 | |||
380 | foreach ($cleanup as $alias) { |
||
381 | $this->removeService($alias); |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * {@inheritdoc} |
||
388 | */ |
||
389 | public function make(string $alias, ...$parameters) |
||
390 | { |
||
391 | try { |
||
392 | return $this->getService($alias); |
||
393 | } catch (MissingServiceException $e) { |
||
394 | // Allow passing arrays or individual lists of dependencies |
||
395 | if (isset($parameters[0]) && \is_array($parameters[0]) && \count($parameters) === 1) { |
||
396 | $parameters = \array_shift($parameters); |
||
397 | } |
||
398 | |||
399 | //Automatically create instance |
||
400 | if (Validators::isType($alias)) { |
||
401 | try { |
||
402 | $instance = $this->getByType($alias); |
||
403 | } catch (MissingServiceException $e) { |
||
404 | $instance = $this->createInstance($alias, $parameters); |
||
405 | } |
||
406 | |||
407 | $this->callInjects($instance); // Call injectors on the new class instance. |
||
408 | |||
409 | return $instance; |
||
410 | } |
||
411 | } |
||
412 | |||
413 | throw new NotFoundServiceException(\sprintf('Service [%s] is not found in container', $alias)); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * {@inheritdoc} |
||
418 | */ |
||
419 | public function hasMethodBinding($method): bool |
||
420 | { |
||
421 | return isset($this->methods[$method]); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Resolve callable methods. |
||
426 | * |
||
427 | * @param string $abstract |
||
428 | * @param string $concrete |
||
429 | * @param string $type |
||
430 | */ |
||
431 | private function resolveMethod(string $abstract, string $concrete, string $type): void |
||
432 | { |
||
433 | if (!$this->hasMethodBinding($concrete)) { |
||
434 | $this->types[$abstract] = $type; |
||
435 | } |
||
436 | |||
437 | if (($expectedType = $this->getServiceType($abstract)) && !\is_a($type, $expectedType, true)) { |
||
438 | throw new ContainerResolutionException( |
||
439 | "Service '$abstract' must be instance of $expectedType, " . |
||
440 | ($type ? "$type given." : 'add typehint to closure.') |
||
441 | ); |
||
442 | } |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Resolve wiring classes + interfaces. |
||
447 | * |
||
448 | * @param string $name |
||
449 | * @param mixed $class |
||
450 | */ |
||
451 | private function resolveWiring(string $name, $class): void |
||
452 | { |
||
453 | $all = []; |
||
454 | |||
455 | foreach (\class_parents($class) + \class_implements($class) + [$class] as $class) { |
||
456 | $all[$class][] = $name; |
||
457 | } |
||
458 | |||
459 | foreach ($all as $class => $names) { |
||
460 | $this->wiring[$class] = \array_filter([ |
||
461 | \array_diff($names, $this->findByType($class) ?? [], $this->findByTag($class) ?? []), |
||
462 | ]); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Get the method to be bounded. |
||
468 | * |
||
469 | * @param array|string $method |
||
470 | * |
||
471 | * @return null|ReflectionType |
||
472 | */ |
||
473 | private function parseBindMethod($method): ?ReflectionType |
||
474 | { |
||
475 | return Callback::toReflection($method)->getReturnType(); |
||
476 | } |
||
477 | |||
478 | private function autowireArguments(ReflectionFunctionAbstract $function, array $args = []): array |
||
484 | }); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Get the Closure or class to be used when building a type. |
||
489 | * |
||
490 | * @param mixed $abstract |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | private function parseServiceType($abstract): string |
||
495 | { |
||
496 | if ($abstract instanceof Closure) { |
||
497 | /** @var ReflectionFunction $tmp */ |
||
498 | if ($tmp = $this->parseBindMethod($abstract)) { |
||
499 | return $tmp->getName(); |
||
500 | } |
||
501 | |||
502 | return ''; |
||
503 | } |
||
504 | |||
505 | return \get_class($abstract); |
||
506 | } |
||
507 | |||
508 | private function getServiceMethods(?array $methods): array |
||
514 | } |
||
515 | )); |
||
516 | } |
||
517 | } |
||
518 |