Skip to content

Commit

Permalink
Created abstract factory for caches
Browse files Browse the repository at this point in the history
- Allows having multiple cache objects by implementing an abstract
  factory.
- When requesting a cache services, prefix with 'Cache\\', but
  configuration omits that prefix for brevity.
  • Loading branch information
weierophinney committed Apr 29, 2013
1 parent c93037a commit c4f2a2a
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
57 changes: 57 additions & 0 deletions library/Zend/Cache/Service/StorageCacheAbstractFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Cache\Service;

use Zend\Cache\StorageFactory;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Storage cache factory for multiple caches.
*/
class StorageCacheAbstractFactory implements AbstractFactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ('cache\\' !== substr(strtolower($requestedName), 0, 6)) {
return false;
}

$config = $serviceLocator->get('Config');
if (!isset($config['caches'])) {
return false;
}

$config = array_change_key_case($config['caches']);
$service = substr(strtolower($requestedName), 6);
return isset($config[$service]) && is_array($config[$service]);
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return \Zend\Cache\Storage\StorageInterface
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
$config = array_change_key_case($config['caches']);
$service = substr(strtolower($requestedName), 6);
$cacheConfig = $config[$service];
return StorageFactory::factory($cacheConfig);
}
}
75 changes: 75 additions & 0 deletions tests/ZendTest/Cache/Service/StorageCacheAbstractFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Cache
*/

namespace ZendTest\Cache\Service;

use Zend\Cache;
use Zend\ServiceManager\ServiceManager;

/**
* @category Zend
* @package Zend_Cache
* @subpackage UnitTests
* @group Zend_Cache
*/
class StorageCacheAbstractFactoryTest extends \PHPUnit_Framework_TestCase
{
protected $sm;

public function setUp()
{
Cache\StorageFactory::resetAdapterPluginManager();
Cache\StorageFactory::resetPluginManager();
$this->sm = new ServiceManager();
$this->sm->setService('Config', array('caches' => array(
'Memory' => array(
'adapter' => 'Memory',
'plugins' => array('Serializer', 'ClearExpiredByFactor'),
),
'invalid' => array(
'adapter' => 'Memory',
'plugins' => array('Serializer', 'ClearExpiredByFactor'),
),
'Foo' => array(
'adapter' => 'Memory',
'plugins' => array('Serializer', 'ClearExpiredByFactor'),
),
)));
$this->sm->addAbstractFactory('Zend\Cache\Service\StorageCacheAbstractFactory');
}

public function tearDown()
{
Cache\StorageFactory::resetAdapterPluginManager();
Cache\StorageFactory::resetPluginManager();
}

public function testCanLookupCacheByName()
{
$this->assertTrue($this->sm->has('Cache\Memory'));
$this->assertTrue($this->sm->has('Cache\Foo'));
}

public function testCanRetrieveCacheByName()
{
$cacheA = $this->sm->get('Cache\Memory');
$this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cacheA);

$cacheB = $this->sm->get('Cache\Foo');
$this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cacheB);

$this->assertNotSame($cacheA, $cacheB);
}

public function testInvalidCacheServiceNameWillBeIgnored()
{
$this->assertFalse($this->sm->has('invalid'));
}
}

0 comments on commit c4f2a2a

Please sign in to comment.