Skip to content

Commit a3f3956

Browse files
committed
The container can now be injected by type-hinting Interop\Container\ContainerInterface
1 parent 5a97b97 commit a3f3956

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Application.php

+4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ public function __construct(ContainerBuilder $containerBuilder = null, array $va
4545
$this->rootContainer->setPimple($this->pimple);
4646

4747
$containerBuilder = $containerBuilder ?: new ContainerBuilder();
48+
$containerBuilder->addDefinitions([
49+
'Interop\Container\ContainerInterface' => $this->rootContainer,
50+
]);
4851
$containerBuilder->wrapContainer($this->rootContainer);
4952
$this->phpdi = $containerBuilder->build();
53+
5054
$this->rootContainer->setPhpdi($this->phpdi);
5155

5256
parent::__construct($values);

tests/FunctionalTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DI\Bridge\Silex\Test;
44

55
use DI\ContainerBuilder;
6+
use Interop\Container\ContainerInterface;
67
use stdClass;
78
use Symfony\Component\HttpFoundation\Request;
89

@@ -119,4 +120,24 @@ public function should_pass_pimple_service_based_on_type_hint()
119120
$response = $app->handle(Request::create('/'));
120121
$this->assertEquals('bar', $response->getContent());
121122
}
123+
124+
/**
125+
* @test
126+
*/
127+
public function should_pass_the_container_based_on_type_hint()
128+
{
129+
$builder = new ContainerBuilder;
130+
$builder->addDefinitions([
131+
'foo' => 'bar',
132+
]);
133+
134+
$app = $this->createApplication($builder);
135+
136+
$app->get('/', function (ContainerInterface $container) {
137+
return $container->get('foo');
138+
});
139+
140+
$response = $app->handle(Request::create('/'));
141+
$this->assertEquals('bar', $response->getContent());
142+
}
122143
}

0 commit comments

Comments
 (0)