@@ -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 | |