diff --git a/README.md b/README.md index 8c02f4e..85782e5 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Picotainer [![Build Status](https://travis-ci.org/thecodingmachine/picotainer.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/picotainer) [![Coverage Status](https://coveralls.io/repos/thecodingmachine/picotainer/badge.svg?branch=1.0)](https://coveralls.io/r/thecodingmachine/picotainer?branch=1.0) -This package contains a really minimalist dependency injection container (24 lines of code!) compatible with +This package contains a really minimalist dependency injection container (26 lines of code!) compatible with [container-interop](https://github.com/container-interop/container-interop) (supports ContainerInterface and delegate lookup feature). diff --git a/src/MissingDependencyException.php b/src/MissingDependencyException.php new file mode 100644 index 0000000..88f8a1d --- /dev/null +++ b/src/MissingDependencyException.php @@ -0,0 +1,13 @@ + + */ +class MissingDependencyException extends \RuntimeException implements ContainerException +{ +} diff --git a/src/Picotainer.php b/src/Picotainer.php index f7ab588..19be8d3 100644 --- a/src/Picotainer.php +++ b/src/Picotainer.php @@ -56,8 +56,11 @@ public function get($identifier) if (!isset($this->callbacks[$identifier])) { throw new PicotainerNotFoundException(sprintf('Identifier "%s" is not defined.', $identifier)); } - - return $this->objects[$identifier] = $this->callbacks[$identifier]($this->delegateLookupContainer); + try { + return $this->objects[$identifier] = $this->callbacks[$identifier]($this->delegateLookupContainer); + } catch (PicotainerNotFoundException $e) { + throw new MissingDependencyException(sprintf('Entry "%s" cannot be constructed because a dependency is missing.', $identifier, $e)); + } } /* (non-PHPdoc) diff --git a/tests/PicotainerTest.php b/tests/PicotainerTest.php index de595fe..12d124d 100644 --- a/tests/PicotainerTest.php +++ b/tests/PicotainerTest.php @@ -22,13 +22,25 @@ public function testGet() * * @expectedException Mouf\Picotainer\PicotainerNotFoundException */ - public function testGetException() + public function testGetNotFoundException() { $container = new Picotainer([]); $container->get('nonexistant'); } + /** + * + * @expectedException Mouf\Picotainer\MissingDependencyException + */ + public function testGetMissingDependencyException() + { + $container = new Picotainer([ + "instance" => function ($container) { return $container->get('nonfound'); }, + ]); + + $container->get('instance'); + } public function testDelegateContainer() { $container = new Picotainer([