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