This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added an abstract factory for configs
- Loading branch information
Chris Raidler
committed
Jul 20, 2013
1 parent
f98ab0b
commit c48bf81
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
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,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]]); | ||
} | ||
} |