Source of file AbstractConfigFactory.php
Size: 4,400 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Config/AbstractConfigFactory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Config; use Traversable; use Zend\ServiceManager; /** * Class AbstractConfigFactory */ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface { /** * @var array */ protected $configs = array(); /** * @var string[] */ protected $defaultPatterns = array( '#config[\._-](.*)$#i', '#^(.*)[\\\\\._-]config$#i' ); /** * @var string[] */ protected $patterns; /** * 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 (isset($this->configs[$requestedName])) { return true; } if (!$serviceLocator->has('Config')) { return false; } $key = $this->match($requestedName); if (null === $key) { return false; } $config = $serviceLocator->get('Config'); return isset($config[$key]); } /** * Create service with name * * @param ServiceManager\ServiceLocatorInterface $serviceLocator * @param string $name * @param string $requestedName * @return string|mixed|array */ public function createServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName) { if (isset($this->configs[$requestedName])) { return $this->configs[$requestedName]; } $key = $this->match($requestedName); if (isset($this->configs[$key])) { $this->configs[$requestedName] = $this->configs[$key]; return $this->configs[$key]; } $config = $serviceLocator->get('Config'); $this->configs[$requestedName] = $this->configs[$key] = $config[$key]; return $config; } /** * @param string $pattern * @return self * @throws Exception\InvalidArgumentException */ public function addPattern($pattern) { if (!is_string($pattern)) { throw new Exception\InvalidArgumentException('pattern must be string'); } $patterns = $this->getPatterns(); array_unshift($patterns, $pattern); $this->setPatterns($patterns); return $this; } /** * @param array|Traversable $patterns * @return self * @throws Exception\InvalidArgumentException */ public function addPatterns($patterns) { if ($patterns instanceof Traversable) { $patterns = iterator_to_array($patterns); } if (!is_array($patterns)) { throw new Exception\InvalidArgumentException("patterns must be array or Traversable"); } foreach ($patterns as $pattern) { $this->addPattern($pattern); } return $this; } /** * @param array|Traversable $patterns * @return self * @throws \InvalidArgumentException */ public function setPatterns($patterns) { if ($patterns instanceof Traversable) { $patterns = iterator_to_array($patterns); } if (!is_array($patterns)) { throw new \InvalidArgumentException("patterns must be array or Traversable"); } $this->patterns = $patterns; return $this; } /** * @return array */ public function getPatterns() { if (null === $this->patterns) { $this->setPatterns($this->defaultPatterns); } return $this->patterns; } /** * @param string $requestedName * @return null|string */ protected function match($requestedName) { foreach ($this->getPatterns() as $pattern) { if (preg_match($pattern, $requestedName, $matches)) { return $matches[1]; } } return null; } } |