Skip to content

Commit

Permalink
Added League container support
Browse files Browse the repository at this point in the history
  • Loading branch information
inverse authored and localheinz committed Feb 26, 2016
1 parent d212e45 commit cea98bf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Command design pattern. You can also use the `FailoverOnMissContainer` decorator

* [Aura.Di Container](https://github.com/auraphp/Aura.Di/blob/develop/src/Aura/Di/ContainerInterface.php)
* [Laravel Container](https://github.com/laravel/framework/blob/master/src/Illuminate/Container/Container.php)
* [League Container](https://github.com/thephpleague/container/blob/master/src/ContainerInterface.php)
* [Nette DI Container](https://github.com/nette/nette/blob/master/Nette/DI/Container.php)
* [PHP-DI Container](https://github.com/mnapoli/PHP-DI/blob/master/src/DI/Container.php)
* [Pimple](https://github.com/fabpot/Pimple/blob/master/lib/Pimple.php)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": ">=5.4.0",
"aura/di": "^1.0",
"illuminate/container": "^4.0 || ^5.0",
"league/container": "^1.0",
"mnapoli/php-di": "^3.0",
"nette/nette": "^2.1",
"phpunit/phpunit": "^4.0 || ^5.0",
Expand Down
51 changes: 51 additions & 0 deletions src/Adapter/LeagueContainerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Acclimate\Container\Adapter;

use Acclimate\Container\Exception\ContainerException as AcclimateContainerException;
use Acclimate\Container\Exception\NotFoundException as AcclimateNotFoundException;
use Interop\Container\ContainerInterface as AcclimateContainerInterface;
use League\Container\ContainerInterface;
use League\Container\Exception\ReflectionException;

/**
* An adapter from a League Container to the standardized ContainerInterface
*/
class LeagueContainerAdapter implements AcclimateContainerInterface
{

/**
* @var ContainerInterface A League Container
*/
private $container;

/**
* @param ContainerInterface $container A League Container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function get($id)
{
try {
return $this->container->get($id);
} catch (\ReflectionException $prev) {
throw AcclimateNotFoundException::fromPrevious($id, $prev);
} catch (\Exception $prev) {
throw AcclimateContainerException::fromPrevious($id, $prev);
}
}

public function has($id)
{
try {
$this->container->get($id);
} catch (ReflectionException $e) {
return false;
}

return true;
}
}
23 changes: 23 additions & 0 deletions tests/Adapter/LeagueContainerAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Acclimate\Container\Test\Adapter;

use Acclimate\Container\Adapter\LeagueContainerAdapter;
use League\Container\Container;
use RuntimeException;

/**
* @covers \Acclimate\Container\Adapter\LeagueContainerAdapter
*/
class LeagueContainerAdapterTest extends AbstractContainerAdapterTest
{
protected function createContainer()
{
$container = new Container();

$container->add('array_iterator', new \ArrayIterator(range(1, 5)));
$container->add('error', new RuntimeException());

return new LeagueContainerAdapter($container);
}
}

0 comments on commit cea98bf

Please sign in to comment.