Skip to content

Commit

Permalink
Remove dependency on Orno\Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Bennett committed Jan 12, 2015
1 parent e0b3947 commit 5b3e047
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 159 deletions.
67 changes: 2 additions & 65 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace League\Container;

use Orno\Cache\Cache;
use League\Container\Definition\ClassDefinition;
use League\Container\Definition\ClosureDefinition;
use League\Container\Definition\DefinitionInterface;
Expand All @@ -15,11 +14,6 @@ class Container implements ContainerInterface, \ArrayAccess
*/
protected $factory;

/**
* @var \Orno\Cache\Cache
*/
protected $cache;

/**
* @var array
*/
Expand All @@ -43,17 +37,14 @@ class Container implements ContainerInterface, \ArrayAccess
/**
* Constructor
*
* @param \Orno\Cache\Cache $cache
* @param array|ArrayAccess $config
* @param array|ArrayAccess|ArrayObject $config
* @param \League\Container\Definition\Factory $factory
*/
public function __construct(
Cache $cache = null,
$config = [],
Factory $factory = null
) {
$this->factory = (is_null($factory)) ? new Factory() : $factory;
$this->cache = $cache;
$this->factory = (is_null($factory)) ? new Factory : $factory;

$this->addItemsFromConfig($config);

Expand Down Expand Up @@ -143,20 +134,11 @@ public function get($alias, array $args = [])
return $this->resolveDefinition($alias, $args);
}

// check for and invoke a definition that was reflected on then cached
if ($this->isCaching() && $cached = $this->getCachedDefinition($alias)) {
return $cached;
}

// if we've got this far, we can assume we need to reflect on a class
// and automatically resolve it's dependencies, we also cache the
// result if a caching adapter is available
$definition = $this->reflect($alias);

if ($this->isCaching()) {
$this->cache->set('orno::container::' . $alias, serialize($definition));
}

$this->items[$alias]['definition'] = $definition;

return $definition();
Expand Down Expand Up @@ -209,23 +191,6 @@ protected function resolveDefinition($alias, array $args)
return $return;
}

/**
* Return a cached definition object
*
* @param string $alias
* @return \League\Container\Definition\DefinitionInterface|boolean
*/
protected function getCachedDefinition($alias)
{
if ($cached = $this->cache->get('orno::container::' . $alias)) {
$definition = unserialize($cached);

return $definition();
}

return false;
}

/**
* {@inheritdoc}
*/
Expand All @@ -245,34 +210,6 @@ public function isSingleton($alias)
);
}

/**
* {@inheritdoc}
*/
public function enableCaching()
{
$this->caching = true;

return $this;
}

/**
* {@inheritdoc}
*/
public function disableCaching()
{
$this->caching = false;

return $this;
}

/**
* {@inheritdoc}
*/
public function isCaching()
{
return (! is_null($this->cache) && $this->caching === true);
}

/**
* Encapsulate the definition factory to allow for invokation
*
Expand Down
21 changes: 0 additions & 21 deletions src/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,4 @@ public function isRegistered($alias);
* @return boolean
*/
public function isSingleton($alias);

/**
* Enable caching
*
* @return \League\Container\ContainerInterface
*/
public function enableCaching();

/**
* Disable caching
*
* @return \League\Container\ContainerInterface
*/
public function disableCaching();

/**
* Checks if the container is currently caching reflection results
*
* @return boolean
*/
public function isCaching();
}
81 changes: 8 additions & 73 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,71 +204,6 @@ public function testReflectionThrowsExceptionForArgumentWithNoDefaultValue()
$c->get('League\Container\Test\Asset\FooWithNoDefaultArg');
}

public function testEnablingAndDisablingCachingWorksCorrectly()
{
$cache = $this->getMockBuilder('Orno\Cache\Cache')->disableOriginalConstructor()->getMock();

$c = new Container($cache);

$this->assertTrue($c->isCaching());

$c->disableCaching();

$this->assertFalse($c->isCaching());

$c->enableCaching();

$this->assertTrue($c->isCaching());
}

