From b7f5b542bd24922402b37a2fe75c0da4bba55ef3 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 29 Jun 2017 09:08:44 -0300 Subject: [PATCH 1/3] drafting implementation of a PSR-11 compliant container --- composer.json | 1 + src/Illuminate/Container/Container.php | 24 +++++++++++++++++++ .../Container/EntryNotFoundException.php | 10 ++++++++ .../Container/BindingResolutionException.php | 3 ++- .../Contracts/Container/Container.php | 3 ++- tests/Container/ContainerTest.php | 16 +++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/Illuminate/Container/EntryNotFoundException.php diff --git a/composer.json b/composer.json index 4e2aa29df35d..b9d8137557dd 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "monolog/monolog": "~1.12", "mtdowling/cron-expression": "~1.0", "nesbot/carbon": "~1.20", + "psr/container": "~1.0", "ramsey/uuid": "~3.0", "swiftmailer/swiftmailer": "~6.0", "symfony/console": "~3.3", diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 8e33249153d4..99ed901a0ec3 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -5,6 +5,8 @@ use Closure; use ArrayAccess; use LogicException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use ReflectionClass; use ReflectionParameter; use Illuminate\Contracts\Container\BindingResolutionException; @@ -1222,4 +1224,26 @@ public function __set($key, $value) { $this[$key] = $value; } + + /** + * @inheritdoc + */ + public function get($id) + { + if ($this->has($id)) { + return $this->resolve($id); + } + + throw new EntryNotFoundException(); + } + + /** + * @inheritdoc + */ + public function has($id) + { + return $this->getConcrete($id) != $id; + } + + } diff --git a/src/Illuminate/Container/EntryNotFoundException.php b/src/Illuminate/Container/EntryNotFoundException.php new file mode 100644 index 000000000000..9e98b34ce1b3 --- /dev/null +++ b/src/Illuminate/Container/EntryNotFoundException.php @@ -0,0 +1,10 @@ +bind('Illuminate\Tests\Container\IContainerContractStub', 'Illuminate\Tests\Container\ContainerImplementationStub'); $this->assertInstanceOf(ContainerDependentStub::class, $container->build(ContainerDependentStub::class)); } + + public function testContainerKnowsEntry() + { + $container = new Container; + $container->bind('Illuminate\Tests\Container\IContainerContractStub', 'Illuminate\Tests\Container\ContainerImplementationStub'); + $this->assertEquals(true, $container->has('Illuminate\Tests\Container\IContainerContractStub')); + } + + /** + * @expectedException \Illuminate\Container\EntryNotFoundException + */ + public function testUnknownEntry() + { + $container = new Container; + $container->get('Taylor'); + } } class ContainerConcreteStub From d3ca6731230931907d0ada3adb56c8bad654086c Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 29 Jun 2017 09:18:09 -0300 Subject: [PATCH 2/3] StyleCI --- src/Illuminate/Container/Container.php | 8 ++------ src/Illuminate/Container/EntryNotFoundException.php | 2 +- tests/Container/ContainerTest.php | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 99ed901a0ec3..1e47ed13d763 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -5,8 +5,6 @@ use Closure; use ArrayAccess; use LogicException; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; use ReflectionClass; use ReflectionParameter; use Illuminate\Contracts\Container\BindingResolutionException; @@ -1226,7 +1224,7 @@ public function __set($key, $value) } /** - * @inheritdoc + * {@inheritdoc} */ public function get($id) { @@ -1238,12 +1236,10 @@ public function get($id) } /** - * @inheritdoc + * {@inheritdoc} */ public function has($id) { return $this->getConcrete($id) != $id; } - - } diff --git a/src/Illuminate/Container/EntryNotFoundException.php b/src/Illuminate/Container/EntryNotFoundException.php index 9e98b34ce1b3..bdcb6401d78c 100644 --- a/src/Illuminate/Container/EntryNotFoundException.php +++ b/src/Illuminate/Container/EntryNotFoundException.php @@ -7,4 +7,4 @@ class EntryNotFoundException extends Exception implements NotFoundExceptionInterface { -} \ No newline at end of file +} diff --git a/tests/Container/ContainerTest.php b/tests/Container/ContainerTest.php index b40da298ac35..4fe2da9f064a 100755 --- a/tests/Container/ContainerTest.php +++ b/tests/Container/ContainerTest.php @@ -980,7 +980,7 @@ public function testContainerKnowsEntry() /** * @expectedException \Illuminate\Container\EntryNotFoundException */ - public function testUnknownEntry() + public function testUnknownEntryThrowsException() { $container = new Container; $container->get('Taylor'); From 3077e58b6bfea949db0f3728a57865d0a9f48478 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 29 Jun 2017 09:35:35 -0300 Subject: [PATCH 3/3] Rely on bound instead of trying to parse from abstract to concrete --- src/Illuminate/Container/Container.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 1e47ed13d763..2d275916c3c2 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -1240,6 +1240,6 @@ public function get($id) */ public function has($id) { - return $this->getConcrete($id) != $id; + return $this->bound($id); } }