Skip to content

Commit

Permalink
The container can now be injected by type-hinting Interop\Container\C…
Browse files Browse the repository at this point in the history
…ontainerInterface
  • Loading branch information
mnapoli committed Jul 31, 2015
1 parent 5a97b97 commit a3f3956
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DI\Bridge\Silex\Test;

use DI\ContainerBuilder;
use Interop\Container\ContainerInterface;
use stdClass;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit a3f3956

Please sign in to comment.