public function testContainerSetsCacheWhenAvailableAndEnabled()
{
$cache = $this->getMockBuilder('Orno\Cache\Cache')
->setMethods(['get', 'set'])
->disableOriginalConstructor()
->getMock();

$cache->expects($this->once())
->method('set')
->with($this->equalTo('orno::container::League\Container\Test\Asset\Baz'));

$cache->expects($this->once())
->method('get')
->with($this->equalTo('orno::container::League\Container\Test\Asset\Baz'))
->will($this->returnValue(false));

$c = new Container($cache);

$this->assertInstanceOf('League\Container\Test\Asset\Baz', $c->get('League\Container\Test\Asset\Baz'));
}

public function testContainerGetsFromCacheWhenAvailableAndEnabled()
{
$cache = $this->getMockBuilder('Orno\Cache\Cache')
->setMethods(['get', 'set'])
->disableOriginalConstructor()
->getMock();

$definition = $this->getMockBuilder('League\Container\Definition\ClassDefinition')
->disableOriginalConstructor()
->getMock();

$definition->expects($this->any())
->method('__invoke')
->will($this->returnValue(new Asset\Baz));

$definition = serialize($definition);

$cache->expects($this->once())
->method('get')
->with($this->equalTo('orno::container::League\Container\Test\Asset\Baz'))
->will($this->returnValue($definition));

$c = new Container($cache);

$this->assertInstanceOf('League\Container\Test\Asset\Baz', $c->get('League\Container\Test\Asset\Baz'));
}

public function testArrayAccessMapsToCorrectMethods()
{
$c = new Container;
Expand All @@ -286,7 +221,7 @@ public function testArrayAccessMapsToCorrectMethods()

public function testContainerAcceptsArrayWithKey()
{
$c = new Container(null, ['di' => $this->configArray]);
$c = new Container(['di' => $this->configArray]);

$foo = $c->get('League\Container\Test\Asset\Foo');

Expand All @@ -303,7 +238,7 @@ public function testContainerDoesntAcceptArrayWithoutKey()
{
$this->setExpectedException('RuntimeException');

$c = new Container(null, $this->configArray);
$c = new Container($this->configArray);
}

public function testContainerAcceptsArrayAccess()
Expand All @@ -320,7 +255,7 @@ public function testContainerAcceptsArrayAccess()
->will($this->returnValue(true));


$c = new Container(null, $config);
$c = new Container($config);

$foo = $c->get('League\Container\Test\Asset\Foo');

Expand All @@ -337,7 +272,7 @@ public function testContainerDoesntAcceptInvalidConfigType()
{
$this->setExpectedException('InvalidArgumentException');

$c = new Container(null, new \stdClass());
$c = new Container(new \stdClass());
}

public function testExtendThrowsExceptionWhenUnregisteredServiceIsGiven()
Expand Down Expand Up @@ -384,17 +319,17 @@ public function testCallExecutesNamedFunction()
{
$method = '\League\Container\Test\Asset\sayHi';

$c = new Container();
$c = new Container;
$returned = $c->call($method);
$this->assertSame($returned, 'hi');
}

public function testCallExecutesCallableDefinedByArray()
{
$expected = 'qux';
$baz = new BazStatic();
$baz = new BazStatic;

$c = new Container();
$c = new Container;
$returned = $c->call([$baz, 'qux']);

$this->assertSame($returned, $expected);
Expand All @@ -417,7 +352,7 @@ public function testCallExecutesStaticMethod()
$method = '\League\Container\Test\Asset\BazStatic::baz';
$expected = 'qux';

$c = new Container();
$c = new Container;
$returned = $c->call($method, ['foo' => $expected]);
$this->assertSame($returned, $expected);
}
Expand Down

0 comments on commit 5b3e047

Please sign in to comment.