forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
c93037a
commit c4f2a2a
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
library/Zend/Cache/Service/StorageCacheAbstractFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
tests/ZendTest/Cache/Service/StorageCacheAbstractFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |