@@ -97,30 +97,30 @@ discard block |
||
| 97 | 97 | public function resolveArguments(ContextFunction $reflection, array $parameters = []): array |
| 98 | 98 | { |
| 99 | 99 | $arguments = []; |
| 100 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 101 | - try { |
|
| 100 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 101 | + try{ |
|
| 102 | 102 | //Information we need to know about argument in order to resolve it's value |
| 103 | 103 | $name = $parameter->getName(); |
| 104 | 104 | $class = $parameter->getClass(); |
| 105 | - } catch (\Throwable $e) { |
|
| 105 | + }catch (\Throwable $e){ |
|
| 106 | 106 | //Possibly invalid class definition or syntax error |
| 107 | 107 | $location = $reflection->getName(); |
| 108 | - if ($reflection instanceof \ReflectionMethod) { |
|
| 108 | + if ($reflection instanceof \ReflectionMethod){ |
|
| 109 | 109 | $location = "{$reflection->getDeclaringClass()->getName()}->{$location}"; |
| 110 | 110 | } |
| 111 | 111 | //Possibly invalid class definition or syntax error |
| 112 | 112 | throw new ContainerException( |
| 113 | - "Unable to resolve `{$parameter->getName()}` in {$location}: " . $e->getMessage(), |
|
| 113 | + "Unable to resolve `{$parameter->getName()}` in {$location}: ".$e->getMessage(), |
|
| 114 | 114 | $e->getCode(), |
| 115 | 115 | $e |
| 116 | 116 | ); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
| 120 | - if ($parameters[$name] instanceof Autowire) { |
|
| 119 | + if (isset($parameters[$name]) && is_object($parameters[$name])){ |
|
| 120 | + if ($parameters[$name] instanceof Autowire){ |
|
| 121 | 121 | //Supplied by user as late dependency |
| 122 | 122 | $arguments[] = $parameters[$name]->resolve($this); |
| 123 | - } else { |
|
| 123 | + }else{ |
|
| 124 | 124 | //Supplied by user as object |
| 125 | 125 | $arguments[] = $parameters[$name]; |
| 126 | 126 | } |
@@ -128,16 +128,16 @@ discard block |
||
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // no declared type or scalar type or array |
| 131 | - if (!isset($class)) { |
|
| 131 | + if (!isset($class)){ |
|
| 132 | 132 | //Provided from outside |
| 133 | - if (\array_key_exists($name, $parameters)) { |
|
| 133 | + if (\array_key_exists($name, $parameters)){ |
|
| 134 | 134 | //Make sure it's properly typed |
| 135 | 135 | $this->assertType($parameter, $reflection, $parameters[$name]); |
| 136 | 136 | $arguments[] = $parameters[$name]; |
| 137 | 137 | continue; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 140 | + if ($parameter->isDefaultValueAvailable()){ |
|
| 141 | 141 | //Default value |
| 142 | 142 | $arguments[] = $parameter->getDefaultValue(); |
| 143 | 143 | continue; |
@@ -147,12 +147,12 @@ discard block |
||
| 147 | 147 | throw new ArgumentException($parameter, $reflection); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - try { |
|
| 150 | + try{ |
|
| 151 | 151 | //Requesting for contextual dependency |
| 152 | 152 | $arguments[] = $this->get($class->getName(), $name); |
| 153 | 153 | continue; |
| 154 | - } catch (AutowireException $e) { |
|
| 155 | - if ($parameter->isOptional()) { |
|
| 154 | + }catch (AutowireException $e){ |
|
| 155 | + if ($parameter->isOptional()){ |
|
| 156 | 156 | //This is optional dependency, skip |
| 157 | 157 | $arguments[] = null; |
| 158 | 158 | continue; |
@@ -177,11 +177,11 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void |
| 179 | 179 | { |
| 180 | - if ($value === null) { |
|
| 180 | + if ($value === null){ |
|
| 181 | 181 | if ( |
| 182 | 182 | !$parameter->isOptional() && |
| 183 | 183 | !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null) |
| 184 | - ) { |
|
| 184 | + ){ |
|
| 185 | 185 | throw new ArgumentException($parameter, $context); |
| 186 | 186 | } |
| 187 | 187 | |
@@ -189,20 +189,20 @@ discard block |
||
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | $type = $parameter->getType(); |
| 192 | - if ($type === null) { |
|
| 192 | + if ($type === null){ |
|
| 193 | 193 | return; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | $typeName = $type->getName(); |
| 197 | - if ($typeName === 'array' && !\is_array($value)) { |
|
| 197 | + if ($typeName === 'array' && !\is_array($value)){ |
|
| 198 | 198 | throw new ArgumentException($parameter, $context); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
| 201 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)){ |
|
| 202 | 202 | throw new ArgumentException($parameter, $context); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
| 205 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)){ |
|
| 206 | 206 | throw new ArgumentException($parameter, $context); |
| 207 | 207 | } |
| 208 | 208 | } |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | public function get($alias, string $context = null) |
| 226 | 226 | { |
| 227 | - if ($alias instanceof Autowire) { |
|
| 227 | + if ($alias instanceof Autowire){ |
|
| 228 | 228 | return $alias->resolve($this); |
| 229 | 229 | } |
| 230 | 230 | |
@@ -240,34 +240,34 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | public function make(string $alias, array $parameters = [], string $context = null) |
| 242 | 242 | { |
| 243 | - if (!isset($this->bindings[$alias])) { |
|
| 243 | + if (!isset($this->bindings[$alias])){ |
|
| 244 | 244 | //No direct instructions how to construct class, make is automatically |
| 245 | 245 | return $this->autowire($alias, $parameters, $context); |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | $binding = $this->bindings[$alias]; |
| 249 | - if (\is_object($binding)) { |
|
| 249 | + if (\is_object($binding)){ |
|
| 250 | 250 | //When binding is instance, assuming singleton |
| 251 | 251 | return $binding; |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | - if (\is_string($binding)) { |
|
| 254 | + if (\is_string($binding)){ |
|
| 255 | 255 | //Binding is pointing to something else |
| 256 | 256 | return $this->make($binding, $parameters, $context); |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | unset($this->bindings[$alias]); |
| 260 | - try { |
|
| 261 | - if ($binding[0] === $alias) { |
|
| 260 | + try{ |
|
| 261 | + if ($binding[0] === $alias){ |
|
| 262 | 262 | $instance = $this->autowire($alias, $parameters, $context); |
| 263 | - } else { |
|
| 263 | + }else{ |
|
| 264 | 264 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 265 | 265 | } |
| 266 | - } finally { |
|
| 266 | + }finally{ |
|
| 267 | 267 | $this->bindings[$alias] = $binding; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - if ($binding[1]) { |
|
| 270 | + if ($binding[1]){ |
|
| 271 | 271 | //Indicates singleton |
| 272 | 272 | $this->bindings[$alias] = $instance; |
| 273 | 273 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | */ |
| 289 | 289 | protected function autowire(string $class, array $parameters, string $context = null) |
| 290 | 290 | { |
| 291 | - if (!\class_exists($class)) { |
|
| 291 | + if (!\class_exists($class)){ |
|
| 292 | 292 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
| 293 | 293 | } |
| 294 | 294 | |
@@ -312,22 +312,22 @@ discard block |
||
| 312 | 312 | */ |
| 313 | 313 | private function createInstance(string $class, array $parameters, string $context = null) |
| 314 | 314 | { |
| 315 | - try { |
|
| 315 | + try{ |
|
| 316 | 316 | $reflection = new \ReflectionClass($class); |
| 317 | - } catch (\ReflectionException $e) { |
|
| 317 | + }catch (\ReflectionException $e){ |
|
| 318 | 318 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | //We have to construct class using external injector when we know exact context |
| 322 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
| 322 | + if ($parameters === [] && $this->checkInjector($reflection)){ |
|
| 323 | 323 | $injector = $this->injectors[$reflection->getName()]; |
| 324 | 324 | |
| 325 | 325 | $instance = null; |
| 326 | - try { |
|
| 326 | + try{ |
|
| 327 | 327 | /** @var InjectorInterface $injectorInstance */ |
| 328 | 328 | $injectorInstance = $this->get($injector); |
| 329 | 329 | |
| 330 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 330 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 331 | 331 | throw new InjectionException( |
| 332 | 332 | \sprintf( |
| 333 | 333 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 341 | - if (!$reflection->isInstance($instance)) { |
|
| 341 | + if (!$reflection->isInstance($instance)){ |
|
| 342 | 342 | throw new InjectionException( |
| 343 | 343 | \sprintf( |
| 344 | 344 | "Invalid injection response for '%s'", |
@@ -346,23 +346,23 @@ discard block |
||
| 346 | 346 | ) |
| 347 | 347 | ); |
| 348 | 348 | } |
| 349 | - } finally { |
|
| 349 | + }finally{ |
|
| 350 | 350 | $this->injectors[$reflection->getName()] = $injector; |
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | return $instance; |
| 354 | 354 | } |
| 355 | 355 | |
| 356 | - if (!$reflection->isInstantiable()) { |
|
| 356 | + if (!$reflection->isInstantiable()){ |
|
| 357 | 357 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | $constructor = $reflection->getConstructor(); |
| 361 | 361 | |
| 362 | - if ($constructor !== null) { |
|
| 362 | + if ($constructor !== null){ |
|
| 363 | 363 | // Using constructor with resolved arguments |
| 364 | 364 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
| 365 | - } else { |
|
| 365 | + }else{ |
|
| 366 | 366 | // No constructor specified |
| 367 | 367 | $instance = $reflection->newInstance(); |
| 368 | 368 | } |
@@ -379,28 +379,28 @@ discard block |
||
| 379 | 379 | private function checkInjector(\ReflectionClass $reflection): bool |
| 380 | 380 | { |
| 381 | 381 | $class = $reflection->getName(); |
| 382 | - if (\array_key_exists($class, $this->injectors)) { |
|
| 382 | + if (\array_key_exists($class, $this->injectors)){ |
|
| 383 | 383 | return $this->injectors[$class] !== null; |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | if ( |
| 387 | 387 | $reflection->implementsInterface(InjectableInterface::class) |
| 388 | 388 | && $reflection->hasConstant('INJECTOR') |
| 389 | - ) { |
|
| 389 | + ){ |
|
| 390 | 390 | $this->injectors[$class] = $reflection->getConstant('INJECTOR'); |
| 391 | 391 | |
| 392 | 392 | return true; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - if (!isset($this->injectorsCache[$class])) { |
|
| 395 | + if (!isset($this->injectorsCache[$class])){ |
|
| 396 | 396 | $this->injectorsCache[$class] = null; |
| 397 | 397 | |
| 398 | 398 | // check interfaces |
| 399 | - foreach ($this->injectors as $target => $injector) { |
|
| 399 | + foreach ($this->injectors as $target => $injector){ |
|
| 400 | 400 | if ( |
| 401 | 401 | \class_exists($target, true) |
| 402 | 402 | && $reflection->isSubclassOf($target) |
| 403 | - ) { |
|
| 403 | + ){ |
|
| 404 | 404 | $this->injectors[$class] = $injector; |
| 405 | 405 | |
| 406 | 406 | return true; |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | if ( |
| 410 | 410 | \interface_exists($target, true) |
| 411 | 411 | && $reflection->implementsInterface($target) |
| 412 | - ) { |
|
| 412 | + ){ |
|
| 413 | 413 | $this->injectors[$class] = $injector; |
| 414 | 414 | |
| 415 | 415 | return true; |
@@ -431,9 +431,9 @@ discard block |
||
| 431 | 431 | private function registerInstance($instance, array $parameters) |
| 432 | 432 | { |
| 433 | 433 | //Declarative singletons (only when class received via direct get) |
| 434 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 434 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
| 435 | 435 | $alias = \get_class($instance); |
| 436 | - if (!isset($this->bindings[$alias])) { |
|
| 436 | + if (!isset($this->bindings[$alias])){ |
|
| 437 | 437 | $this->bindings[$alias] = $instance; |
| 438 | 438 | } |
| 439 | 439 | } |
@@ -457,20 +457,20 @@ discard block |
||
| 457 | 457 | $target, |
| 458 | 458 | array $parameters, |
| 459 | 459 | string $context = null |
| 460 | - ) { |
|
| 461 | - if (\is_string($target)) { |
|
| 460 | + ){ |
|
| 461 | + if (\is_string($target)){ |
|
| 462 | 462 | //Reference |
| 463 | 463 | return $this->make($target, $parameters, $context); |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | - if ($target instanceof Autowire) { |
|
| 466 | + if ($target instanceof Autowire){ |
|
| 467 | 467 | return $target->resolve($this, $parameters); |
| 468 | 468 | } |
| 469 | 469 | |
| 470 | - if ($target instanceof \Closure) { |
|
| 471 | - try { |
|
| 470 | + if ($target instanceof \Closure){ |
|
| 471 | + try{ |
|
| 472 | 472 | $reflection = new \ReflectionFunction($target); |
| 473 | - } catch (\ReflectionException $e) { |
|
| 473 | + }catch (\ReflectionException $e){ |
|
| 474 | 474 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 475 | 475 | } |
| 476 | 476 | |
@@ -480,16 +480,16 @@ discard block |
||
| 480 | 480 | ); |
| 481 | 481 | } |
| 482 | 482 | |
| 483 | - if (is_array($target) && isset($target[1])) { |
|
| 483 | + if (is_array($target) && isset($target[1])){ |
|
| 484 | 484 | //In a form of resolver and method |
| 485 | 485 | [$resolver, $method] = $target; |
| 486 | 486 | |
| 487 | 487 | //Resolver instance (i.e. [ClassName::class, 'method']) |
| 488 | 488 | $resolver = $this->get($resolver); |
| 489 | 489 | |
| 490 | - try { |
|
| 490 | + try{ |
|
| 491 | 491 | $method = new \ReflectionMethod($resolver, $method); |
| 492 | - } catch (\ReflectionException $e) { |
|
| 492 | + }catch (\ReflectionException $e){ |
|
| 493 | 493 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 494 | 494 | } |
| 495 | 495 | |
@@ -511,28 +511,28 @@ discard block |
||
| 511 | 511 | public function runScope(array $bindings, callable $scope) |
| 512 | 512 | { |
| 513 | 513 | $cleanup = $previous = []; |
| 514 | - foreach ($bindings as $alias => $resolver) { |
|
| 515 | - if (isset($this->bindings[$alias])) { |
|
| 514 | + foreach ($bindings as $alias => $resolver){ |
|
| 515 | + if (isset($this->bindings[$alias])){ |
|
| 516 | 516 | $previous[$alias] = $this->bindings[$alias]; |
| 517 | - } else { |
|
| 517 | + }else{ |
|
| 518 | 518 | $cleanup[] = $alias; |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | $this->bind($alias, $resolver); |
| 522 | 522 | } |
| 523 | 523 | |
| 524 | - try { |
|
| 525 | - if (ContainerScope::getContainer() !== $this) { |
|
| 524 | + try{ |
|
| 525 | + if (ContainerScope::getContainer() !== $this){ |
|
| 526 | 526 | return ContainerScope::runScope($this, $scope); |
| 527 | 527 | } |
| 528 | 528 | |
| 529 | 529 | return $scope(); |
| 530 | - } finally { |
|
| 531 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
| 530 | + }finally{ |
|
| 531 | + foreach (\array_reverse($previous) as $alias => $resolver){ |
|
| 532 | 532 | $this->bindings[$alias] = $resolver; |
| 533 | 533 | } |
| 534 | 534 | |
| 535 | - foreach ($cleanup as $alias) { |
|
| 535 | + foreach ($cleanup as $alias){ |
|
| 536 | 536 | unset($this->bindings[$alias]); |
| 537 | 537 | } |
| 538 | 538 | } |
@@ -548,7 +548,7 @@ discard block |
||
| 548 | 548 | */ |
| 549 | 549 | public function bind(string $alias, $resolver): void |
| 550 | 550 | { |
| 551 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
| 551 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire){ |
|
| 552 | 552 | // array means = execute me, false = not singleton |
| 553 | 553 | $this->bindings[$alias] = [$resolver, false]; |
| 554 | 554 | |
@@ -567,7 +567,7 @@ discard block |
||
| 567 | 567 | */ |
| 568 | 568 | public function bindSingleton(string $alias, $resolver): void |
| 569 | 569 | { |
| 570 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
| 570 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire){ |
|
| 571 | 571 | // direct binding to an instance |
| 572 | 572 | $this->bindings[$alias] = $resolver; |
| 573 | 573 | |
@@ -585,11 +585,11 @@ discard block |
||
| 585 | 585 | */ |
| 586 | 586 | public function hasInstance(string $alias): bool |
| 587 | 587 | { |
| 588 | - if (!$this->has($alias)) { |
|
| 588 | + if (!$this->has($alias)){ |
|
| 589 | 589 | return false; |
| 590 | 590 | } |
| 591 | 591 | |
| 592 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
| 592 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])){ |
|
| 593 | 593 | //Checking alias tree |
| 594 | 594 | $alias = $this->bindings[$alias]; |
| 595 | 595 | } |
@@ -97,15 +97,20 @@ discard block |
||
| 97 | 97 | public function resolveArguments(ContextFunction $reflection, array $parameters = []): array |
| 98 | 98 | { |
| 99 | 99 | $arguments = []; |
| 100 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 101 | - try { |
|
| 100 | + foreach ($reflection->getParameters() as $parameter) |
|
| 101 | + { |
|
| 102 | + try |
|
| 103 | + { |
|
| 102 | 104 | //Information we need to know about argument in order to resolve it's value |
| 103 | 105 | $name = $parameter->getName(); |
| 104 | 106 | $class = $parameter->getClass(); |
| 105 | - } catch (\Throwable $e) { |
|
| 107 | + } |
|
| 108 | + catch (\Throwable $e) |
|
| 109 | + { |
|
| 106 | 110 | //Possibly invalid class definition or syntax error |
| 107 | 111 | $location = $reflection->getName(); |
| 108 | - if ($reflection instanceof \ReflectionMethod) { |
|
| 112 | + if ($reflection instanceof \ReflectionMethod) |
|
| 113 | + { |
|
| 109 | 114 | $location = "{$reflection->getDeclaringClass()->getName()}->{$location}"; |
| 110 | 115 | } |
| 111 | 116 | //Possibly invalid class definition or syntax error |
@@ -116,11 +121,15 @@ discard block |
||
| 116 | 121 | ); |
| 117 | 122 | } |
| 118 | 123 | |
| 119 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
| 120 | - if ($parameters[$name] instanceof Autowire) { |
|
| 124 | + if (isset($parameters[$name]) && is_object($parameters[$name])) |
|
| 125 | + { |
|
| 126 | + if ($parameters[$name] instanceof Autowire) |
|
| 127 | + { |
|
| 121 | 128 | //Supplied by user as late dependency |
| 122 | 129 | $arguments[] = $parameters[$name]->resolve($this); |
| 123 | - } else { |
|
| 130 | + } |
|
| 131 | + else |
|
| 132 | + { |
|
| 124 | 133 | //Supplied by user as object |
| 125 | 134 | $arguments[] = $parameters[$name]; |
| 126 | 135 | } |
@@ -128,16 +137,19 @@ discard block |
||
| 128 | 137 | } |
| 129 | 138 | |
| 130 | 139 | // no declared type or scalar type or array |
| 131 | - if (!isset($class)) { |
|
| 140 | + if (!isset($class)) |
|
| 141 | + { |
|
| 132 | 142 | //Provided from outside |
| 133 | - if (\array_key_exists($name, $parameters)) { |
|
| 143 | + if (\array_key_exists($name, $parameters)) |
|
| 144 | + { |
|
| 134 | 145 | //Make sure it's properly typed |
| 135 | 146 | $this->assertType($parameter, $reflection, $parameters[$name]); |
| 136 | 147 | $arguments[] = $parameters[$name]; |
| 137 | 148 | continue; |
| 138 | 149 | } |
| 139 | 150 | |
| 140 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 151 | + if ($parameter->isDefaultValueAvailable()) |
|
| 152 | + { |
|
| 141 | 153 | //Default value |
| 142 | 154 | $arguments[] = $parameter->getDefaultValue(); |
| 143 | 155 | continue; |
@@ -147,12 +159,16 @@ discard block |
||
| 147 | 159 | throw new ArgumentException($parameter, $reflection); |
| 148 | 160 | } |
| 149 | 161 | |
| 150 | - try { |
|
| 162 | + try |
|
| 163 | + { |
|
| 151 | 164 | //Requesting for contextual dependency |
| 152 | 165 | $arguments[] = $this->get($class->getName(), $name); |
| 153 | 166 | continue; |
| 154 | - } catch (AutowireException $e) { |
|
| 155 | - if ($parameter->isOptional()) { |
|
| 167 | + } |
|
| 168 | + catch (AutowireException $e) |
|
| 169 | + { |
|
| 170 | + if ($parameter->isOptional()) |
|
| 171 | + { |
|
| 156 | 172 | //This is optional dependency, skip |
| 157 | 173 | $arguments[] = null; |
| 158 | 174 | continue; |
@@ -177,7 +193,8 @@ discard block |
||
| 177 | 193 | */ |
| 178 | 194 | private function assertType(\ReflectionParameter $parameter, ContextFunction $context, $value): void |
| 179 | 195 | { |
| 180 | - if ($value === null) { |
|
| 196 | + if ($value === null) |
|
| 197 | + { |
|
| 181 | 198 | if ( |
| 182 | 199 | !$parameter->isOptional() && |
| 183 | 200 | !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null) |
@@ -189,20 +206,24 @@ discard block |
||
| 189 | 206 | } |
| 190 | 207 | |
| 191 | 208 | $type = $parameter->getType(); |
| 192 | - if ($type === null) { |
|
| 209 | + if ($type === null) |
|
| 210 | + { |
|
| 193 | 211 | return; |
| 194 | 212 | } |
| 195 | 213 | |
| 196 | 214 | $typeName = $type->getName(); |
| 197 | - if ($typeName === 'array' && !\is_array($value)) { |
|
| 215 | + if ($typeName === 'array' && !\is_array($value)) |
|
| 216 | + { |
|
| 198 | 217 | throw new ArgumentException($parameter, $context); |
| 199 | 218 | } |
| 200 | 219 | |
| 201 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
| 220 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) |
|
| 221 | + { |
|
| 202 | 222 | throw new ArgumentException($parameter, $context); |
| 203 | 223 | } |
| 204 | 224 | |
| 205 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
| 225 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) |
|
| 226 | + { |
|
| 206 | 227 | throw new ArgumentException($parameter, $context); |
| 207 | 228 | } |
| 208 | 229 | } |
@@ -224,7 +245,8 @@ discard block |
||
| 224 | 245 | */ |
| 225 | 246 | public function get($alias, string $context = null) |
| 226 | 247 | { |
| 227 | - if ($alias instanceof Autowire) { |
|
| 248 | + if ($alias instanceof Autowire) |
|
| 249 | + { |
|
| 228 | 250 | return $alias->resolve($this); |
| 229 | 251 | } |
| 230 | 252 | |
@@ -240,34 +262,44 @@ discard block |
||
| 240 | 262 | */ |
| 241 | 263 | public function make(string $alias, array $parameters = [], string $context = null) |
| 242 | 264 | { |
| 243 | - if (!isset($this->bindings[$alias])) { |
|
| 265 | + if (!isset($this->bindings[$alias])) |
|
| 266 | + { |
|
| 244 | 267 | //No direct instructions how to construct class, make is automatically |
| 245 | 268 | return $this->autowire($alias, $parameters, $context); |
| 246 | 269 | } |
| 247 | 270 | |
| 248 | 271 | $binding = $this->bindings[$alias]; |
| 249 | - if (\is_object($binding)) { |
|
| 272 | + if (\is_object($binding)) |
|
| 273 | + { |
|
| 250 | 274 | //When binding is instance, assuming singleton |
| 251 | 275 | return $binding; |
| 252 | 276 | } |
| 253 | 277 | |
| 254 | - if (\is_string($binding)) { |
|
| 278 | + if (\is_string($binding)) |
|
| 279 | + { |
|
| 255 | 280 | //Binding is pointing to something else |
| 256 | 281 | return $this->make($binding, $parameters, $context); |
| 257 | 282 | } |
| 258 | 283 | |
| 259 | 284 | unset($this->bindings[$alias]); |
| 260 | - try { |
|
| 261 | - if ($binding[0] === $alias) { |
|
| 285 | + try |
|
| 286 | + { |
|
| 287 | + if ($binding[0] === $alias) |
|
| 288 | + { |
|
| 262 | 289 | $instance = $this->autowire($alias, $parameters, $context); |
| 263 | - } else { |
|
| 290 | + } |
|
| 291 | + else |
|
| 292 | + { |
|
| 264 | 293 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 265 | 294 | } |
| 266 | - } finally { |
|
| 295 | + } |
|
| 296 | + finally |
|
| 297 | + { |
|
| 267 | 298 | $this->bindings[$alias] = $binding; |
| 268 | 299 | } |
| 269 | 300 | |
| 270 | - if ($binding[1]) { |
|
| 301 | + if ($binding[1]) |
|
| 302 | + { |
|
| 271 | 303 | //Indicates singleton |
| 272 | 304 | $this->bindings[$alias] = $instance; |
| 273 | 305 | } |
@@ -288,7 +320,8 @@ discard block |
||
| 288 | 320 | */ |
| 289 | 321 | protected function autowire(string $class, array $parameters, string $context = null) |
| 290 | 322 | { |
| 291 | - if (!\class_exists($class)) { |
|
| 323 | + if (!\class_exists($class)) |
|
| 324 | + { |
|
| 292 | 325 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
| 293 | 326 | } |
| 294 | 327 | |
@@ -312,22 +345,28 @@ discard block |
||
| 312 | 345 | */ |
| 313 | 346 | private function createInstance(string $class, array $parameters, string $context = null) |
| 314 | 347 | { |
| 315 | - try { |
|
| 348 | + try |
|
| 349 | + { |
|
| 316 | 350 | $reflection = new \ReflectionClass($class); |
| 317 | - } catch (\ReflectionException $e) { |
|
| 351 | + } |
|
| 352 | + catch (\ReflectionException $e) |
|
| 353 | + { |
|
| 318 | 354 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 319 | 355 | } |
| 320 | 356 | |
| 321 | 357 | //We have to construct class using external injector when we know exact context |
| 322 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
| 358 | + if ($parameters === [] && $this->checkInjector($reflection)) |
|
| 359 | + { |
|
| 323 | 360 | $injector = $this->injectors[$reflection->getName()]; |
| 324 | 361 | |
| 325 | 362 | $instance = null; |
| 326 | - try { |
|
| 363 | + try |
|
| 364 | + { |
|
| 327 | 365 | /** @var InjectorInterface $injectorInstance */ |
| 328 | 366 | $injectorInstance = $this->get($injector); |
| 329 | 367 | |
| 330 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 368 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 369 | + { |
|
| 331 | 370 | throw new InjectionException( |
| 332 | 371 | \sprintf( |
| 333 | 372 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -338,7 +377,8 @@ discard block |
||
| 338 | 377 | } |
| 339 | 378 | |
| 340 | 379 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 341 | - if (!$reflection->isInstance($instance)) { |
|
| 380 | + if (!$reflection->isInstance($instance)) |
|
| 381 | + { |
|
| 342 | 382 | throw new InjectionException( |
| 343 | 383 | \sprintf( |
| 344 | 384 | "Invalid injection response for '%s'", |
@@ -346,23 +386,29 @@ discard block |
||
| 346 | 386 | ) |
| 347 | 387 | ); |
| 348 | 388 | } |
| 349 | - } finally { |
|
| 389 | + } |
|
| 390 | + finally |
|
| 391 | + { |
|
| 350 | 392 | $this->injectors[$reflection->getName()] = $injector; |
| 351 | 393 | } |
| 352 | 394 | |
| 353 | 395 | return $instance; |
| 354 | 396 | } |
| 355 | 397 | |
| 356 | - if (!$reflection->isInstantiable()) { |
|
| 398 | + if (!$reflection->isInstantiable()) |
|
| 399 | + { |
|
| 357 | 400 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
| 358 | 401 | } |
| 359 | 402 | |
| 360 | 403 | $constructor = $reflection->getConstructor(); |
| 361 | 404 | |
| 362 | - if ($constructor !== null) { |
|
| 405 | + if ($constructor !== null) |
|
| 406 | + { |
|
| 363 | 407 | // Using constructor with resolved arguments |
| 364 | 408 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
| 365 | - } else { |
|
| 409 | + } |
|
| 410 | + else |
|
| 411 | + { |
|
| 366 | 412 | // No constructor specified |
| 367 | 413 | $instance = $reflection->newInstance(); |
| 368 | 414 | } |
@@ -379,7 +425,8 @@ discard block |
||
| 379 | 425 | private function checkInjector(\ReflectionClass $reflection): bool |
| 380 | 426 | { |
| 381 | 427 | $class = $reflection->getName(); |
| 382 | - if (\array_key_exists($class, $this->injectors)) { |
|
| 428 | + if (\array_key_exists($class, $this->injectors)) |
|
| 429 | + { |
|
| 383 | 430 | return $this->injectors[$class] !== null; |
| 384 | 431 | } |
| 385 | 432 | |
@@ -392,11 +439,13 @@ discard block |
||
| 392 | 439 | return true; |
| 393 | 440 | } |
| 394 | 441 | |
| 395 | - if (!isset($this->injectorsCache[$class])) { |
|
| 442 | + if (!isset($this->injectorsCache[$class])) |
|
| 443 | + { |
|
| 396 | 444 | $this->injectorsCache[$class] = null; |
| 397 | 445 | |
| 398 | 446 | // check interfaces |
| 399 | - foreach ($this->injectors as $target => $injector) { |
|
| 447 | + foreach ($this->injectors as $target => $injector) |
|
| 448 | + { |
|
| 400 | 449 | if ( |
| 401 | 450 | \class_exists($target, true) |
| 402 | 451 | && $reflection->isSubclassOf($target) |
@@ -431,9 +480,11 @@ discard block |
||
| 431 | 480 | private function registerInstance($instance, array $parameters) |
| 432 | 481 | { |
| 433 | 482 | //Declarative singletons (only when class received via direct get) |
| 434 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 483 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
| 484 | + { |
|
| 435 | 485 | $alias = \get_class($instance); |
| 436 | - if (!isset($this->bindings[$alias])) { |
|
| 486 | + if (!isset($this->bindings[$alias])) |
|
| 487 | + { |
|
| 437 | 488 | $this->bindings[$alias] = $instance; |
| 438 | 489 | } |
| 439 | 490 | } |
@@ -458,19 +509,25 @@ discard block |
||
| 458 | 509 | array $parameters, |
| 459 | 510 | string $context = null |
| 460 | 511 | ) { |
| 461 | - if (\is_string($target)) { |
|
| 512 | + if (\is_string($target)) |
|
| 513 | + { |
|
| 462 | 514 | //Reference |
| 463 | 515 | return $this->make($target, $parameters, $context); |
| 464 | 516 | } |
| 465 | 517 | |
| 466 | - if ($target instanceof Autowire) { |
|
| 518 | + if ($target instanceof Autowire) |
|
| 519 | + { |
|
| 467 | 520 | return $target->resolve($this, $parameters); |
| 468 | 521 | } |
| 469 | 522 | |
| 470 | - if ($target instanceof \Closure) { |
|
| 471 | - try { |
|
| 523 | + if ($target instanceof \Closure) |
|
| 524 | + { |
|
| 525 | + try |
|
| 526 | + { |
|
| 472 | 527 | $reflection = new \ReflectionFunction($target); |
| 473 | - } catch (\ReflectionException $e) { |
|
| 528 | + } |
|
| 529 | + catch (\ReflectionException $e) |
|
| 530 | + { |
|
| 474 | 531 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 475 | 532 | } |
| 476 | 533 | |
@@ -480,16 +537,20 @@ discard block |
||
| 480 | 537 | ); |
| 481 | 538 | } |
| 482 | 539 | |
| 483 | - if (is_array($target) && isset($target[1])) { |
|
| 540 | + if (is_array($target) && isset($target[1])) |
|
| 541 | + { |
|
| 484 | 542 | //In a form of resolver and method |
| 485 | 543 | [$resolver, $method] = $target; |
| 486 | 544 | |
| 487 | 545 | //Resolver instance (i.e. [ClassName::class, 'method']) |
| 488 | 546 | $resolver = $this->get($resolver); |
| 489 | 547 | |
| 490 | - try { |
|
| 548 | + try |
|
| 549 | + { |
|
| 491 | 550 | $method = new \ReflectionMethod($resolver, $method); |
| 492 | - } catch (\ReflectionException $e) { |
|
| 551 | + } |
|
| 552 | + catch (\ReflectionException $e) |
|
| 553 | + { |
|
| 493 | 554 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 494 | 555 | } |
| 495 | 556 | |
@@ -511,28 +572,38 @@ discard block |
||
| 511 | 572 | public function runScope(array $bindings, callable $scope) |
| 512 | 573 | { |
| 513 | 574 | $cleanup = $previous = []; |
| 514 | - foreach ($bindings as $alias => $resolver) { |
|
| 515 | - if (isset($this->bindings[$alias])) { |
|
| 575 | + foreach ($bindings as $alias => $resolver) |
|
| 576 | + { |
|
| 577 | + if (isset($this->bindings[$alias])) |
|
| 578 | + { |
|
| 516 | 579 | $previous[$alias] = $this->bindings[$alias]; |
| 517 | - } else { |
|
| 580 | + } |
|
| 581 | + else |
|
| 582 | + { |
|
| 518 | 583 | $cleanup[] = $alias; |
| 519 | 584 | } |
| 520 | 585 | |
| 521 | 586 | $this->bind($alias, $resolver); |
| 522 | 587 | } |
| 523 | 588 | |
| 524 | - try { |
|
| 525 | - if (ContainerScope::getContainer() !== $this) { |
|
| 589 | + try |
|
| 590 | + { |
|
| 591 | + if (ContainerScope::getContainer() !== $this) |
|
| 592 | + { |
|
| 526 | 593 | return ContainerScope::runScope($this, $scope); |
| 527 | 594 | } |
| 528 | 595 | |
| 529 | 596 | return $scope(); |
| 530 | - } finally { |
|
| 531 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
| 597 | + } |
|
| 598 | + finally |
|
| 599 | + { |
|
| 600 | + foreach (\array_reverse($previous) as $alias => $resolver) |
|
| 601 | + { |
|
| 532 | 602 | $this->bindings[$alias] = $resolver; |
| 533 | 603 | } |
| 534 | 604 | |
| 535 | - foreach ($cleanup as $alias) { |
|
| 605 | + foreach ($cleanup as $alias) |
|
| 606 | + { |
|
| 536 | 607 | unset($this->bindings[$alias]); |
| 537 | 608 | } |
| 538 | 609 | } |
@@ -548,7 +619,8 @@ discard block |
||
| 548 | 619 | */ |
| 549 | 620 | public function bind(string $alias, $resolver): void |
| 550 | 621 | { |
| 551 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
| 622 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) |
|
| 623 | + { |
|
| 552 | 624 | // array means = execute me, false = not singleton |
| 553 | 625 | $this->bindings[$alias] = [$resolver, false]; |
| 554 | 626 | |
@@ -567,7 +639,8 @@ discard block |
||
| 567 | 639 | */ |
| 568 | 640 | public function bindSingleton(string $alias, $resolver): void |
| 569 | 641 | { |
| 570 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
| 642 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) |
|
| 643 | + { |
|
| 571 | 644 | // direct binding to an instance |
| 572 | 645 | $this->bindings[$alias] = $resolver; |
| 573 | 646 | |
@@ -585,11 +658,13 @@ discard block |
||
| 585 | 658 | */ |
| 586 | 659 | public function hasInstance(string $alias): bool |
| 587 | 660 | { |
| 588 | - if (!$this->has($alias)) { |
|
| 661 | + if (!$this->has($alias)) |
|
| 662 | + { |
|
| 589 | 663 | return false; |
| 590 | 664 | } |
| 591 | 665 | |
| 592 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
| 666 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) |
|
| 667 | + { |
|
| 593 | 668 | //Checking alias tree |
| 594 | 669 | $alias = $this->bindings[$alias]; |
| 595 | 670 | } |