@@ -13,7 +13,7 @@ |
||
13 | 13 | |
14 | 14 | class UnionTypes |
15 | 15 | { |
16 | - public static function example(SampleClass|TypedClass $example) |
|
16 | + public static function example(SampleClass | TypedClass $example) |
|
17 | 17 | { |
18 | 18 | } |
19 | 19 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | { |
99 | 99 | $arguments = []; |
100 | 100 | |
101 | - foreach ($reflection->getParameters() as $parameter) { |
|
101 | + foreach ($reflection->getParameters() as $parameter){ |
|
102 | 102 | $type = $parameter->getType(); |
103 | 103 | $name = $parameter->getName(); |
104 | 104 | $class = null; |
@@ -107,17 +107,17 @@ discard block |
||
107 | 107 | // Container do not currently support union types. In the future, we |
108 | 108 | // can provide the possibility of autowiring based on priorities (TBD). |
109 | 109 | // |
110 | - if ($type instanceof \ReflectionUnionType) { |
|
110 | + if ($type instanceof \ReflectionUnionType){ |
|
111 | 111 | $error = 'Parameter $%s in %s contain union type hint which cannot be inferred unambiguously'; |
112 | 112 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
113 | 113 | |
114 | 114 | throw new ContainerException($error); |
115 | 115 | } |
116 | 116 | |
117 | - if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) { |
|
118 | - try { |
|
117 | + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()){ |
|
118 | + try{ |
|
119 | 119 | $class = new \ReflectionClass($type->getName()); |
120 | - } catch (\ReflectionException $e) { |
|
120 | + }catch (\ReflectionException $e){ |
|
121 | 121 | $location = $this->getLocationString($reflection); |
122 | 122 | $error = "Unable to resolve `\${$parameter->getName()}` parameter in {$location}: {$e->getMessage()}"; |
123 | 123 | |
@@ -125,11 +125,11 @@ discard block |
||
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
128 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
129 | - if ($parameters[$name] instanceof Autowire) { |
|
128 | + if (isset($parameters[$name]) && is_object($parameters[$name])){ |
|
129 | + if ($parameters[$name] instanceof Autowire){ |
|
130 | 130 | //Supplied by user as late dependency |
131 | 131 | $arguments[] = $parameters[$name]->resolve($this); |
132 | - } else { |
|
132 | + }else{ |
|
133 | 133 | //Supplied by user as object |
134 | 134 | $arguments[] = $parameters[$name]; |
135 | 135 | } |
@@ -137,16 +137,16 @@ discard block |
||
137 | 137 | } |
138 | 138 | |
139 | 139 | // no declared type or scalar type or array |
140 | - if (!isset($class)) { |
|
140 | + if (!isset($class)){ |
|
141 | 141 | //Provided from outside |
142 | - if (\array_key_exists($name, $parameters)) { |
|
142 | + if (\array_key_exists($name, $parameters)){ |
|
143 | 143 | //Make sure it's properly typed |
144 | 144 | $this->assertType($parameter, $reflection, $parameters[$name]); |
145 | 145 | $arguments[] = $parameters[$name]; |
146 | 146 | continue; |
147 | 147 | } |
148 | 148 | |
149 | - if ($parameter->isDefaultValueAvailable()) { |
|
149 | + if ($parameter->isDefaultValueAvailable()){ |
|
150 | 150 | //Default value |
151 | 151 | $arguments[] = $parameter->getDefaultValue(); |
152 | 152 | continue; |
@@ -156,12 +156,12 @@ discard block |
||
156 | 156 | throw new ArgumentException($parameter, $reflection); |
157 | 157 | } |
158 | 158 | |
159 | - try { |
|
159 | + try{ |
|
160 | 160 | //Requesting for contextual dependency |
161 | 161 | $arguments[] = $this->get($class->getName(), $name); |
162 | 162 | continue; |
163 | - } catch (AutowireException $e) { |
|
164 | - if ($parameter->isOptional()) { |
|
163 | + }catch (AutowireException $e){ |
|
164 | + if ($parameter->isOptional()){ |
|
165 | 165 | //This is optional dependency, skip |
166 | 166 | $arguments[] = null; |
167 | 167 | continue; |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | */ |
192 | 192 | public function get($alias, string $context = null) |
193 | 193 | { |
194 | - if ($alias instanceof Autowire) { |
|
194 | + if ($alias instanceof Autowire){ |
|
195 | 195 | return $alias->resolve($this); |
196 | 196 | } |
197 | 197 | |
@@ -207,34 +207,34 @@ discard block |
||
207 | 207 | */ |
208 | 208 | public function make(string $alias, array $parameters = [], string $context = null) |
209 | 209 | { |
210 | - if (!isset($this->bindings[$alias])) { |
|
210 | + if (!isset($this->bindings[$alias])){ |
|
211 | 211 | //No direct instructions how to construct class, make is automatically |
212 | 212 | return $this->autowire($alias, $parameters, $context); |
213 | 213 | } |
214 | 214 | |
215 | 215 | $binding = $this->bindings[$alias]; |
216 | - if (\is_object($binding)) { |
|
216 | + if (\is_object($binding)){ |
|
217 | 217 | //When binding is instance, assuming singleton |
218 | 218 | return $binding; |
219 | 219 | } |
220 | 220 | |
221 | - if (\is_string($binding)) { |
|
221 | + if (\is_string($binding)){ |
|
222 | 222 | //Binding is pointing to something else |
223 | 223 | return $this->make($binding, $parameters, $context); |
224 | 224 | } |
225 | 225 | |
226 | 226 | unset($this->bindings[$alias]); |
227 | - try { |
|
228 | - if ($binding[0] === $alias) { |
|
227 | + try{ |
|
228 | + if ($binding[0] === $alias){ |
|
229 | 229 | $instance = $this->autowire($alias, $parameters, $context); |
230 | - } else { |
|
230 | + }else{ |
|
231 | 231 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
232 | 232 | } |
233 | - } finally { |
|
233 | + }finally{ |
|
234 | 234 | $this->bindings[$alias] = $binding; |
235 | 235 | } |
236 | 236 | |
237 | - if ($binding[1]) { |
|
237 | + if ($binding[1]){ |
|
238 | 238 | //Indicates singleton |
239 | 239 | $this->bindings[$alias] = $instance; |
240 | 240 | } |
@@ -248,28 +248,28 @@ discard block |
||
248 | 248 | public function runScope(array $bindings, callable $scope) |
249 | 249 | { |
250 | 250 | $cleanup = $previous = []; |
251 | - foreach ($bindings as $alias => $resolver) { |
|
252 | - if (isset($this->bindings[$alias])) { |
|
251 | + foreach ($bindings as $alias => $resolver){ |
|
252 | + if (isset($this->bindings[$alias])){ |
|
253 | 253 | $previous[$alias] = $this->bindings[$alias]; |
254 | - } else { |
|
254 | + }else{ |
|
255 | 255 | $cleanup[] = $alias; |
256 | 256 | } |
257 | 257 | |
258 | 258 | $this->bind($alias, $resolver); |
259 | 259 | } |
260 | 260 | |
261 | - try { |
|
262 | - if (ContainerScope::getContainer() !== $this) { |
|
261 | + try{ |
|
262 | + if (ContainerScope::getContainer() !== $this){ |
|
263 | 263 | return ContainerScope::runScope($this, $scope); |
264 | 264 | } |
265 | 265 | |
266 | 266 | return $scope(); |
267 | - } finally { |
|
268 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
267 | + }finally{ |
|
268 | + foreach (\array_reverse($previous) as $alias => $resolver){ |
|
269 | 269 | $this->bindings[$alias] = $resolver; |
270 | 270 | } |
271 | 271 | |
272 | - foreach ($cleanup as $alias) { |
|
272 | + foreach ($cleanup as $alias){ |
|
273 | 273 | unset($this->bindings[$alias]); |
274 | 274 | } |
275 | 275 | } |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | */ |
286 | 286 | public function bind(string $alias, $resolver): void |
287 | 287 | { |
288 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
288 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire){ |
|
289 | 289 | // array means = execute me, false = not singleton |
290 | 290 | $this->bindings[$alias] = [$resolver, false]; |
291 | 291 | |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | */ |
305 | 305 | public function bindSingleton(string $alias, $resolver): void |
306 | 306 | { |
307 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
307 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire){ |
|
308 | 308 | // direct binding to an instance |
309 | 309 | $this->bindings[$alias] = $resolver; |
310 | 310 | |
@@ -322,11 +322,11 @@ discard block |
||
322 | 322 | */ |
323 | 323 | public function hasInstance(string $alias): bool |
324 | 324 | { |
325 | - if (!$this->has($alias)) { |
|
325 | + if (!$this->has($alias)){ |
|
326 | 326 | return false; |
327 | 327 | } |
328 | 328 | |
329 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
329 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])){ |
|
330 | 330 | //Checking alias tree |
331 | 331 | $alias = $this->bindings[$alias]; |
332 | 332 | } |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | */ |
409 | 409 | protected function autowire(string $class, array $parameters, string $context = null) |
410 | 410 | { |
411 | - if (!\class_exists($class)) { |
|
411 | + if (!\class_exists($class)){ |
|
412 | 412 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
413 | 413 | } |
414 | 414 | |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | { |
428 | 428 | $location = $reflection->getName(); |
429 | 429 | |
430 | - if ($reflection instanceof \ReflectionMethod) { |
|
430 | + if ($reflection instanceof \ReflectionMethod){ |
|
431 | 431 | return "{$reflection->getDeclaringClass()->getName()}::{$location}()"; |
432 | 432 | } |
433 | 433 | |
@@ -446,11 +446,11 @@ discard block |
||
446 | 446 | */ |
447 | 447 | private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void |
448 | 448 | { |
449 | - if ($value === null) { |
|
449 | + if ($value === null){ |
|
450 | 450 | if ( |
451 | 451 | !$parameter->isOptional() && |
452 | 452 | !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null) |
453 | - ) { |
|
453 | + ){ |
|
454 | 454 | throw new ArgumentException($parameter, $context); |
455 | 455 | } |
456 | 456 | |
@@ -458,20 +458,20 @@ discard block |
||
458 | 458 | } |
459 | 459 | |
460 | 460 | $type = $parameter->getType(); |
461 | - if ($type === null) { |
|
461 | + if ($type === null){ |
|
462 | 462 | return; |
463 | 463 | } |
464 | 464 | |
465 | 465 | $typeName = $type->getName(); |
466 | - if ($typeName === 'array' && !\is_array($value)) { |
|
466 | + if ($typeName === 'array' && !\is_array($value)){ |
|
467 | 467 | throw new ArgumentException($parameter, $context); |
468 | 468 | } |
469 | 469 | |
470 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
470 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)){ |
|
471 | 471 | throw new ArgumentException($parameter, $context); |
472 | 472 | } |
473 | 473 | |
474 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
474 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)){ |
|
475 | 475 | throw new ArgumentException($parameter, $context); |
476 | 476 | } |
477 | 477 | } |
@@ -489,22 +489,22 @@ discard block |
||
489 | 489 | */ |
490 | 490 | private function createInstance(string $class, array $parameters, string $context = null) |
491 | 491 | { |
492 | - try { |
|
492 | + try{ |
|
493 | 493 | $reflection = new \ReflectionClass($class); |
494 | - } catch (\ReflectionException $e) { |
|
494 | + }catch (\ReflectionException $e){ |
|
495 | 495 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
496 | 496 | } |
497 | 497 | |
498 | 498 | //We have to construct class using external injector when we know exact context |
499 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
499 | + if ($parameters === [] && $this->checkInjector($reflection)){ |
|
500 | 500 | $injector = $this->injectors[$reflection->getName()]; |
501 | 501 | |
502 | 502 | $instance = null; |
503 | - try { |
|
503 | + try{ |
|
504 | 504 | /** @var InjectorInterface $injectorInstance */ |
505 | 505 | $injectorInstance = $this->get($injector); |
506 | 506 | |
507 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
507 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
508 | 508 | throw new InjectionException( |
509 | 509 | \sprintf( |
510 | 510 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -515,7 +515,7 @@ discard block |
||
515 | 515 | } |
516 | 516 | |
517 | 517 | $instance = $injectorInstance->createInjection($reflection, $context); |
518 | - if (!$reflection->isInstance($instance)) { |
|
518 | + if (!$reflection->isInstance($instance)){ |
|
519 | 519 | throw new InjectionException( |
520 | 520 | \sprintf( |
521 | 521 | "Invalid injection response for '%s'", |
@@ -523,23 +523,23 @@ discard block |
||
523 | 523 | ) |
524 | 524 | ); |
525 | 525 | } |
526 | - } finally { |
|
526 | + }finally{ |
|
527 | 527 | $this->injectors[$reflection->getName()] = $injector; |
528 | 528 | } |
529 | 529 | |
530 | 530 | return $instance; |
531 | 531 | } |
532 | 532 | |
533 | - if (!$reflection->isInstantiable()) { |
|
533 | + if (!$reflection->isInstantiable()){ |
|
534 | 534 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
535 | 535 | } |
536 | 536 | |
537 | 537 | $constructor = $reflection->getConstructor(); |
538 | 538 | |
539 | - if ($constructor !== null) { |
|
539 | + if ($constructor !== null){ |
|
540 | 540 | // Using constructor with resolved arguments |
541 | 541 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
542 | - } else { |
|
542 | + }else{ |
|
543 | 543 | // No constructor specified |
544 | 544 | $instance = $reflection->newInstance(); |
545 | 545 | } |
@@ -556,28 +556,28 @@ discard block |
||
556 | 556 | private function checkInjector(\ReflectionClass $reflection): bool |
557 | 557 | { |
558 | 558 | $class = $reflection->getName(); |
559 | - if (\array_key_exists($class, $this->injectors)) { |
|
559 | + if (\array_key_exists($class, $this->injectors)){ |
|
560 | 560 | return $this->injectors[$class] !== null; |
561 | 561 | } |
562 | 562 | |
563 | 563 | if ( |
564 | 564 | $reflection->implementsInterface(InjectableInterface::class) |
565 | 565 | && $reflection->hasConstant('INJECTOR') |
566 | - ) { |
|
566 | + ){ |
|
567 | 567 | $this->injectors[$class] = $reflection->getConstant('INJECTOR'); |
568 | 568 | |
569 | 569 | return true; |
570 | 570 | } |
571 | 571 | |
572 | - if (!isset($this->injectorsCache[$class])) { |
|
572 | + if (!isset($this->injectorsCache[$class])){ |
|
573 | 573 | $this->injectorsCache[$class] = null; |
574 | 574 | |
575 | 575 | // check interfaces |
576 | - foreach ($this->injectors as $target => $injector) { |
|
576 | + foreach ($this->injectors as $target => $injector){ |
|
577 | 577 | if ( |
578 | 578 | \class_exists($target, true) |
579 | 579 | && $reflection->isSubclassOf($target) |
580 | - ) { |
|
580 | + ){ |
|
581 | 581 | $this->injectors[$class] = $injector; |
582 | 582 | |
583 | 583 | return true; |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | if ( |
587 | 587 | \interface_exists($target, true) |
588 | 588 | && $reflection->implementsInterface($target) |
589 | - ) { |
|
589 | + ){ |
|
590 | 590 | $this->injectors[$class] = $injector; |
591 | 591 | |
592 | 592 | return true; |
@@ -608,9 +608,9 @@ discard block |
||
608 | 608 | private function registerInstance($instance, array $parameters) |
609 | 609 | { |
610 | 610 | //Declarative singletons (only when class received via direct get) |
611 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
611 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
612 | 612 | $alias = \get_class($instance); |
613 | - if (!isset($this->bindings[$alias])) { |
|
613 | + if (!isset($this->bindings[$alias])){ |
|
614 | 614 | $this->bindings[$alias] = $instance; |
615 | 615 | } |
616 | 616 | } |
@@ -634,20 +634,20 @@ discard block |
||
634 | 634 | $target, |
635 | 635 | array $parameters, |
636 | 636 | string $context = null |
637 | - ) { |
|
638 | - if (\is_string($target)) { |
|
637 | + ){ |
|
638 | + if (\is_string($target)){ |
|
639 | 639 | //Reference |
640 | 640 | return $this->make($target, $parameters, $context); |
641 | 641 | } |
642 | 642 | |
643 | - if ($target instanceof Autowire) { |
|
643 | + if ($target instanceof Autowire){ |
|
644 | 644 | return $target->resolve($this, $parameters); |
645 | 645 | } |
646 | 646 | |
647 | - if ($target instanceof \Closure) { |
|
648 | - try { |
|
647 | + if ($target instanceof \Closure){ |
|
648 | + try{ |
|
649 | 649 | $reflection = new \ReflectionFunction($target); |
650 | - } catch (\ReflectionException $e) { |
|
650 | + }catch (\ReflectionException $e){ |
|
651 | 651 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
652 | 652 | } |
653 | 653 | |
@@ -657,16 +657,16 @@ discard block |
||
657 | 657 | ); |
658 | 658 | } |
659 | 659 | |
660 | - if (is_array($target) && isset($target[1])) { |
|
660 | + if (is_array($target) && isset($target[1])){ |
|
661 | 661 | //In a form of resolver and method |
662 | 662 | [$resolver, $method] = $target; |
663 | 663 | |
664 | 664 | //Resolver instance (i.e. [ClassName::class, 'method']) |
665 | 665 | $resolver = $this->get($resolver); |
666 | 666 | |
667 | - try { |
|
667 | + try{ |
|
668 | 668 | $method = new \ReflectionMethod($resolver, $method); |
669 | - } catch (\ReflectionException $e) { |
|
669 | + }catch (\ReflectionException $e){ |
|
670 | 670 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
671 | 671 | } |
672 | 672 |
@@ -98,7 +98,8 @@ discard block |
||
98 | 98 | { |
99 | 99 | $arguments = []; |
100 | 100 | |
101 | - foreach ($reflection->getParameters() as $parameter) { |
|
101 | + foreach ($reflection->getParameters() as $parameter) |
|
102 | + { |
|
102 | 103 | $type = $parameter->getType(); |
103 | 104 | $name = $parameter->getName(); |
104 | 105 | $class = null; |
@@ -107,17 +108,22 @@ discard block |
||
107 | 108 | // Container do not currently support union types. In the future, we |
108 | 109 | // can provide the possibility of autowiring based on priorities (TBD). |
109 | 110 | // |
110 | - if ($type instanceof \ReflectionUnionType) { |
|
111 | + if ($type instanceof \ReflectionUnionType) |
|
112 | + { |
|
111 | 113 | $error = 'Parameter $%s in %s contain union type hint which cannot be inferred unambiguously'; |
112 | 114 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
113 | 115 | |
114 | 116 | throw new ContainerException($error); |
115 | 117 | } |
116 | 118 | |
117 | - if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) { |
|
118 | - try { |
|
119 | + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) |
|
120 | + { |
|
121 | + try |
|
122 | + { |
|
119 | 123 | $class = new \ReflectionClass($type->getName()); |
120 | - } catch (\ReflectionException $e) { |
|
124 | + } |
|
125 | + catch (\ReflectionException $e) |
|
126 | + { |
|
121 | 127 | $location = $this->getLocationString($reflection); |
122 | 128 | $error = "Unable to resolve `\${$parameter->getName()}` parameter in {$location}: {$e->getMessage()}"; |
123 | 129 | |
@@ -125,11 +131,15 @@ discard block |
||
125 | 131 | } |
126 | 132 | } |
127 | 133 | |
128 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
129 | - if ($parameters[$name] instanceof Autowire) { |
|
134 | + if (isset($parameters[$name]) && is_object($parameters[$name])) |
|
135 | + { |
|
136 | + if ($parameters[$name] instanceof Autowire) |
|
137 | + { |
|
130 | 138 | //Supplied by user as late dependency |
131 | 139 | $arguments[] = $parameters[$name]->resolve($this); |
132 | - } else { |
|
140 | + } |
|
141 | + else |
|
142 | + { |
|
133 | 143 | //Supplied by user as object |
134 | 144 | $arguments[] = $parameters[$name]; |
135 | 145 | } |
@@ -137,16 +147,19 @@ discard block |
||
137 | 147 | } |
138 | 148 | |
139 | 149 | // no declared type or scalar type or array |
140 | - if (!isset($class)) { |
|
150 | + if (!isset($class)) |
|
151 | + { |
|
141 | 152 | //Provided from outside |
142 | - if (\array_key_exists($name, $parameters)) { |
|
153 | + if (\array_key_exists($name, $parameters)) |
|
154 | + { |
|
143 | 155 | //Make sure it's properly typed |
144 | 156 | $this->assertType($parameter, $reflection, $parameters[$name]); |
145 | 157 | $arguments[] = $parameters[$name]; |
146 | 158 | continue; |
147 | 159 | } |
148 | 160 | |
149 | - if ($parameter->isDefaultValueAvailable()) { |
|
161 | + if ($parameter->isDefaultValueAvailable()) |
|
162 | + { |
|
150 | 163 | //Default value |
151 | 164 | $arguments[] = $parameter->getDefaultValue(); |
152 | 165 | continue; |
@@ -156,12 +169,16 @@ discard block |
||
156 | 169 | throw new ArgumentException($parameter, $reflection); |
157 | 170 | } |
158 | 171 | |
159 | - try { |
|
172 | + try |
|
173 | + { |
|
160 | 174 | //Requesting for contextual dependency |
161 | 175 | $arguments[] = $this->get($class->getName(), $name); |
162 | 176 | continue; |
163 | - } catch (AutowireException $e) { |
|
164 | - if ($parameter->isOptional()) { |
|
177 | + } |
|
178 | + catch (AutowireException $e) |
|
179 | + { |
|
180 | + if ($parameter->isOptional()) |
|
181 | + { |
|
165 | 182 | //This is optional dependency, skip |
166 | 183 | $arguments[] = null; |
167 | 184 | continue; |
@@ -191,7 +208,8 @@ discard block |
||
191 | 208 | */ |
192 | 209 | public function get($alias, string $context = null) |
193 | 210 | { |
194 | - if ($alias instanceof Autowire) { |
|
211 | + if ($alias instanceof Autowire) |
|
212 | + { |
|
195 | 213 | return $alias->resolve($this); |
196 | 214 | } |
197 | 215 | |
@@ -207,34 +225,44 @@ discard block |
||
207 | 225 | */ |
208 | 226 | public function make(string $alias, array $parameters = [], string $context = null) |
209 | 227 | { |
210 | - if (!isset($this->bindings[$alias])) { |
|
228 | + if (!isset($this->bindings[$alias])) |
|
229 | + { |
|
211 | 230 | //No direct instructions how to construct class, make is automatically |
212 | 231 | return $this->autowire($alias, $parameters, $context); |
213 | 232 | } |
214 | 233 | |
215 | 234 | $binding = $this->bindings[$alias]; |
216 | - if (\is_object($binding)) { |
|
235 | + if (\is_object($binding)) |
|
236 | + { |
|
217 | 237 | //When binding is instance, assuming singleton |
218 | 238 | return $binding; |
219 | 239 | } |
220 | 240 | |
221 | - if (\is_string($binding)) { |
|
241 | + if (\is_string($binding)) |
|
242 | + { |
|
222 | 243 | //Binding is pointing to something else |
223 | 244 | return $this->make($binding, $parameters, $context); |
224 | 245 | } |
225 | 246 | |
226 | 247 | unset($this->bindings[$alias]); |
227 | - try { |
|
228 | - if ($binding[0] === $alias) { |
|
248 | + try |
|
249 | + { |
|
250 | + if ($binding[0] === $alias) |
|
251 | + { |
|
229 | 252 | $instance = $this->autowire($alias, $parameters, $context); |
230 | - } else { |
|
253 | + } |
|
254 | + else |
|
255 | + { |
|
231 | 256 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
232 | 257 | } |
233 | - } finally { |
|
258 | + } |
|
259 | + finally |
|
260 | + { |
|
234 | 261 | $this->bindings[$alias] = $binding; |
235 | 262 | } |
236 | 263 | |
237 | - if ($binding[1]) { |
|
264 | + if ($binding[1]) |
|
265 | + { |
|
238 | 266 | //Indicates singleton |
239 | 267 | $this->bindings[$alias] = $instance; |
240 | 268 | } |
@@ -248,28 +276,38 @@ discard block |
||
248 | 276 | public function runScope(array $bindings, callable $scope) |
249 | 277 | { |
250 | 278 | $cleanup = $previous = []; |
251 | - foreach ($bindings as $alias => $resolver) { |
|
252 | - if (isset($this->bindings[$alias])) { |
|
279 | + foreach ($bindings as $alias => $resolver) |
|
280 | + { |
|
281 | + if (isset($this->bindings[$alias])) |
|
282 | + { |
|
253 | 283 | $previous[$alias] = $this->bindings[$alias]; |
254 | - } else { |
|
284 | + } |
|
285 | + else |
|
286 | + { |
|
255 | 287 | $cleanup[] = $alias; |
256 | 288 | } |
257 | 289 | |
258 | 290 | $this->bind($alias, $resolver); |
259 | 291 | } |
260 | 292 | |
261 | - try { |
|
262 | - if (ContainerScope::getContainer() !== $this) { |
|
293 | + try |
|
294 | + { |
|
295 | + if (ContainerScope::getContainer() !== $this) |
|
296 | + { |
|
263 | 297 | return ContainerScope::runScope($this, $scope); |
264 | 298 | } |
265 | 299 | |
266 | 300 | return $scope(); |
267 | - } finally { |
|
268 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
301 | + } |
|
302 | + finally |
|
303 | + { |
|
304 | + foreach (\array_reverse($previous) as $alias => $resolver) |
|
305 | + { |
|
269 | 306 | $this->bindings[$alias] = $resolver; |
270 | 307 | } |
271 | 308 | |
272 | - foreach ($cleanup as $alias) { |
|
309 | + foreach ($cleanup as $alias) |
|
310 | + { |
|
273 | 311 | unset($this->bindings[$alias]); |
274 | 312 | } |
275 | 313 | } |
@@ -285,7 +323,8 @@ discard block |
||
285 | 323 | */ |
286 | 324 | public function bind(string $alias, $resolver): void |
287 | 325 | { |
288 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
326 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) |
|
327 | + { |
|
289 | 328 | // array means = execute me, false = not singleton |
290 | 329 | $this->bindings[$alias] = [$resolver, false]; |
291 | 330 | |
@@ -304,7 +343,8 @@ discard block |
||
304 | 343 | */ |
305 | 344 | public function bindSingleton(string $alias, $resolver): void |
306 | 345 | { |
307 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
346 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) |
|
347 | + { |
|
308 | 348 | // direct binding to an instance |
309 | 349 | $this->bindings[$alias] = $resolver; |
310 | 350 | |
@@ -322,11 +362,13 @@ discard block |
||
322 | 362 | */ |
323 | 363 | public function hasInstance(string $alias): bool |
324 | 364 | { |
325 | - if (!$this->has($alias)) { |
|
365 | + if (!$this->has($alias)) |
|
366 | + { |
|
326 | 367 | return false; |
327 | 368 | } |
328 | 369 | |
329 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
370 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) |
|
371 | + { |
|
330 | 372 | //Checking alias tree |
331 | 373 | $alias = $this->bindings[$alias]; |
332 | 374 | } |
@@ -408,7 +450,8 @@ discard block |
||
408 | 450 | */ |
409 | 451 | protected function autowire(string $class, array $parameters, string $context = null) |
410 | 452 | { |
411 | - if (!\class_exists($class)) { |
|
453 | + if (!\class_exists($class)) |
|
454 | + { |
|
412 | 455 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
413 | 456 | } |
414 | 457 | |
@@ -427,7 +470,8 @@ discard block |
||
427 | 470 | { |
428 | 471 | $location = $reflection->getName(); |
429 | 472 | |
430 | - if ($reflection instanceof \ReflectionMethod) { |
|
473 | + if ($reflection instanceof \ReflectionMethod) |
|
474 | + { |
|
431 | 475 | return "{$reflection->getDeclaringClass()->getName()}::{$location}()"; |
432 | 476 | } |
433 | 477 | |
@@ -446,7 +490,8 @@ discard block |
||
446 | 490 | */ |
447 | 491 | private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void |
448 | 492 | { |
449 | - if ($value === null) { |
|
493 | + if ($value === null) |
|
494 | + { |
|
450 | 495 | if ( |
451 | 496 | !$parameter->isOptional() && |
452 | 497 | !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null) |
@@ -458,20 +503,24 @@ discard block |
||
458 | 503 | } |
459 | 504 | |
460 | 505 | $type = $parameter->getType(); |
461 | - if ($type === null) { |
|
506 | + if ($type === null) |
|
507 | + { |
|
462 | 508 | return; |
463 | 509 | } |
464 | 510 | |
465 | 511 | $typeName = $type->getName(); |
466 | - if ($typeName === 'array' && !\is_array($value)) { |
|
512 | + if ($typeName === 'array' && !\is_array($value)) |
|
513 | + { |
|
467 | 514 | throw new ArgumentException($parameter, $context); |
468 | 515 | } |
469 | 516 | |
470 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
517 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) |
|
518 | + { |
|
471 | 519 | throw new ArgumentException($parameter, $context); |
472 | 520 | } |
473 | 521 | |
474 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
522 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) |
|
523 | + { |
|
475 | 524 | throw new ArgumentException($parameter, $context); |
476 | 525 | } |
477 | 526 | } |
@@ -489,22 +538,28 @@ discard block |
||
489 | 538 | */ |
490 | 539 | private function createInstance(string $class, array $parameters, string $context = null) |
491 | 540 | { |
492 | - try { |
|
541 | + try |
|
542 | + { |
|
493 | 543 | $reflection = new \ReflectionClass($class); |
494 | - } catch (\ReflectionException $e) { |
|
544 | + } |
|
545 | + catch (\ReflectionException $e) |
|
546 | + { |
|
495 | 547 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
496 | 548 | } |
497 | 549 | |
498 | 550 | //We have to construct class using external injector when we know exact context |
499 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
551 | + if ($parameters === [] && $this->checkInjector($reflection)) |
|
552 | + { |
|
500 | 553 | $injector = $this->injectors[$reflection->getName()]; |
501 | 554 | |
502 | 555 | $instance = null; |
503 | - try { |
|
556 | + try |
|
557 | + { |
|
504 | 558 | /** @var InjectorInterface $injectorInstance */ |
505 | 559 | $injectorInstance = $this->get($injector); |
506 | 560 | |
507 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
561 | + if (!$injectorInstance instanceof InjectorInterface) |
|
562 | + { |
|
508 | 563 | throw new InjectionException( |
509 | 564 | \sprintf( |
510 | 565 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -515,7 +570,8 @@ discard block |
||
515 | 570 | } |
516 | 571 | |
517 | 572 | $instance = $injectorInstance->createInjection($reflection, $context); |
518 | - if (!$reflection->isInstance($instance)) { |
|
573 | + if (!$reflection->isInstance($instance)) |
|
574 | + { |
|
519 | 575 | throw new InjectionException( |
520 | 576 | \sprintf( |
521 | 577 | "Invalid injection response for '%s'", |
@@ -523,23 +579,29 @@ discard block |
||
523 | 579 | ) |
524 | 580 | ); |
525 | 581 | } |
526 | - } finally { |
|
582 | + } |
|
583 | + finally |
|
584 | + { |
|
527 | 585 | $this->injectors[$reflection->getName()] = $injector; |
528 | 586 | } |
529 | 587 | |
530 | 588 | return $instance; |
531 | 589 | } |
532 | 590 | |
533 | - if (!$reflection->isInstantiable()) { |
|
591 | + if (!$reflection->isInstantiable()) |
|
592 | + { |
|
534 | 593 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
535 | 594 | } |
536 | 595 | |
537 | 596 | $constructor = $reflection->getConstructor(); |
538 | 597 | |
539 | - if ($constructor !== null) { |
|
598 | + if ($constructor !== null) |
|
599 | + { |
|
540 | 600 | // Using constructor with resolved arguments |
541 | 601 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
542 | - } else { |
|
602 | + } |
|
603 | + else |
|
604 | + { |
|
543 | 605 | // No constructor specified |
544 | 606 | $instance = $reflection->newInstance(); |
545 | 607 | } |
@@ -556,7 +618,8 @@ discard block |
||
556 | 618 | private function checkInjector(\ReflectionClass $reflection): bool |
557 | 619 | { |
558 | 620 | $class = $reflection->getName(); |
559 | - if (\array_key_exists($class, $this->injectors)) { |
|
621 | + if (\array_key_exists($class, $this->injectors)) |
|
622 | + { |
|
560 | 623 | return $this->injectors[$class] !== null; |
561 | 624 | } |
562 | 625 | |
@@ -569,11 +632,13 @@ discard block |
||
569 | 632 | return true; |
570 | 633 | } |
571 | 634 | |
572 | - if (!isset($this->injectorsCache[$class])) { |
|
635 | + if (!isset($this->injectorsCache[$class])) |
|
636 | + { |
|
573 | 637 | $this->injectorsCache[$class] = null; |
574 | 638 | |
575 | 639 | // check interfaces |
576 | - foreach ($this->injectors as $target => $injector) { |
|
640 | + foreach ($this->injectors as $target => $injector) |
|
641 | + { |
|
577 | 642 | if ( |
578 | 643 | \class_exists($target, true) |
579 | 644 | && $reflection->isSubclassOf($target) |
@@ -608,9 +673,11 @@ discard block |
||
608 | 673 | private function registerInstance($instance, array $parameters) |
609 | 674 | { |
610 | 675 | //Declarative singletons (only when class received via direct get) |
611 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
676 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
677 | + { |
|
612 | 678 | $alias = \get_class($instance); |
613 | - if (!isset($this->bindings[$alias])) { |
|
679 | + if (!isset($this->bindings[$alias])) |
|
680 | + { |
|
614 | 681 | $this->bindings[$alias] = $instance; |
615 | 682 | } |
616 | 683 | } |
@@ -635,19 +702,25 @@ discard block |
||
635 | 702 | array $parameters, |
636 | 703 | string $context = null |
637 | 704 | ) { |
638 | - if (\is_string($target)) { |
|
705 | + if (\is_string($target)) |
|
706 | + { |
|
639 | 707 | //Reference |
640 | 708 | return $this->make($target, $parameters, $context); |
641 | 709 | } |
642 | 710 | |
643 | - if ($target instanceof Autowire) { |
|
711 | + if ($target instanceof Autowire) |
|
712 | + { |
|
644 | 713 | return $target->resolve($this, $parameters); |
645 | 714 | } |
646 | 715 | |
647 | - if ($target instanceof \Closure) { |
|
648 | - try { |
|
716 | + if ($target instanceof \Closure) |
|
717 | + { |
|
718 | + try |
|
719 | + { |
|
649 | 720 | $reflection = new \ReflectionFunction($target); |
650 | - } catch (\ReflectionException $e) { |
|
721 | + } |
|
722 | + catch (\ReflectionException $e) |
|
723 | + { |
|
651 | 724 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
652 | 725 | } |
653 | 726 | |
@@ -657,16 +730,20 @@ discard block |
||
657 | 730 | ); |
658 | 731 | } |
659 | 732 | |
660 | - if (is_array($target) && isset($target[1])) { |
|
733 | + if (is_array($target) && isset($target[1])) |
|
734 | + { |
|
661 | 735 | //In a form of resolver and method |
662 | 736 | [$resolver, $method] = $target; |
663 | 737 | |
664 | 738 | //Resolver instance (i.e. [ClassName::class, 'method']) |
665 | 739 | $resolver = $this->get($resolver); |
666 | 740 | |
667 | - try { |
|
741 | + try |
|
742 | + { |
|
668 | 743 | $method = new \ReflectionMethod($resolver, $method); |
669 | - } catch (\ReflectionException $e) { |
|
744 | + } |
|
745 | + catch (\ReflectionException $e) |
|
746 | + { |
|
670 | 747 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
671 | 748 | } |
672 | 749 |