From a3f39568aa874e63ca9c9e36c5d08ea94597635a Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Fri, 31 Jul 2015 12:10:06 +0200 Subject: [PATCH] The container can now be injected by type-hinting Interop\Container\ContainerInterface --- src/Application.php | 4 ++++ tests/FunctionalTest.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Application.php b/src/Application.php index bc09f73..c526e44 100644 --- a/src/Application.php +++ b/src/Application.php @@ -45,8 +45,12 @@ public function __construct(ContainerBuilder $containerBuilder = null, array $va $this->rootContainer->setPimple($this->pimple); $containerBuilder = $containerBuilder ?: new ContainerBuilder(); + $containerBuilder->addDefinitions([ + 'Interop\Container\ContainerInterface' => $this->rootContainer, + ]); $containerBuilder->wrapContainer($this->rootContainer); $this->phpdi = $containerBuilder->build(); + $this->rootContainer->setPhpdi($this->phpdi); parent::__construct($values); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 261c13f..4b16802 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -3,6 +3,7 @@ namespace DI\Bridge\Silex\Test; use DI\ContainerBuilder; +use Interop\Container\ContainerInterface; use stdClass; use Symfony\Component\HttpFoundation\Request; @@ -119,4 +120,24 @@ public function should_pass_pimple_service_based_on_type_hint() $response = $app->handle(Request::create('/')); $this->assertEquals('bar', $response->getContent()); } + + /** + * @test + */ + public function should_pass_the_container_based_on_type_hint() + { + $builder = new ContainerBuilder; + $builder->addDefinitions([ + 'foo' => 'bar', + ]); + + $app = $this->createApplication($builder); + + $app->get('/', function (ContainerInterface $container) { + return $container->get('foo'); + }); + + $response = $app->handle(Request::create('/')); + $this->assertEquals('bar', $response->getContent()); + } }