diff --git a/src/Container.php b/src/Container.php index ad98770..8388216 100644 --- a/src/Container.php +++ b/src/Container.php @@ -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; @@ -15,11 +14,6 @@ class Container implements ContainerInterface, \ArrayAccess */ protected $factory; - /** - * @var \Orno\Cache\Cache - */ - protected $cache; - /** * @var array */ @@ -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); @@ -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(); @@ -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} */ @@ -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 * diff --git a/src/ContainerInterface.php b/src/ContainerInterface.php index dc1b641..a4dc860 100644 --- a/src/ContainerInterface.php +++ b/src/ContainerInterface.php @@ -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(); } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 9601859..b5b3893 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -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; @@ -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'); @@ -303,7 +238,7 @@ public function testContainerDoesntAcceptArrayWithoutKey() { $this->setExpectedException('RuntimeException'); - $c = new Container(null, $this->configArray); + $c = new Container($this->configArray); } public function testContainerAcceptsArrayAccess() @@ -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'); @@ -337,7 +272,7 @@ public function testContainerDoesntAcceptInvalidConfigType() { $this->setExpectedException('InvalidArgumentException'); - $c = new Container(null, new \stdClass()); + $c = new Container(new \stdClass()); } public function testExtendThrowsExceptionWhenUnregisteredServiceIsGiven() @@ -384,7 +319,7 @@ public function testCallExecutesNamedFunction() { $method = '\League\Container\Test\Asset\sayHi'; - $c = new Container(); + $c = new Container; $returned = $c->call($method); $this->assertSame($returned, 'hi'); } @@ -392,9 +327,9 @@ public function testCallExecutesNamedFunction() 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); @@ -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); }