Skip to content

Commit

Permalink
Make abstract factories consistent
Browse files Browse the repository at this point in the history
- Consistent getConfig() usage
- Consistent naming
  • Loading branch information
weierophinney committed Apr 30, 2013
1 parent 8231aef commit a728a97
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
17 changes: 11 additions & 6 deletions library/Zend/Cache/Service/StorageCacheAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class StorageCacheAbstractFactory implements AbstractFactoryInterface
protected $configKey = 'caches';

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($serviceLocator);
$config = $this->getConfig($services);
if (empty($config)) {
return false;
}
Expand All @@ -47,14 +47,14 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return \Zend\Cache\Storage\StorageInterface
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($serviceLocator);
$config = $this->getConfig($services);
$config = $config[$requestedName];
return StorageFactory::factory($config);
}
Expand All @@ -71,6 +71,11 @@ protected function getConfig(ServiceLocatorInterface $services)
return $this->config;
}

if (!$services->has('Config')) {
$this->config = array();
return $this->config;
}

$config = $services->get('Config');
if (!isset($config[$this->configKey])) {
$this->config = array();
Expand Down
17 changes: 11 additions & 6 deletions library/Zend/Log/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class LoggerAbstractServiceFactory implements AbstractFactoryInterface
protected $configKey = 'log';

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($serviceLocator);
$config = $this->getConfig($services);
if (empty($config)) {
return false;
}
Expand All @@ -48,14 +48,14 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return Logger
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($serviceLocator);
$config = $this->getConfig($services);
return new Logger($config[$requestedName]);
}

Expand All @@ -71,6 +71,11 @@ protected function getConfig(ServiceLocatorInterface $services)
return $this->config;
}

if (!$services->has('Config')) {
$this->config = array();
return $this->config;
}

$config = $services->get('Config');
if (!isset($config[$this->configKey])) {
$this->config = array();
Expand Down
29 changes: 13 additions & 16 deletions library/Zend/Session/Service/ContainerAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,58 +58,55 @@ class ContainerAbstractFactory implements AbstractFactoryInterface
protected $sessionManager;

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($serviceLocator);
if (!$config) {
$config = $this->getConfig($services);
if (empty($config)) {
return false;
}

$containerName = $this->normalizeContainerName($requestedName);

return array_key_exists($containerName, $config);
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return Container
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$manager = $this->getSessionManager($serviceLocator);
$manager = $this->getSessionManager($services);
return new Container($requestedName, $manager);
}

/**
* Retrieve config from service locator, and cache for later
*
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $services
* @return false|array
*/
protected function getConfig(ServiceLocatorInterface $serviceLocator)
protected function getConfig(ServiceLocatorInterface $services)
{
if (null !== $this->config) {
return $this->config;
}

if (!$serviceLocator->has('Config')) {
if (!$services->has('Config')) {
$this->config = array();

return false;
return $this->config;
}

$config = $serviceLocator->get('Config');
$config = $services->get('Config');
if (!isset($config[$this->configKey]) || !is_array($config[$this->configKey])) {
$this->config = array();

return false;
return $this->config;
}

$config = $config[$this->configKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ public function tearDown()

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

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

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

$this->assertNotSame($cacheA, $cacheB);
Expand Down

0 comments on commit a728a97

Please sign in to comment.