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

Commit

Permalink
added an abstract factory for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Raidler committed Jul 20, 2013
1 parent f98ab0b commit c48bf81
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions library/Zend/Config/AbstractConfigFactory.php
@@ -0,0 +1,60 @@
<?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\Config;

use Zend\ServiceManager;

/**
* Class AbstractConfigFactory
*/
class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface
{
/**
* @var string
*/
protected $pattern = '#^(.*)\\\\Config$#i';

/**
* Determine if we can create a service with name
*
* @param ServiceManager\ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if (!preg_match($this->pattern, $requestedName, $matches)) {
return false;
}

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

return true;
}

/**
* Create service with name
*
* @param ServiceManager\ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return mixed
*/
public function createServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
preg_match($this->pattern, $requestedName, $matches);
$config = $serviceLocator->get('Config');
return new Config($config[$matches[1]]);
}
}

0 comments on commit c48bf81

Please sign in to comment.