Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'feature/4860' into develop

Close #4860
  • Loading branch information...
commit e9e3110022bb94624c8088e1c7490be6b59aff29 2 parents 9248490 + d0cd123
@weierophinney weierophinney authored
View
172 library/Zend/Config/AbstractConfigFactory.php
@@ -0,0 +1,172 @@
+<?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 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;
+ }
+}
View
130 tests/ZendTest/Config/AbstractFactoryTest.php
@@ -0,0 +1,130 @@
+<?php
+/**
+ * AbstractFactoryTest.php
+ *
+ * @author Chris Raidler <chris@raidler.com>
+ * @copyright Copyright 2012 - 2013, raidler dot com
+ */
+namespace ZendTest\Config;
+
+use Zend\Config\AbstractConfigFactory;
+use Zend\Mvc\Service\ServiceManagerConfig;
+use Zend\ServiceManager;
+
+/**
+ * Class AbstractFactoryTest
+ */
+class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var \Zend\Mvc\Application
+ */
+ protected $application;
+
+ /**
+ * @var \Zend\ServiceManager\ServiceManager
+ */
+ protected $serviceManager;
+
+ /**
+ * @return void
+ */
+ public function setUp()
+ {
+ $config = array(
+ 'MyModule' => array(
+ 'foo' => array(
+ 'bar'
+ )
+ ),
+ 'phly-blog' => array(
+ 'foo' => array(
+ 'bar'
+ )
+ )
+ );
+
+ $sm = $this->serviceManager = new ServiceManager\ServiceManager(
+ new ServiceManagerConfig(array(
+ 'abstract_factories' => array(
+ 'Zend\Config\AbstractConfigFactory',
+ )
+ ))
+ );
+
+ $sm->setService('Config', $config);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @return void
+ */
+ public function testInvalidPattern()
+ {
+ $factory = new AbstractConfigFactory();
+ $factory->addPattern(new \stdClass());
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @return void
+ */
+ public function testInvalidPatternIterator()
+ {
+ $factory = new AbstractConfigFactory();
+ $factory->addPatterns('invalid');
+ }
+
+ /**
+ * @return void
+ */
+ public function testPatterns()
+ {
+ $factory = new AbstractConfigFactory();
+ $defaults = $factory->getPatterns();
+
+ // Tests that the accessor returns an array
+ $this->assertInternalType('array', $defaults);
+ $this->assertGreaterThan(0, count($defaults));
+
+ // Tests adding a single pattern
+ $this->assertSame($factory, $factory->addPattern('#foobarone#i'));
+ $this->assertCount(count($defaults) + 1, $factory->getPatterns());
+
+ // Tests adding multiple patterns at once
+ $patterns = $factory->getPatterns();
+ $this->assertSame($factory, $factory->addPatterns(array('#foobartwo#i', '#foobarthree#i')));
+ $this->assertCount(count($patterns) + 2, $factory->getPatterns());
+
+ // Tests whether the latest added pattern is the first in stack
+ $patterns = $factory->getPatterns();
+ $this->assertSame('#foobarthree#i', $patterns[0]);
+ }
+
+ /**
+ * @return void
+ */
+ public function testCanCreateService()
+ {
+ $factory = new AbstractConfigFactory();
+ $serviceLocator = $this->serviceManager;
+
+ $this->assertFalse($factory->canCreateServiceWithName($serviceLocator, 'mymodulefail', 'MyModule\Fail'));
+ $this->assertTrue($factory->canCreateServiceWithName($serviceLocator, 'mymoduleconfig', 'MyModule\Config'));
+ }
+
+ /**
+ * @depends testCanCreateService
+ * @return void
+ */
+ public function testCreateService()
+ {
+ $serviceLocator = $this->serviceManager;
+ $this->assertInternalType('array', $serviceLocator->get('MyModule\Config'));
+ $this->assertInternalType('array', $serviceLocator->get('MyModule_Config'));
+ $this->assertInternalType('array', $serviceLocator->get('Config.MyModule'));
+ $this->assertInternalType('array', $serviceLocator->get('phly-blog.config'));
+ $this->assertInternalType('array', $serviceLocator->get('phly-blog-config'));
+ $this->assertInternalType('array', $serviceLocator->get('config-phly-blog'));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.