@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | $this->assertNull($this->app->getEnvironment()->get('FIRED')); |
| 44 | 44 | |
| 45 | 45 | $w->shouldReceive('receive')->once()->with( |
| 46 | - \Mockery::on(function (&$context) { |
|
| 46 | + \Mockery::on(function (&$context) |
|
| 47 | + { |
|
| 47 | 48 | $context = $this->arrayToContextString(TestJob::class); |
| 48 | 49 | |
| 49 | 50 | return true; |
@@ -68,7 +69,8 @@ discard block |
||
| 68 | 69 | $this->app->getContainer()->bind(Worker::class, $w); |
| 69 | 70 | |
| 70 | 71 | $w->shouldReceive('receive')->once()->with( |
| 71 | - \Mockery::on(function (&$context) { |
|
| 72 | + \Mockery::on(function (&$context) |
|
| 73 | + { |
|
| 72 | 74 | $context = $this->arrayToContextString(ErrorJob::class); |
| 73 | 75 | |
| 74 | 76 | return true; |
@@ -41,7 +41,8 @@ discard block |
||
| 41 | 41 | $container = new Container(); |
| 42 | 42 | $this->assertInstanceOf(FactoryInterface::class, $container); |
| 43 | 43 | |
| 44 | - $container->bind(Bucket::class, function ($data) { |
|
| 44 | + $container->bind(Bucket::class, function ($data) |
|
| 45 | + { |
|
| 45 | 46 | return new Bucket('via-closure', $data); |
| 46 | 47 | }); |
| 47 | 48 | |
@@ -78,7 +79,8 @@ discard block |
||
| 78 | 79 | $sample = new SampleClass(); |
| 79 | 80 | |
| 80 | 81 | $container->bind(Bucket::class, [self::class, 'makeBucketWithSample']); |
| 81 | - $container->bind(SampleClass::class, function () use ($sample) { |
|
| 82 | + $container->bind(SampleClass::class, function () use ($sample) |
|
| 83 | + { |
|
| 82 | 84 | return $sample; |
| 83 | 85 | }); |
| 84 | 86 | |
@@ -27,7 +27,8 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | $this->assertNull(ContainerScope::getContainer()); |
| 29 | 29 | |
| 30 | - $this->assertTrue(ContainerScope::runScope($container, function () use ($container) { |
|
| 30 | + $this->assertTrue(ContainerScope::runScope($container, function () use ($container) |
|
| 31 | + { |
|
| 31 | 32 | return $container === ContainerScope::getContainer(); |
| 32 | 33 | })); |
| 33 | 34 | |
@@ -40,11 +41,14 @@ discard block |
||
| 40 | 41 | |
| 41 | 42 | $this->assertNull(ContainerScope::getContainer()); |
| 42 | 43 | |
| 43 | - try { |
|
| 44 | + try |
|
| 45 | + { |
|
| 44 | 46 | $this->assertTrue(ContainerScope::runScope($container, function () use ($container): void { |
| 45 | 47 | throw new RuntimeException('exception'); |
| 46 | 48 | })); |
| 47 | - } catch (\Throwable $e) { |
|
| 49 | + } |
|
| 50 | + catch (\Throwable $e) |
|
| 51 | + { |
|
| 48 | 52 | } |
| 49 | 53 | |
| 50 | 54 | $this->assertInstanceOf(RuntimeException::class, $e); |
@@ -62,7 +66,8 @@ discard block |
||
| 62 | 66 | $this->assertTrue($c->runScope([ |
| 63 | 67 | 'bucket' => new Bucket('b'), |
| 64 | 68 | 'other' => new SampleClass() |
| 65 | - ], function () use ($c) { |
|
| 69 | + ], function () use ($c) |
|
| 70 | + { |
|
| 66 | 71 | $this->assertSame('b', $c->get('bucket')->getName()); |
| 67 | 72 | $this->assertTrue($c->has('other')); |
| 68 | 73 | |
@@ -84,21 +89,25 @@ discard block |
||
| 84 | 89 | $this->assertTrue($c->runScope([ |
| 85 | 90 | 'bucket' => new Bucket('b'), |
| 86 | 91 | 'other' => new SampleClass() |
| 87 | - ], function () use ($c) { |
|
| 92 | + ], function () use ($c) |
|
| 93 | + { |
|
| 88 | 94 | $this->assertSame('b', $c->get('bucket')->getName()); |
| 89 | 95 | $this->assertTrue($c->has('other')); |
| 90 | 96 | |
| 91 | 97 | return $c->get('bucket')->getName() == 'b' && $c->has('other'); |
| 92 | 98 | })); |
| 93 | 99 | |
| 94 | - try { |
|
| 100 | + try |
|
| 101 | + { |
|
| 95 | 102 | $this->assertTrue($c->runScope([ |
| 96 | 103 | 'bucket' => new Bucket('b'), |
| 97 | 104 | 'other' => new SampleClass() |
| 98 | 105 | ], function () use ($c): void { |
| 99 | 106 | throw new RuntimeException('exception'); |
| 100 | 107 | })); |
| 101 | - } catch (\Throwable $e) { |
|
| 108 | + } |
|
| 109 | + catch (\Throwable $e) |
|
| 110 | + { |
|
| 102 | 111 | } |
| 103 | 112 | |
| 104 | 113 | $this->assertSame('a', $c->get('bucket')->getName()); |
@@ -60,7 +60,8 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | $instance = new SampleClass(); |
| 62 | 62 | |
| 63 | - $container->bindSingleton('sampleClass', function () use ($instance) { |
|
| 63 | + $container->bindSingleton('sampleClass', function () use ($instance) |
|
| 64 | + { |
|
| 64 | 65 | return $instance; |
| 65 | 66 | }); |
| 66 | 67 | |
@@ -71,7 +72,8 @@ discard block |
||
| 71 | 72 | { |
| 72 | 73 | $container = new Container(); |
| 73 | 74 | |
| 74 | - $container->bindSingleton('sampleClass', function () { |
|
| 75 | + $container->bindSingleton('sampleClass', function () |
|
| 76 | + { |
|
| 75 | 77 | return new SampleClass(); |
| 76 | 78 | }); |
| 77 | 79 | |
@@ -98,7 +100,8 @@ discard block |
||
| 98 | 100 | $container = new Container(); |
| 99 | 101 | $container->bindSingleton('singleton', 'sampleClass'); |
| 100 | 102 | |
| 101 | - $container->bind('sampleClass', function () { |
|
| 103 | + $container->bind('sampleClass', function () |
|
| 104 | + { |
|
| 102 | 105 | return new SampleClass(); |
| 103 | 106 | }); |
| 104 | 107 | |
@@ -125,7 +125,8 @@ discard block |
||
| 125 | 125 | $container->bind(ConfigsInterface::class, $configurator); |
| 126 | 126 | |
| 127 | 127 | $configurator->shouldReceive('createInjection') |
| 128 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 128 | + ->with(m::on(static function (ReflectionClass $r) |
|
| 129 | + { |
|
| 129 | 130 | return $r->getName() === TestConfig::class; |
| 130 | 131 | }), null) |
| 131 | 132 | ->andReturn($expected) |
@@ -143,7 +144,8 @@ discard block |
||
| 143 | 144 | $container->bind(ConfigsInterface::class, $configurator); |
| 144 | 145 | |
| 145 | 146 | $configurator->shouldReceive('createInjection') |
| 146 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 147 | + ->with(m::on(static function (ReflectionClass $r) |
|
| 148 | + { |
|
| 147 | 149 | return $r->getName() === TestConfig::class; |
| 148 | 150 | }), 'context') |
| 149 | 151 | ->andReturn($expected) |
@@ -161,7 +163,8 @@ discard block |
||
| 161 | 163 | $container->bind(ConfigsInterface::class, $configurator); |
| 162 | 164 | |
| 163 | 165 | $configurator->shouldReceive('createInjection') |
| 164 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 166 | + ->with(m::on(static function (ReflectionClass $r) |
|
| 167 | + { |
|
| 165 | 168 | return $r->getName() === TestConfig::class; |
| 166 | 169 | }), 'contextArgument') |
| 167 | 170 | ->andReturn($expected) |
@@ -25,7 +25,8 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | $this->assertFalse($container->has('abc')); |
| 27 | 27 | |
| 28 | - $container->bind('abc', function () { |
|
| 28 | + $container->bind('abc', function () |
|
| 29 | + { |
|
| 29 | 30 | return 'hello'; |
| 30 | 31 | }); |
| 31 | 32 | |
@@ -38,7 +39,8 @@ discard block |
||
| 38 | 39 | $container = new Container(); |
| 39 | 40 | |
| 40 | 41 | $this->assertFalse($container->has('abc')); |
| 41 | - $container->bind('abc', function () { |
|
| 42 | + $container->bind('abc', function () |
|
| 43 | + { |
|
| 42 | 44 | return 'hello'; |
| 43 | 45 | }); |
| 44 | 46 | |
@@ -46,11 +46,16 @@ |
||
| 46 | 46 | { |
| 47 | 47 | [$previous, self::$container] = [self::$container, $container]; |
| 48 | 48 | |
| 49 | - try { |
|
| 49 | + try |
|
| 50 | + { |
|
| 50 | 51 | return $scope(); |
| 51 | - } catch (Throwable $e) { |
|
| 52 | + } |
|
| 53 | + catch (Throwable $e) |
|
| 54 | + { |
|
| 52 | 55 | throw $e; |
| 53 | - } finally { |
|
| 56 | + } |
|
| 57 | + finally |
|
| 58 | + { |
|
| 54 | 59 | self::$container = $previous; |
| 55 | 60 | } |
| 56 | 61 | } |
@@ -126,7 +126,8 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | public function get($alias, string $context = null) |
| 128 | 128 | { |
| 129 | - if ($alias instanceof Autowire) { |
|
| 129 | + if ($alias instanceof Autowire) |
|
| 130 | + { |
|
| 130 | 131 | return $alias->resolve($this); |
| 131 | 132 | } |
| 132 | 133 | |
@@ -142,34 +143,44 @@ discard block |
||
| 142 | 143 | */ |
| 143 | 144 | public function make(string $alias, array $parameters = [], string $context = null) |
| 144 | 145 | { |
| 145 | - if (!isset($this->bindings[$alias])) { |
|
| 146 | + if (!isset($this->bindings[$alias])) |
|
| 147 | + { |
|
| 146 | 148 | //No direct instructions how to construct class, make is automatically |
| 147 | 149 | return $this->autowire($alias, $parameters, $context); |
| 148 | 150 | } |
| 149 | 151 | |
| 150 | 152 | $binding = $this->bindings[$alias]; |
| 151 | - if (is_object($binding)) { |
|
| 153 | + if (is_object($binding)) |
|
| 154 | + { |
|
| 152 | 155 | //When binding is instance, assuming singleton |
| 153 | 156 | return $binding; |
| 154 | 157 | } |
| 155 | 158 | |
| 156 | - if (is_string($binding)) { |
|
| 159 | + if (is_string($binding)) |
|
| 160 | + { |
|
| 157 | 161 | //Binding is pointing to something else |
| 158 | 162 | return $this->make($binding, $parameters, $context); |
| 159 | 163 | } |
| 160 | 164 | |
| 161 | 165 | unset($this->bindings[$alias]); |
| 162 | - try { |
|
| 163 | - if ($binding[0] === $alias) { |
|
| 166 | + try |
|
| 167 | + { |
|
| 168 | + if ($binding[0] === $alias) |
|
| 169 | + { |
|
| 164 | 170 | $instance = $this->autowire($alias, $parameters, $context); |
| 165 | - } else { |
|
| 171 | + } |
|
| 172 | + else |
|
| 173 | + { |
|
| 166 | 174 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 167 | 175 | } |
| 168 | - } finally { |
|
| 176 | + } |
|
| 177 | + finally |
|
| 178 | + { |
|
| 169 | 179 | $this->bindings[$alias] = $binding; |
| 170 | 180 | } |
| 171 | 181 | |
| 172 | - if ($binding[1]) { |
|
| 182 | + if ($binding[1]) |
|
| 183 | + { |
|
| 173 | 184 | //Indicates singleton |
| 174 | 185 | $this->bindings[$alias] = $instance; |
| 175 | 186 | } |
@@ -190,15 +201,20 @@ discard block |
||
| 190 | 201 | string $context = null |
| 191 | 202 | ): array { |
| 192 | 203 | $arguments = []; |
| 193 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 194 | - try { |
|
| 204 | + foreach ($reflection->getParameters() as $parameter) |
|
| 205 | + { |
|
| 206 | + try |
|
| 207 | + { |
|
| 195 | 208 | //Information we need to know about argument in order to resolve it's value |
| 196 | 209 | $name = $parameter->getName(); |
| 197 | 210 | $class = $parameter->getClass(); |
| 198 | - } catch (Throwable $e) { |
|
| 211 | + } |
|
| 212 | + catch (Throwable $e) |
|
| 213 | + { |
|
| 199 | 214 | //Possibly invalid class definition or syntax error |
| 200 | 215 | $location = $reflection->getName(); |
| 201 | - if ($reflection instanceof ReflectionMethod) { |
|
| 216 | + if ($reflection instanceof ReflectionMethod) |
|
| 217 | + { |
|
| 202 | 218 | $location = "{$reflection->getDeclaringClass()->getName()}->{$location}"; |
| 203 | 219 | } |
| 204 | 220 | //Possibly invalid class definition or syntax error |
@@ -209,11 +225,15 @@ discard block |
||
| 209 | 225 | ); |
| 210 | 226 | } |
| 211 | 227 | |
| 212 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
| 213 | - if ($parameters[$name] instanceof Autowire) { |
|
| 228 | + if (isset($parameters[$name]) && is_object($parameters[$name])) |
|
| 229 | + { |
|
| 230 | + if ($parameters[$name] instanceof Autowire) |
|
| 231 | + { |
|
| 214 | 232 | //Supplied by user as late dependency |
| 215 | 233 | $arguments[] = $parameters[$name]->resolve($this); |
| 216 | - } else { |
|
| 234 | + } |
|
| 235 | + else |
|
| 236 | + { |
|
| 217 | 237 | //Supplied by user as object |
| 218 | 238 | $arguments[] = $parameters[$name]; |
| 219 | 239 | } |
@@ -221,16 +241,19 @@ discard block |
||
| 221 | 241 | } |
| 222 | 242 | |
| 223 | 243 | // no declared type or scalar type or array |
| 224 | - if (!isset($class)) { |
|
| 244 | + if (!isset($class)) |
|
| 245 | + { |
|
| 225 | 246 | //Provided from outside |
| 226 | - if (array_key_exists($name, $parameters)) { |
|
| 247 | + if (array_key_exists($name, $parameters)) |
|
| 248 | + { |
|
| 227 | 249 | //Make sure it's properly typed |
| 228 | 250 | $this->assertType($parameter, $reflection, $parameters[$name]); |
| 229 | 251 | $arguments[] = $parameters[$name]; |
| 230 | 252 | continue; |
| 231 | 253 | } |
| 232 | 254 | |
| 233 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 255 | + if ($parameter->isDefaultValueAvailable()) |
|
| 256 | + { |
|
| 234 | 257 | //Default value |
| 235 | 258 | $arguments[] = $parameter->getDefaultValue(); |
| 236 | 259 | continue; |
@@ -240,12 +263,16 @@ discard block |
||
| 240 | 263 | throw new ArgumentException($parameter, $reflection); |
| 241 | 264 | } |
| 242 | 265 | |
| 243 | - try { |
|
| 266 | + try |
|
| 267 | + { |
|
| 244 | 268 | //Requesting for contextual dependency |
| 245 | 269 | $arguments[] = $this->get($class->getName(), $name); |
| 246 | 270 | continue; |
| 247 | - } catch (AutowireException $e) { |
|
| 248 | - if ($parameter->isOptional()) { |
|
| 271 | + } |
|
| 272 | + catch (AutowireException $e) |
|
| 273 | + { |
|
| 274 | + if ($parameter->isOptional()) |
|
| 275 | + { |
|
| 249 | 276 | //This is optional dependency, skip |
| 250 | 277 | $arguments[] = null; |
| 251 | 278 | continue; |
@@ -264,28 +291,38 @@ discard block |
||
| 264 | 291 | public function runScope(array $bindings, callable $scope) |
| 265 | 292 | { |
| 266 | 293 | $cleanup = $previous = []; |
| 267 | - foreach ($bindings as $alias => $resolver) { |
|
| 268 | - if (isset($this->bindings[$alias])) { |
|
| 294 | + foreach ($bindings as $alias => $resolver) |
|
| 295 | + { |
|
| 296 | + if (isset($this->bindings[$alias])) |
|
| 297 | + { |
|
| 269 | 298 | $previous[$alias] = $this->bindings[$alias]; |
| 270 | - } else { |
|
| 299 | + } |
|
| 300 | + else |
|
| 301 | + { |
|
| 271 | 302 | $cleanup[] = $alias; |
| 272 | 303 | } |
| 273 | 304 | |
| 274 | 305 | $this->bind($alias, $resolver); |
| 275 | 306 | } |
| 276 | 307 | |
| 277 | - try { |
|
| 278 | - if (ContainerScope::getContainer() !== $this) { |
|
| 308 | + try |
|
| 309 | + { |
|
| 310 | + if (ContainerScope::getContainer() !== $this) |
|
| 311 | + { |
|
| 279 | 312 | return ContainerScope::runScope($this, $scope); |
| 280 | 313 | } |
| 281 | 314 | |
| 282 | 315 | return $scope(); |
| 283 | - } finally { |
|
| 284 | - foreach (array_reverse($previous) as $alias => $resolver) { |
|
| 316 | + } |
|
| 317 | + finally |
|
| 318 | + { |
|
| 319 | + foreach (array_reverse($previous) as $alias => $resolver) |
|
| 320 | + { |
|
| 285 | 321 | $this->bindings[$alias] = $resolver; |
| 286 | 322 | } |
| 287 | 323 | |
| 288 | - foreach ($cleanup as $alias) { |
|
| 324 | + foreach ($cleanup as $alias) |
|
| 325 | + { |
|
| 289 | 326 | unset($this->bindings[$alias]); |
| 290 | 327 | } |
| 291 | 328 | } |
@@ -301,7 +338,8 @@ discard block |
||
| 301 | 338 | */ |
| 302 | 339 | public function bind(string $alias, $resolver): void |
| 303 | 340 | { |
| 304 | - if (is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) { |
|
| 341 | + if (is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) |
|
| 342 | + { |
|
| 305 | 343 | // array means = execute me, false = not singleton |
| 306 | 344 | $this->bindings[$alias] = [$resolver, false]; |
| 307 | 345 | return; |
@@ -319,7 +357,8 @@ discard block |
||
| 319 | 357 | */ |
| 320 | 358 | public function bindSingleton(string $alias, $resolver): void |
| 321 | 359 | { |
| 322 | - if (is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) { |
|
| 360 | + if (is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) |
|
| 361 | + { |
|
| 323 | 362 | // direct binding to an instance |
| 324 | 363 | $this->bindings[$alias] = $resolver; |
| 325 | 364 | return; |
@@ -336,11 +375,13 @@ discard block |
||
| 336 | 375 | */ |
| 337 | 376 | public function hasInstance(string $alias): bool |
| 338 | 377 | { |
| 339 | - if (!$this->has($alias)) { |
|
| 378 | + if (!$this->has($alias)) |
|
| 379 | + { |
|
| 340 | 380 | return false; |
| 341 | 381 | } |
| 342 | 382 | |
| 343 | - while (isset($this->bindings[$alias]) && is_string($this->bindings[$alias])) { |
|
| 383 | + while (isset($this->bindings[$alias]) && is_string($this->bindings[$alias])) |
|
| 384 | + { |
|
| 344 | 385 | //Checking alias tree |
| 345 | 386 | $alias = $this->bindings[$alias]; |
| 346 | 387 | } |
@@ -414,7 +455,8 @@ discard block |
||
| 414 | 455 | */ |
| 415 | 456 | protected function autowire(string $class, array $parameters, string $context = null) |
| 416 | 457 | { |
| 417 | - if (!class_exists($class)) { |
|
| 458 | + if (!class_exists($class)) |
|
| 459 | + { |
|
| 418 | 460 | throw new NotFoundException(sprintf("Undefined class or binding '%s'", $class)); |
| 419 | 461 | } |
| 420 | 462 | |
@@ -436,9 +478,11 @@ discard block |
||
| 436 | 478 | private function registerInstance($instance, array $parameters) |
| 437 | 479 | { |
| 438 | 480 | //Declarative singletons (only when class received via direct get) |
| 439 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 481 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
| 482 | + { |
|
| 440 | 483 | $alias = get_class($instance); |
| 441 | - if (!isset($this->bindings[$alias])) { |
|
| 484 | + if (!isset($this->bindings[$alias])) |
|
| 485 | + { |
|
| 442 | 486 | $this->bindings[$alias] = $instance; |
| 443 | 487 | } |
| 444 | 488 | } |
@@ -463,19 +507,25 @@ discard block |
||
| 463 | 507 | array $parameters, |
| 464 | 508 | string $context = null |
| 465 | 509 | ) { |
| 466 | - if (is_string($target)) { |
|
| 510 | + if (is_string($target)) |
|
| 511 | + { |
|
| 467 | 512 | //Reference |
| 468 | 513 | return $this->make($target, $parameters, $context); |
| 469 | 514 | } |
| 470 | 515 | |
| 471 | - if ($target instanceof Autowire) { |
|
| 516 | + if ($target instanceof Autowire) |
|
| 517 | + { |
|
| 472 | 518 | return $target->resolve($this, $parameters); |
| 473 | 519 | } |
| 474 | 520 | |
| 475 | - if ($target instanceof Closure) { |
|
| 476 | - try { |
|
| 521 | + if ($target instanceof Closure) |
|
| 522 | + { |
|
| 523 | + try |
|
| 524 | + { |
|
| 477 | 525 | $reflection = new ReflectionFunction($target); |
| 478 | - } catch (ReflectionException $e) { |
|
| 526 | + } |
|
| 527 | + catch (ReflectionException $e) |
|
| 528 | + { |
|
| 479 | 529 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 480 | 530 | } |
| 481 | 531 | |
@@ -485,16 +535,20 @@ discard block |
||
| 485 | 535 | ); |
| 486 | 536 | } |
| 487 | 537 | |
| 488 | - if (is_array($target) && isset($target[1])) { |
|
| 538 | + if (is_array($target) && isset($target[1])) |
|
| 539 | + { |
|
| 489 | 540 | //In a form of resolver and method |
| 490 | 541 | [$resolver, $method] = $target; |
| 491 | 542 | |
| 492 | 543 | //Resolver instance (i.e. [ClassName::class, 'method']) |
| 493 | 544 | $resolver = $this->get($resolver); |
| 494 | 545 | |
| 495 | - try { |
|
| 546 | + try |
|
| 547 | + { |
|
| 496 | 548 | $method = new ReflectionMethod($resolver, $method); |
| 497 | - } catch (ReflectionException $e) { |
|
| 549 | + } |
|
| 550 | + catch (ReflectionException $e) |
|
| 551 | + { |
|
| 498 | 552 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 499 | 553 | } |
| 500 | 554 | |
@@ -523,22 +577,28 @@ discard block |
||
| 523 | 577 | */ |
| 524 | 578 | private function createInstance(string $class, array $parameters, string $context = null) |
| 525 | 579 | { |
| 526 | - try { |
|
| 580 | + try |
|
| 581 | + { |
|
| 527 | 582 | $reflection = new ReflectionClass($class); |
| 528 | - } catch (ReflectionException $e) { |
|
| 583 | + } |
|
| 584 | + catch (ReflectionException $e) |
|
| 585 | + { |
|
| 529 | 586 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 530 | 587 | } |
| 531 | 588 | |
| 532 | 589 | //We have to construct class using external injector when we know exact context |
| 533 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
| 590 | + if ($parameters === [] && $this->checkInjector($reflection)) |
|
| 591 | + { |
|
| 534 | 592 | $injector = $this->injectors[$reflection->getName()]; |
| 535 | 593 | |
| 536 | 594 | $instance = null; |
| 537 | - try { |
|
| 595 | + try |
|
| 596 | + { |
|
| 538 | 597 | /** @var InjectorInterface $injectorInstance */ |
| 539 | 598 | $injectorInstance = $this->get($injector); |
| 540 | 599 | |
| 541 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 600 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 601 | + { |
|
| 542 | 602 | throw new InjectionException( |
| 543 | 603 | sprintf( |
| 544 | 604 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -549,7 +609,8 @@ discard block |
||
| 549 | 609 | } |
| 550 | 610 | |
| 551 | 611 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 552 | - if (!$reflection->isInstance($instance)) { |
|
| 612 | + if (!$reflection->isInstance($instance)) |
|
| 613 | + { |
|
| 553 | 614 | throw new InjectionException( |
| 554 | 615 | sprintf( |
| 555 | 616 | "Invalid injection response for '%s'", |
@@ -557,23 +618,29 @@ discard block |
||
| 557 | 618 | ) |
| 558 | 619 | ); |
| 559 | 620 | } |
| 560 | - } finally { |
|
| 621 | + } |
|
| 622 | + finally |
|
| 623 | + { |
|
| 561 | 624 | $this->injectors[$reflection->getName()] = $injector; |
| 562 | 625 | } |
| 563 | 626 | |
| 564 | 627 | return $instance; |
| 565 | 628 | } |
| 566 | 629 | |
| 567 | - if (!$reflection->isInstantiable()) { |
|
| 630 | + if (!$reflection->isInstantiable()) |
|
| 631 | + { |
|
| 568 | 632 | throw new ContainerException(sprintf("Class '%s' can not be constructed", $class)); |
| 569 | 633 | } |
| 570 | 634 | |
| 571 | 635 | $constructor = $reflection->getConstructor(); |
| 572 | 636 | |
| 573 | - if ($constructor !== null) { |
|
| 637 | + if ($constructor !== null) |
|
| 638 | + { |
|
| 574 | 639 | // Using constructor with resolved arguments |
| 575 | 640 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
| 576 | - } else { |
|
| 641 | + } |
|
| 642 | + else |
|
| 643 | + { |
|
| 577 | 644 | // No constructor specified |
| 578 | 645 | $instance = $reflection->newInstance(); |
| 579 | 646 | } |
@@ -590,7 +657,8 @@ discard block |
||
| 590 | 657 | private function checkInjector(ReflectionClass $reflection): bool |
| 591 | 658 | { |
| 592 | 659 | $class = $reflection->getName(); |
| 593 | - if (array_key_exists($class, $this->injectors)) { |
|
| 660 | + if (array_key_exists($class, $this->injectors)) |
|
| 661 | + { |
|
| 594 | 662 | return $this->injectors[$class] !== null; |
| 595 | 663 | } |
| 596 | 664 | |
@@ -602,11 +670,13 @@ discard block |
||
| 602 | 670 | return true; |
| 603 | 671 | } |
| 604 | 672 | |
| 605 | - if (!isset($this->injectorsCache[$class])) { |
|
| 673 | + if (!isset($this->injectorsCache[$class])) |
|
| 674 | + { |
|
| 606 | 675 | $this->injectorsCache[$class] = null; |
| 607 | 676 | |
| 608 | 677 | // check interfaces |
| 609 | - foreach ($this->injectors as $target => $injector) { |
|
| 678 | + foreach ($this->injectors as $target => $injector) |
|
| 679 | + { |
|
| 610 | 680 | if ( |
| 611 | 681 | class_exists($target, true) |
| 612 | 682 | && $reflection->isSubclassOf($target) |
@@ -640,7 +710,8 @@ discard block |
||
| 640 | 710 | */ |
| 641 | 711 | private function assertType(ReflectionParameter $parameter, ContextFunction $context, $value): void |
| 642 | 712 | { |
| 643 | - if ($value === null) { |
|
| 713 | + if ($value === null) |
|
| 714 | + { |
|
| 644 | 715 | if ( |
| 645 | 716 | !$parameter->isOptional() && |
| 646 | 717 | !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null) |
@@ -652,20 +723,24 @@ discard block |
||
| 652 | 723 | } |
| 653 | 724 | |
| 654 | 725 | $type = $parameter->getType(); |
| 655 | - if ($type === null) { |
|
| 726 | + if ($type === null) |
|
| 727 | + { |
|
| 656 | 728 | return; |
| 657 | 729 | } |
| 658 | 730 | |
| 659 | 731 | $typeName = $type->getName(); |
| 660 | - if ($typeName === 'array' && !is_array($value)) { |
|
| 732 | + if ($typeName === 'array' && !is_array($value)) |
|
| 733 | + { |
|
| 661 | 734 | throw new ArgumentException($parameter, $context); |
| 662 | 735 | } |
| 663 | 736 | |
| 664 | - if (($typeName === 'int' || $typeName === 'float') && !is_numeric($value)) { |
|
| 737 | + if (($typeName === 'int' || $typeName === 'float') && !is_numeric($value)) |
|
| 738 | + { |
|
| 665 | 739 | throw new ArgumentException($parameter, $context); |
| 666 | 740 | } |
| 667 | 741 | |
| 668 | - if ($typeName === 'bool' && !is_bool($value) && !is_numeric($value)) { |
|
| 742 | + if ($typeName === 'bool' && !is_bool($value) && !is_numeric($value)) |
|
| 743 | + { |
|
| 669 | 744 | throw new ArgumentException($parameter, $context); |
| 670 | 745 | } |
| 671 | 746 | } |
@@ -44,7 +44,8 @@ |
||
| 44 | 44 | $this->context = $context; |
| 45 | 45 | |
| 46 | 46 | $name = $context->getName(); |
| 47 | - if ($context instanceof ReflectionMethod) { |
|
| 47 | + if ($context instanceof ReflectionMethod) |
|
| 48 | + { |
|
| 48 | 49 | $name = $context->class . '::' . $name; |
| 49 | 50 | } |
| 50 | 51 | |