Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

AbstractFactory consistency #4364

59 changes: 0 additions & 59 deletions library/Zend/Cache/Service/StorageCacheAbstractFactory.php

This file was deleted.

88 changes: 88 additions & 0 deletions library/Zend/Cache/Service/StorageCacheAbstractServiceFactory.php
@@ -0,0 +1,88 @@
<?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 StorageCacheAbstractServiceFactory implements AbstractFactoryInterface
{
/**
* @var array
*/
protected $config;

/**
* Configuration key for cache objects
*
* @var string
*/
protected $configKey = 'caches';

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

return (isset($config[$requestedName]) && is_array($config[$requestedName]));
}

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

/**
* Retrieve cache configuration, if any
*
* @param ServiceLocatorInterface $services
* @return array
*/
protected function getConfig(ServiceLocatorInterface $services)
{
if ($this->config !== null) {
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();
return $this->config;
}

$this->config = $config[$this->configKey];
return $this->config;
}
}
78 changes: 69 additions & 9 deletions library/Zend/Db/Adapter/AdapterAbstractServiceFactory.php
Expand Up @@ -15,25 +15,85 @@
/**
* Database adapter abstract service factory.
*
* Allow configure several database instances (writer and reader).
* Allows configuring several database instances (such as writer and reader).
*/
class AdapterAbstractServiceFactory implements AbstractFactoryInterface
{
/**
* @see \Zend\ServiceManager\AbstractFactoryInterface::canCreateServiceWithName()
* @var array
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
protected $config;

/**
* Can we create an adapter by the requested name?
*
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
return isset($config['db']['adapters'][$requestedName]);
$config = $this->getConfig($services);
if (empty($config)) {
return false;
}

return (
isset($config[$requestedName])
&& is_array($config[$requestedName])
&& !empty($config[$requestedName])
);
}

/**
* @see \Zend\ServiceManager\AbstractFactoryInterface::createServiceWithName()
* Create a DB adapter
*
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return Adapter
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
return new Adapter($config['db']['adapters'][$requestedName]);
$config = $this->getConfig($services);
return new Adapter($config[$requestedName]);
}

/**
* Get db configuration, if any
*
* @param ServiceLocatorInterface $services
* @return array
*/
protected function getConfig(ServiceLocatorInterface $services)
{
if ($this->config !== null) {
return $this->config;
}

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

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

$config = $config['db'];
if (!isset($config['adapters'])
|| !is_array($config['adapters'])
) {
$this->config = array();
return $this->config;
}

$this->config = $config['adapters'];
return $this->config;
}
}
Expand Up @@ -13,22 +13,22 @@
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class FormAbstractFactory implements AbstractFactoryInterface
class FormAbstractServiceFactory implements AbstractFactoryInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to create a new subnamespace? -> Zend\Form\Service\FormAbstractFactory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Because the FormElementManager is already in the component root namespace, it makes no sense to create a new subnamespace. If we'd anticipated multiple SM-related factories originally, yes.

{
/**
* @var string Top-level configuration key indicating forms configuration
* @var array
*/
protected $configKey = 'form_manager';
protected $config;

/**
* @var Factory Form factory used to create forms
* @var string Top-level configuration key indicating forms configuration
*/
protected $factory;
protected $configKey = 'forms';

/**
* @var string Service prefix necessary for abstract factory to trigger
* @var Factory Form factory used to create forms
*/
protected $servicePrefix = 'Form\\';
protected $factory;

/**
* Can we create the requested service?
Expand All @@ -40,36 +40,12 @@ class FormAbstractFactory implements AbstractFactoryInterface
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $rName)
{
if (!$services->has('Config')) {
return false;
}

$prefixLength = strlen($this->servicePrefix);
if (strlen($rName) < $prefixLength
|| substr($rName, 0, $prefixLength) !== $this->servicePrefix
) {
return false;
}

$config = $services->get('Config');
if (!isset($config[$this->configKey])
|| !is_array($config[$this->configKey])
|| empty($config[$this->configKey])
) {
return false;
}

$config = $config[$this->configKey];

$serviceName = substr($rName, $prefixLength);
if (!isset($config[$serviceName])
|| !is_array($config[$serviceName])
|| empty($config[$serviceName])
) {
$config = $this->getConfig($services);
if (empty($config)) {
return false;
}

return true;
return (isset($config[$rName]) && is_array($config[$rName]) && !empty($config[$rName]));
}

/**
Expand All @@ -82,17 +58,43 @@ public function canCreateServiceWithName(ServiceLocatorInterface $services, $nam
*/
public function createServiceWithName(ServiceLocatorInterface $services, $name, $rName)
{
$serviceName = substr($rName, strlen($this->servicePrefix));
$config = $services->get('Config');

$config = $config[$this->configKey][$serviceName];

$config = $this->getConfig($services);
$config = $config[$rName];
$factory = $this->getFormFactory($services);
$this->marshalInputFilter($config, $services, $factory);

$this->marshalInputFilter($config, $services, $factory);
return $factory->createForm($config);
}

/**
* Get forms configuration, if any
*
* @param ServiceLocatorInterface $services
* @return array
*/
protected function getConfig(ServiceLocatorInterface $services)
{
if ($this->config !== null) {
return $this->config;
}

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

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

$this->config = $config[$this->configKey];
return $this->config;
}

/**
* Retrieve the form factory, creating it if necessary
*
Expand Down