@@ -43,7 +43,7 @@ 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 | $context = $this->arrayToContextString(TestJob::class); |
| 48 | 48 | |
| 49 | 49 | return true; |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | $this->app->getContainer()->bind(Worker::class, $w); |
| 69 | 69 | |
| 70 | 70 | $w->shouldReceive('receive')->once()->with( |
| 71 | - \Mockery::on(function (&$context) { |
|
| 71 | + \Mockery::on(function (&$context){ |
|
| 72 | 72 | $context = $this->arrayToContextString(ErrorJob::class); |
| 73 | 73 | |
| 74 | 74 | return true; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | $w->shouldReceive('receive')->once()->andReturn(null); |
| 82 | 82 | |
| 83 | 83 | $files = $this->app->get(FilesInterface::class)->getFiles( |
| 84 | - $this->app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' |
|
| 84 | + $this->app->get(DirectoriesInterface::class)->get('runtime').'/snapshots/' |
|
| 85 | 85 | ); |
| 86 | 86 | |
| 87 | 87 | $this->assertCount(0, $files); |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $this->app->get(JobDispatcher::class)->serve(); |
| 90 | 90 | |
| 91 | 91 | $files = $this->app->get(FilesInterface::class)->getFiles( |
| 92 | - $this->app->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' |
|
| 92 | + $this->app->get(DirectoriesInterface::class)->get('runtime').'/snapshots/' |
|
| 93 | 93 | ); |
| 94 | 94 | |
| 95 | 95 | $this->assertCount(1, $files); |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | return \json_encode([ |
| 109 | 109 | 'id' => (string)$id, |
| 110 | - 'job' => \strtolower(\implode('.', $signature)) . '.' . $class |
|
| 110 | + 'job' => \strtolower(\implode('.', $signature)).'.'.$class |
|
| 111 | 111 | ]); |
| 112 | 112 | } |
| 113 | 113 | } |
@@ -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; |
@@ -20,6 +20,6 @@ |
||
| 20 | 20 | bool $bool, |
| 21 | 21 | array $array = [], |
| 22 | 22 | string $pong = null |
| 23 | - ) { |
|
| 23 | + ){ |
|
| 24 | 24 | } |
| 25 | 25 | } |
@@ -41,7 +41,7 @@ |
||
| 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 | return new Bucket('via-closure', $data); |
| 46 | 46 | }); |
| 47 | 47 | |
@@ -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 | |
@@ -40,11 +40,11 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | $this->assertNull(ContainerScope::getContainer()); |
| 42 | 42 | |
| 43 | - try { |
|
| 43 | + try{ |
|
| 44 | 44 | $this->assertTrue(ContainerScope::runScope($container, function () use ($container): void { |
| 45 | 45 | throw new RuntimeException('exception'); |
| 46 | 46 | })); |
| 47 | - } catch (\Throwable $e) { |
|
| 47 | + }catch (\Throwable $e){ |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | $this->assertInstanceOf(RuntimeException::class, $e); |
@@ -91,14 +91,14 @@ discard block |
||
| 91 | 91 | return $c->get('bucket')->getName() == 'b' && $c->has('other'); |
| 92 | 92 | })); |
| 93 | 93 | |
| 94 | - try { |
|
| 94 | + try{ |
|
| 95 | 95 | $this->assertTrue($c->runScope([ |
| 96 | 96 | 'bucket' => new Bucket('b'), |
| 97 | 97 | 'other' => new SampleClass() |
| 98 | 98 | ], function () use ($c): void { |
| 99 | 99 | throw new RuntimeException('exception'); |
| 100 | 100 | })); |
| 101 | - } catch (\Throwable $e) { |
|
| 101 | + }catch (\Throwable $e){ |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | $this->assertSame('a', $c->get('bucket')->getName()); |
@@ -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()); |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | { |
| 72 | 72 | $container = new Container(); |
| 73 | 73 | |
| 74 | - $container->bindSingleton('sampleClass', function () { |
|
| 74 | + $container->bindSingleton('sampleClass', function (){ |
|
| 75 | 75 | return new SampleClass(); |
| 76 | 76 | }); |
| 77 | 77 | |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $container = new Container(); |
| 99 | 99 | $container->bindSingleton('singleton', 'sampleClass'); |
| 100 | 100 | |
| 101 | - $container->bind('sampleClass', function () { |
|
| 101 | + $container->bind('sampleClass', function (){ |
|
| 102 | 102 | return new SampleClass(); |
| 103 | 103 | }); |
| 104 | 104 | |
@@ -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 | |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | public function testInvalidInjector(): void |
| 38 | 38 | { |
| 39 | - $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " . |
|
| 39 | + $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an ". |
|
| 40 | 40 | "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'"; |
| 41 | 41 | $this->expectException(InjectionException::class); |
| 42 | 42 | $this->expectExceptionMessage($excepted); |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | public function testInvalidRuntimeInjector(): void |
| 62 | 62 | { |
| 63 | - $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " . |
|
| 63 | + $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an ". |
|
| 64 | 64 | "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'"; |
| 65 | 65 | $this->expectException(InjectionException::class); |
| 66 | 66 | $this->expectExceptionMessage($excepted); |
@@ -125,7 +125,7 @@ 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 | return $r->getName() === TestConfig::class; |
| 130 | 130 | }), null) |
| 131 | 131 | ->andReturn($expected) |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | $container->bind(ConfigsInterface::class, $configurator); |
| 144 | 144 | |
| 145 | 145 | $configurator->shouldReceive('createInjection') |
| 146 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 146 | + ->with(m::on(static function (ReflectionClass $r){ |
|
| 147 | 147 | return $r->getName() === TestConfig::class; |
| 148 | 148 | }), 'context') |
| 149 | 149 | ->andReturn($expected) |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | $container->bind(ConfigsInterface::class, $configurator); |
| 162 | 162 | |
| 163 | 163 | $configurator->shouldReceive('createInjection') |
| 164 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 164 | + ->with(m::on(static function (ReflectionClass $r){ |
|
| 165 | 165 | return $r->getName() === TestConfig::class; |
| 166 | 166 | }), 'contextArgument') |
| 167 | 167 | ->andReturn($expected) |
@@ -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,7 @@ 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 | return 'hello'; |
| 30 | 30 | }); |
| 31 | 31 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | $container = new Container(); |
| 39 | 39 | |
| 40 | 40 | $this->assertFalse($container->has('abc')); |
| 41 | - $container->bind('abc', function () { |
|
| 41 | + $container->bind('abc', function (){ |
|
| 42 | 42 | return 'hello'; |
| 43 | 43 | }); |
| 44 | 44 | |
@@ -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,11 @@ |
||
| 46 | 46 | { |
| 47 | 47 | [$previous, self::$container] = [self::$container, $container]; |
| 48 | 48 | |
| 49 | - try { |
|
| 49 | + try{ |
|
| 50 | 50 | return $scope(); |
| 51 | - } catch (Throwable $e) { |
|
| 51 | + }catch (Throwable $e){ |
|
| 52 | 52 | throw $e; |
| 53 | - } finally { |
|
| 53 | + }finally{ |
|
| 54 | 54 | self::$container = $previous; |
| 55 | 55 | } |
| 56 | 56 | } |
@@ -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,7 @@ 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 | return $alias->resolve($this); |
| 131 | 131 | } |
| 132 | 132 | |
@@ -142,34 +142,34 @@ discard block |
||
| 142 | 142 | */ |
| 143 | 143 | public function make(string $alias, array $parameters = [], string $context = null) |
| 144 | 144 | { |
| 145 | - if (!isset($this->bindings[$alias])) { |
|
| 145 | + if (!isset($this->bindings[$alias])){ |
|
| 146 | 146 | //No direct instructions how to construct class, make is automatically |
| 147 | 147 | return $this->autowire($alias, $parameters, $context); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | $binding = $this->bindings[$alias]; |
| 151 | - if (is_object($binding)) { |
|
| 151 | + if (is_object($binding)){ |
|
| 152 | 152 | //When binding is instance, assuming singleton |
| 153 | 153 | return $binding; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - if (is_string($binding)) { |
|
| 156 | + if (is_string($binding)){ |
|
| 157 | 157 | //Binding is pointing to something else |
| 158 | 158 | return $this->make($binding, $parameters, $context); |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | unset($this->bindings[$alias]); |
| 162 | - try { |
|
| 163 | - if ($binding[0] === $alias) { |
|
| 162 | + try{ |
|
| 163 | + if ($binding[0] === $alias){ |
|
| 164 | 164 | $instance = $this->autowire($alias, $parameters, $context); |
| 165 | - } else { |
|
| 165 | + }else{ |
|
| 166 | 166 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 167 | 167 | } |
| 168 | - } finally { |
|
| 168 | + }finally{ |
|
| 169 | 169 | $this->bindings[$alias] = $binding; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - if ($binding[1]) { |
|
| 172 | + if ($binding[1]){ |
|
| 173 | 173 | //Indicates singleton |
| 174 | 174 | $this->bindings[$alias] = $instance; |
| 175 | 175 | } |
@@ -190,30 +190,30 @@ discard block |
||
| 190 | 190 | string $context = null |
| 191 | 191 | ): array { |
| 192 | 192 | $arguments = []; |
| 193 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 194 | - try { |
|
| 193 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 194 | + try{ |
|
| 195 | 195 | //Information we need to know about argument in order to resolve it's value |
| 196 | 196 | $name = $parameter->getName(); |
| 197 | 197 | $class = $parameter->getClass(); |
| 198 | - } catch (Throwable $e) { |
|
| 198 | + }catch (Throwable $e){ |
|
| 199 | 199 | //Possibly invalid class definition or syntax error |
| 200 | 200 | $location = $reflection->getName(); |
| 201 | - if ($reflection instanceof ReflectionMethod) { |
|
| 201 | + if ($reflection instanceof ReflectionMethod){ |
|
| 202 | 202 | $location = "{$reflection->getDeclaringClass()->getName()}->{$location}"; |
| 203 | 203 | } |
| 204 | 204 | //Possibly invalid class definition or syntax error |
| 205 | 205 | throw new ContainerException( |
| 206 | - "Unable to resolve `{$parameter->getName()}` in {$location}: " . $e->getMessage(), |
|
| 206 | + "Unable to resolve `{$parameter->getName()}` in {$location}: ".$e->getMessage(), |
|
| 207 | 207 | $e->getCode(), |
| 208 | 208 | $e |
| 209 | 209 | ); |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
| 213 | - if ($parameters[$name] instanceof Autowire) { |
|
| 212 | + if (isset($parameters[$name]) && is_object($parameters[$name])){ |
|
| 213 | + if ($parameters[$name] instanceof Autowire){ |
|
| 214 | 214 | //Supplied by user as late dependency |
| 215 | 215 | $arguments[] = $parameters[$name]->resolve($this); |
| 216 | - } else { |
|
| 216 | + }else{ |
|
| 217 | 217 | //Supplied by user as object |
| 218 | 218 | $arguments[] = $parameters[$name]; |
| 219 | 219 | } |
@@ -221,16 +221,16 @@ discard block |
||
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | // no declared type or scalar type or array |
| 224 | - if (!isset($class)) { |
|
| 224 | + if (!isset($class)){ |
|
| 225 | 225 | //Provided from outside |
| 226 | - if (array_key_exists($name, $parameters)) { |
|
| 226 | + if (array_key_exists($name, $parameters)){ |
|
| 227 | 227 | //Make sure it's properly typed |
| 228 | 228 | $this->assertType($parameter, $reflection, $parameters[$name]); |
| 229 | 229 | $arguments[] = $parameters[$name]; |
| 230 | 230 | continue; |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 233 | + if ($parameter->isDefaultValueAvailable()){ |
|
| 234 | 234 | //Default value |
| 235 | 235 | $arguments[] = $parameter->getDefaultValue(); |
| 236 | 236 | continue; |
@@ -240,12 +240,12 @@ discard block |
||
| 240 | 240 | throw new ArgumentException($parameter, $reflection); |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | - try { |
|
| 243 | + try{ |
|
| 244 | 244 | //Requesting for contextual dependency |
| 245 | 245 | $arguments[] = $this->get($class->getName(), $name); |
| 246 | 246 | continue; |
| 247 | - } catch (AutowireException $e) { |
|
| 248 | - if ($parameter->isOptional()) { |
|
| 247 | + }catch (AutowireException $e){ |
|
| 248 | + if ($parameter->isOptional()){ |
|
| 249 | 249 | //This is optional dependency, skip |
| 250 | 250 | $arguments[] = null; |
| 251 | 251 | continue; |
@@ -264,28 +264,28 @@ discard block |
||
| 264 | 264 | public function runScope(array $bindings, callable $scope) |
| 265 | 265 | { |
| 266 | 266 | $cleanup = $previous = []; |
| 267 | - foreach ($bindings as $alias => $resolver) { |
|
| 268 | - if (isset($this->bindings[$alias])) { |
|
| 267 | + foreach ($bindings as $alias => $resolver){ |
|
| 268 | + if (isset($this->bindings[$alias])){ |
|
| 269 | 269 | $previous[$alias] = $this->bindings[$alias]; |
| 270 | - } else { |
|
| 270 | + }else{ |
|
| 271 | 271 | $cleanup[] = $alias; |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | $this->bind($alias, $resolver); |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - try { |
|
| 278 | - if (ContainerScope::getContainer() !== $this) { |
|
| 277 | + try{ |
|
| 278 | + if (ContainerScope::getContainer() !== $this){ |
|
| 279 | 279 | return ContainerScope::runScope($this, $scope); |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | return $scope(); |
| 283 | - } finally { |
|
| 284 | - foreach (array_reverse($previous) as $alias => $resolver) { |
|
| 283 | + }finally{ |
|
| 284 | + foreach (array_reverse($previous) as $alias => $resolver){ |
|
| 285 | 285 | $this->bindings[$alias] = $resolver; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - foreach ($cleanup as $alias) { |
|
| 288 | + foreach ($cleanup as $alias){ |
|
| 289 | 289 | unset($this->bindings[$alias]); |
| 290 | 290 | } |
| 291 | 291 | } |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | */ |
| 302 | 302 | public function bind(string $alias, $resolver): void |
| 303 | 303 | { |
| 304 | - if (is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) { |
|
| 304 | + if (is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire){ |
|
| 305 | 305 | // array means = execute me, false = not singleton |
| 306 | 306 | $this->bindings[$alias] = [$resolver, false]; |
| 307 | 307 | return; |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | */ |
| 320 | 320 | public function bindSingleton(string $alias, $resolver): void |
| 321 | 321 | { |
| 322 | - if (is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) { |
|
| 322 | + if (is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire){ |
|
| 323 | 323 | // direct binding to an instance |
| 324 | 324 | $this->bindings[$alias] = $resolver; |
| 325 | 325 | return; |
@@ -336,11 +336,11 @@ discard block |
||
| 336 | 336 | */ |
| 337 | 337 | public function hasInstance(string $alias): bool |
| 338 | 338 | { |
| 339 | - if (!$this->has($alias)) { |
|
| 339 | + if (!$this->has($alias)){ |
|
| 340 | 340 | return false; |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - while (isset($this->bindings[$alias]) && is_string($this->bindings[$alias])) { |
|
| 343 | + while (isset($this->bindings[$alias]) && is_string($this->bindings[$alias])){ |
|
| 344 | 344 | //Checking alias tree |
| 345 | 345 | $alias = $this->bindings[$alias]; |
| 346 | 346 | } |
@@ -414,7 +414,7 @@ discard block |
||
| 414 | 414 | */ |
| 415 | 415 | protected function autowire(string $class, array $parameters, string $context = null) |
| 416 | 416 | { |
| 417 | - if (!class_exists($class)) { |
|
| 417 | + if (!class_exists($class)){ |
|
| 418 | 418 | throw new NotFoundException(sprintf("Undefined class or binding '%s'", $class)); |
| 419 | 419 | } |
| 420 | 420 | |
@@ -436,9 +436,9 @@ discard block |
||
| 436 | 436 | private function registerInstance($instance, array $parameters) |
| 437 | 437 | { |
| 438 | 438 | //Declarative singletons (only when class received via direct get) |
| 439 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 439 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
| 440 | 440 | $alias = get_class($instance); |
| 441 | - if (!isset($this->bindings[$alias])) { |
|
| 441 | + if (!isset($this->bindings[$alias])){ |
|
| 442 | 442 | $this->bindings[$alias] = $instance; |
| 443 | 443 | } |
| 444 | 444 | } |
@@ -462,20 +462,20 @@ discard block |
||
| 462 | 462 | $target, |
| 463 | 463 | array $parameters, |
| 464 | 464 | string $context = null |
| 465 | - ) { |
|
| 466 | - if (is_string($target)) { |
|
| 465 | + ){ |
|
| 466 | + if (is_string($target)){ |
|
| 467 | 467 | //Reference |
| 468 | 468 | return $this->make($target, $parameters, $context); |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | - if ($target instanceof Autowire) { |
|
| 471 | + if ($target instanceof Autowire){ |
|
| 472 | 472 | return $target->resolve($this, $parameters); |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | - if ($target instanceof Closure) { |
|
| 476 | - try { |
|
| 475 | + if ($target instanceof Closure){ |
|
| 476 | + try{ |
|
| 477 | 477 | $reflection = new ReflectionFunction($target); |
| 478 | - } catch (ReflectionException $e) { |
|
| 478 | + }catch (ReflectionException $e){ |
|
| 479 | 479 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 480 | 480 | } |
| 481 | 481 | |
@@ -485,16 +485,16 @@ discard block |
||
| 485 | 485 | ); |
| 486 | 486 | } |
| 487 | 487 | |
| 488 | - if (is_array($target) && isset($target[1])) { |
|
| 488 | + if (is_array($target) && isset($target[1])){ |
|
| 489 | 489 | //In a form of resolver and method |
| 490 | 490 | [$resolver, $method] = $target; |
| 491 | 491 | |
| 492 | 492 | //Resolver instance (i.e. [ClassName::class, 'method']) |
| 493 | 493 | $resolver = $this->get($resolver); |
| 494 | 494 | |
| 495 | - try { |
|
| 495 | + try{ |
|
| 496 | 496 | $method = new ReflectionMethod($resolver, $method); |
| 497 | - } catch (ReflectionException $e) { |
|
| 497 | + }catch (ReflectionException $e){ |
|
| 498 | 498 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 499 | 499 | } |
| 500 | 500 | |
@@ -523,22 +523,22 @@ discard block |
||
| 523 | 523 | */ |
| 524 | 524 | private function createInstance(string $class, array $parameters, string $context = null) |
| 525 | 525 | { |
| 526 | - try { |
|
| 526 | + try{ |
|
| 527 | 527 | $reflection = new ReflectionClass($class); |
| 528 | - } catch (ReflectionException $e) { |
|
| 528 | + }catch (ReflectionException $e){ |
|
| 529 | 529 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 530 | 530 | } |
| 531 | 531 | |
| 532 | 532 | //We have to construct class using external injector when we know exact context |
| 533 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
| 533 | + if ($parameters === [] && $this->checkInjector($reflection)){ |
|
| 534 | 534 | $injector = $this->injectors[$reflection->getName()]; |
| 535 | 535 | |
| 536 | 536 | $instance = null; |
| 537 | - try { |
|
| 537 | + try{ |
|
| 538 | 538 | /** @var InjectorInterface $injectorInstance */ |
| 539 | 539 | $injectorInstance = $this->get($injector); |
| 540 | 540 | |
| 541 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 541 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 542 | 542 | throw new InjectionException( |
| 543 | 543 | sprintf( |
| 544 | 544 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -549,7 +549,7 @@ discard block |
||
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 552 | - if (!$reflection->isInstance($instance)) { |
|
| 552 | + if (!$reflection->isInstance($instance)){ |
|
| 553 | 553 | throw new InjectionException( |
| 554 | 554 | sprintf( |
| 555 | 555 | "Invalid injection response for '%s'", |
@@ -557,23 +557,23 @@ discard block |
||
| 557 | 557 | ) |
| 558 | 558 | ); |
| 559 | 559 | } |
| 560 | - } finally { |
|
| 560 | + }finally{ |
|
| 561 | 561 | $this->injectors[$reflection->getName()] = $injector; |
| 562 | 562 | } |
| 563 | 563 | |
| 564 | 564 | return $instance; |
| 565 | 565 | } |
| 566 | 566 | |
| 567 | - if (!$reflection->isInstantiable()) { |
|
| 567 | + if (!$reflection->isInstantiable()){ |
|
| 568 | 568 | throw new ContainerException(sprintf("Class '%s' can not be constructed", $class)); |
| 569 | 569 | } |
| 570 | 570 | |
| 571 | 571 | $constructor = $reflection->getConstructor(); |
| 572 | 572 | |
| 573 | - if ($constructor !== null) { |
|
| 573 | + if ($constructor !== null){ |
|
| 574 | 574 | // Using constructor with resolved arguments |
| 575 | 575 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
| 576 | - } else { |
|
| 576 | + }else{ |
|
| 577 | 577 | // No constructor specified |
| 578 | 578 | $instance = $reflection->newInstance(); |
| 579 | 579 | } |
@@ -590,27 +590,27 @@ discard block |
||
| 590 | 590 | private function checkInjector(ReflectionClass $reflection): bool |
| 591 | 591 | { |
| 592 | 592 | $class = $reflection->getName(); |
| 593 | - if (array_key_exists($class, $this->injectors)) { |
|
| 593 | + if (array_key_exists($class, $this->injectors)){ |
|
| 594 | 594 | return $this->injectors[$class] !== null; |
| 595 | 595 | } |
| 596 | 596 | |
| 597 | 597 | if ( |
| 598 | 598 | $reflection->implementsInterface(InjectableInterface::class) |
| 599 | 599 | && $reflection->hasConstant('INJECTOR') |
| 600 | - ) { |
|
| 600 | + ){ |
|
| 601 | 601 | $this->injectors[$class] = $reflection->getConstant('INJECTOR'); |
| 602 | 602 | return true; |
| 603 | 603 | } |
| 604 | 604 | |
| 605 | - if (!isset($this->injectorsCache[$class])) { |
|
| 605 | + if (!isset($this->injectorsCache[$class])){ |
|
| 606 | 606 | $this->injectorsCache[$class] = null; |
| 607 | 607 | |
| 608 | 608 | // check interfaces |
| 609 | - foreach ($this->injectors as $target => $injector) { |
|
| 609 | + foreach ($this->injectors as $target => $injector){ |
|
| 610 | 610 | if ( |
| 611 | 611 | class_exists($target, true) |
| 612 | 612 | && $reflection->isSubclassOf($target) |
| 613 | - ) { |
|
| 613 | + ){ |
|
| 614 | 614 | $this->injectors[$class] = $injector; |
| 615 | 615 | return true; |
| 616 | 616 | } |
@@ -618,7 +618,7 @@ discard block |
||
| 618 | 618 | if ( |
| 619 | 619 | interface_exists($target, true) |
| 620 | 620 | && $reflection->implementsInterface($target) |
| 621 | - ) { |
|
| 621 | + ){ |
|
| 622 | 622 | $this->injectors[$class] = $injector; |
| 623 | 623 | return true; |
| 624 | 624 | } |
@@ -640,11 +640,11 @@ discard block |
||
| 640 | 640 | */ |
| 641 | 641 | private function assertType(ReflectionParameter $parameter, ContextFunction $context, $value): void |
| 642 | 642 | { |
| 643 | - if ($value === null) { |
|
| 643 | + if ($value === null){ |
|
| 644 | 644 | if ( |
| 645 | 645 | !$parameter->isOptional() && |
| 646 | 646 | !($parameter->isDefaultValueAvailable() && $parameter->getDefaultValue() === null) |
| 647 | - ) { |
|
| 647 | + ){ |
|
| 648 | 648 | throw new ArgumentException($parameter, $context); |
| 649 | 649 | } |
| 650 | 650 | |
@@ -652,20 +652,20 @@ discard block |
||
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | $type = $parameter->getType(); |
| 655 | - if ($type === null) { |
|
| 655 | + if ($type === null){ |
|
| 656 | 656 | return; |
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | $typeName = $type->getName(); |
| 660 | - if ($typeName === 'array' && !is_array($value)) { |
|
| 660 | + if ($typeName === 'array' && !is_array($value)){ |
|
| 661 | 661 | throw new ArgumentException($parameter, $context); |
| 662 | 662 | } |
| 663 | 663 | |
| 664 | - if (($typeName === 'int' || $typeName === 'float') && !is_numeric($value)) { |
|
| 664 | + if (($typeName === 'int' || $typeName === 'float') && !is_numeric($value)){ |
|
| 665 | 665 | throw new ArgumentException($parameter, $context); |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | - if ($typeName === 'bool' && !is_bool($value) && !is_numeric($value)) { |
|
| 668 | + if ($typeName === 'bool' && !is_bool($value) && !is_numeric($value)){ |
|
| 669 | 669 | throw new ArgumentException($parameter, $context); |
| 670 | 670 | } |
| 671 | 671 | } |
@@ -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 | } |