From 7473f1e4a9e5ae092264a3339714662530fda366 Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Tue, 20 Aug 2013 17:47:35 +0200 Subject: [PATCH 01/38] added call to attachDefaultListeners if method has been defined --- test/AbstractFactoryTest.php | 90 ++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 test/AbstractFactoryTest.php diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php new file mode 100644 index 0000000..bad39cf --- /dev/null +++ b/test/AbstractFactoryTest.php @@ -0,0 +1,90 @@ + + * @copyright Copyright 2012 - 2013, raidler dot com + */ +namespace ZendTest\Config; + +use Zend\Config\AbstractConfigFactory; +use Zend\Config\Config; +use Zend\Mvc\Service\ServiceManagerConfig; +use Zend\ServiceManager\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\Config' => array( + 'foo' => array( + 'bar' + ) + ) + ); + + $appConfig = array( + 'modules' => array(), + 'module_listener_options' => array( + 'config_cache_enabled' => false, + 'cache_dir' => 'data/cache', + 'module_paths' => array(), + ), + ); + + $sm = $this->serviceManager = new ServiceManager( + new ServiceManagerConfig(array( + 'abstract_factories' => array( + 'Zend\Config\AbstractConfigFactory', + ) + )) + ); + + $sm->setService('ApplicationConfig', $appConfig); + $sm->setService('Config', $config); + } + + public function testCanCreateService() + { + $sm = $this->serviceManager; + $factory = new AbstractConfigFactory(); + + $this->assertFalse($factory->canCreateServiceWithName($sm, 'mymodulefail', 'MyModule\Fail')); + $this->assertTrue($factory->canCreateServiceWithName($sm, 'mymoduleconfig', 'MyModule\Config')); + } + + /** + * @depends testCanCreateService + */ + public function testCreateService() + { + $sm = $this->serviceManager; + $this->assertInstanceOf('Zend\Config\Config', $sm->get('MyModule\Config')); + } + + /** + * @depends testCreateService + */ + public function testServiceManager() + { + $sm = $this->serviceManager; + $this->assertInstanceOf('Zend\Config\Config', $sm->get('MyModule\Config')); + } +} \ No newline at end of file From 0ec25d2d82c1a9ec4d484e8e0db312ca0b8775d2 Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Tue, 20 Aug 2013 17:48:47 +0200 Subject: [PATCH 02/38] added abstract factory test --- test/AbstractFactoryTest.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index bad39cf..1aef65a 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -40,15 +40,6 @@ public function setUp() ) ); - $appConfig = array( - 'modules' => array(), - 'module_listener_options' => array( - 'config_cache_enabled' => false, - 'cache_dir' => 'data/cache', - 'module_paths' => array(), - ), - ); - $sm = $this->serviceManager = new ServiceManager( new ServiceManagerConfig(array( 'abstract_factories' => array( @@ -57,7 +48,6 @@ public function setUp() )) ); - $sm->setService('ApplicationConfig', $appConfig); $sm->setService('Config', $config); } From 6251d5cee7df7337ed733bf7d500e364ce6effed Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Tue, 20 Aug 2013 17:53:33 +0200 Subject: [PATCH 03/38] wrong key ... --- test/AbstractFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index 1aef65a..5e70a4d 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -33,7 +33,7 @@ class AbstractFactoryTest extends \PHPUnit_Framework_TestCase public function setUp() { $config = array( - 'MyModule\Config' => array( + 'MyModule' => array( 'foo' => array( 'bar' ) From f0d156231d26a1f6eb18f1073e2c619c9ebef224 Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 11:08:41 +0200 Subject: [PATCH 04/38] added some more functionality --- src/AbstractConfigFactory.php | 111 +++++++++++++++++++++++++++++++--- test/AbstractFactoryTest.php | 78 ++++++++++++++++++++---- 2 files changed, 167 insertions(+), 22 deletions(-) diff --git a/src/AbstractConfigFactory.php b/src/AbstractConfigFactory.php index d0bf5a3..405aec9 100644 --- a/src/AbstractConfigFactory.php +++ b/src/AbstractConfigFactory.php @@ -10,6 +10,7 @@ namespace Zend\Config; use Zend\ServiceManager; +use Zend\Stdlib\ArrayUtils; /** * Class AbstractConfigFactory @@ -17,9 +18,17 @@ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface { /** - * @var string + * @var string[] */ - protected $pattern = '#^(.*)\\\\Config$#i'; + protected $defaultPatterns = array( + '#config[\._-](.*)$#i', + '#^(.*)[\\\\\._-]config$#i' + ); + + /** + * @var string[] + */ + protected $patterns; /** * Determine if we can create a service with name @@ -31,16 +40,17 @@ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface */ public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - if (!preg_match($this->pattern, $requestedName, $matches)) { + if (!$serviceLocator->has('Config')) { return false; } - $config = $serviceLocator->get('Config'); - if (!isset($config[$matches[1]])) { + $key = $this->match($requestedName); + if (null === $key) { return false; } - return true; + $config = $serviceLocator->get('Config'); + return isset($config[$key]); } /** @@ -53,8 +63,91 @@ public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface */ public function createServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - preg_match($this->pattern, $requestedName, $matches); + $key = $this->match($requestedName); $config = $serviceLocator->get('Config'); - return new Config($config[$matches[1]]); + return new Config($config[$key]); + } + + /** + * @param string $pattern + * @return $this + * @throws \InvalidArgumentException + */ + public function addPattern($pattern) + { + if (!is_string($pattern)) { + throw new \InvalidArgumentException('pattern must be string'); + } + + $patterns = $this->getPatterns(); + array_unshift($patterns, $pattern); + $this->setPatterns($patterns); + return $this; + } + + /** + * @param array|\Traversable $patterns + * @return $this + * @throws \InvalidArgumentException + */ + public function addPatterns($patterns) + { + if ($patterns instanceof \Traversable) { + $patterns = ArrayUtils::iteratorToArray($patterns); + } + + if (!is_array($patterns)) { + throw new \InvalidArgumentException("patterns must be array or Traversable"); + } + + foreach ($patterns as $pattern) { + $this->addPattern($pattern); + } + + return $this; + } + + /** + * @param array|\Traversable $patterns + * @return $this + * @throws \InvalidArgumentException + */ + public function setPatterns($patterns) + { + if ($patterns instanceof \Traversable) { + $patterns = ArrayUtils::iteratorToArray($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; } -} +} \ No newline at end of file diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index 5e70a4d..05c9639 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -10,7 +10,7 @@ use Zend\Config\AbstractConfigFactory; use Zend\Config\Config; use Zend\Mvc\Service\ServiceManagerConfig; -use Zend\ServiceManager\ServiceManager; +use Zend\ServiceManager; /** * Class AbstractFactoryTest @@ -37,10 +37,15 @@ public function setUp() 'foo' => array( 'bar' ) + ), + 'phly-blog' => array( + 'foo' => array( + 'bar' + ) ) ); - $sm = $this->serviceManager = new ServiceManager( + $sm = $this->serviceManager = new ServiceManager\ServiceManager( new ServiceManagerConfig(array( 'abstract_factories' => array( 'Zend\Config\AbstractConfigFactory', @@ -51,30 +56,77 @@ public function setUp() $sm->setService('Config', $config); } - public function testCanCreateService() + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidPattern() + { + $factory = new AbstractConfigFactory(); + $factory->addPattern(new \stdClass()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidPatternIterator() + { + $factory = new AbstractConfigFactory(); + $factory->addPatterns('invalid'); + } + + 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->assertEquals(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->assertEquals(count($patterns + 2), count($factory->getPatterns())); + + // Tests whether the latest added pattern is the first in stack + $patterns = $factory->getPatterns(); + $this->assertSame('#foobarthree#i', $patterns[0]); + } + + /** + * @dataProvider provideServiceLocator + */ + public function testCanCreateService($serviceLocator) { - $sm = $this->serviceManager; $factory = new AbstractConfigFactory(); - $this->assertFalse($factory->canCreateServiceWithName($sm, 'mymodulefail', 'MyModule\Fail')); - $this->assertTrue($factory->canCreateServiceWithName($sm, 'mymoduleconfig', 'MyModule\Config')); + $this->assertFalse($factory->canCreateServiceWithName($serviceLocator, 'mymodulefail', 'MyModule\Fail')); + $this->assertTrue($factory->canCreateServiceWithName($serviceLocator, 'mymoduleconfig', 'MyModule\Config')); } /** * @depends testCanCreateService + * @dataProvider provideServiceLocator */ - public function testCreateService() + public function testCreateService($serviceLocator) { - $sm = $this->serviceManager; - $this->assertInstanceOf('Zend\Config\Config', $sm->get('MyModule\Config')); + $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('MyModule\Config')); + $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('MyModule_Config')); + $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('Config.MyModule')); + $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('phly-blog.config')); + $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('phly-blog-config')); + $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('config-phly-blog')); } /** - * @depends testCreateService + * @return ServiceManager\ServiceLocatorInterface */ - public function testServiceManager() + public function provideServiceLocator() { - $sm = $this->serviceManager; - $this->assertInstanceOf('Zend\Config\Config', $sm->get('MyModule\Config')); + return $this->serviceManager; } } \ No newline at end of file From fe8ffb39e03e587c6d0d8b4d1888ecd21dd77f29 Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 11:24:43 +0200 Subject: [PATCH 05/38] fixed assertion and empty line at file ending --- src/AbstractConfigFactory.php | 2 +- test/AbstractFactoryTest.php | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/AbstractConfigFactory.php b/src/AbstractConfigFactory.php index 405aec9..2a43dc8 100644 --- a/src/AbstractConfigFactory.php +++ b/src/AbstractConfigFactory.php @@ -150,4 +150,4 @@ protected function match($requestedName) } return null; } -} \ No newline at end of file +} diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index 05c9639..aee29f7 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -58,6 +58,7 @@ public function setUp() /** * @expectedException InvalidArgumentException + * @return void */ public function testInvalidPattern() { @@ -67,6 +68,7 @@ public function testInvalidPattern() /** * @expectedException InvalidArgumentException + * @return void */ public function testInvalidPatternIterator() { @@ -74,6 +76,9 @@ public function testInvalidPatternIterator() $factory->addPatterns('invalid'); } + /** + * @return void + */ public function testPatterns() { $factory = new AbstractConfigFactory(); @@ -85,7 +90,7 @@ public function testPatterns() // Tests adding a single pattern $this->assertSame($factory, $factory->addPattern('#foobarone#i')); - $this->assertEquals(count($defaults) + 1, $factory->getPatterns()); + $this->assertEquals(count($defaults) + 1, count($factory->getPatterns())); // Tests adding multiple patterns at once $patterns = $factory->getPatterns(); @@ -111,6 +116,7 @@ public function testCanCreateService($serviceLocator) /** * @depends testCanCreateService * @dataProvider provideServiceLocator + * @return void */ public function testCreateService($serviceLocator) { @@ -123,10 +129,10 @@ public function testCreateService($serviceLocator) } /** - * @return ServiceManager\ServiceLocatorInterface + * @return array */ public function provideServiceLocator() { - return $this->serviceManager; + return array($this->serviceManager); } } \ No newline at end of file From d29c0d8c275a5c222e86e1235ae725a312fd358a Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 11:36:55 +0200 Subject: [PATCH 06/38] fix more assertions and more file endings ... --- test/AbstractFactoryTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index aee29f7..49e622a 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -90,12 +90,12 @@ public function testPatterns() // Tests adding a single pattern $this->assertSame($factory, $factory->addPattern('#foobarone#i')); - $this->assertEquals(count($defaults) + 1, count($factory->getPatterns())); + $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->assertEquals(count($patterns + 2), count($factory->getPatterns())); + $this->assertCount(count($patterns + 2), $factory->getPatterns()); // Tests whether the latest added pattern is the first in stack $patterns = $factory->getPatterns(); @@ -135,4 +135,4 @@ public function provideServiceLocator() { return array($this->serviceManager); } -} \ No newline at end of file +} From 2551dd9be83b760128d846d255766d23a961163f Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 11:52:39 +0200 Subject: [PATCH 07/38] fix assertion --- test/AbstractFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index 49e622a..fd9edc3 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -95,7 +95,7 @@ public function testPatterns() // 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()); + $this->assertCount(count($patterns) + 2, $factory->getPatterns()); // Tests whether the latest added pattern is the first in stack $patterns = $factory->getPatterns(); From 4fcf90501153cd78f2981571b5a770589c467f32 Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 12:02:03 +0200 Subject: [PATCH 08/38] fix data provider --- test/AbstractFactoryTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index fd9edc3..e9bc8f8 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -133,6 +133,11 @@ public function testCreateService($serviceLocator) */ public function provideServiceLocator() { - return array($this->serviceManager); + return array( + array( + $this->serviceManager, + $this->serviceManager + ) + ); } } From 88ea300fc60b8c7bdc56117c0498bbfba73b317d Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 12:24:20 +0200 Subject: [PATCH 09/38] fix data provider --- test/AbstractFactoryTest.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index e9bc8f8..71a7d7e 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -103,11 +103,12 @@ public function testPatterns() } /** - * @dataProvider provideServiceLocator + * @return void */ - public function testCanCreateService($serviceLocator) + 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')); @@ -115,11 +116,11 @@ public function testCanCreateService($serviceLocator) /** * @depends testCanCreateService - * @dataProvider provideServiceLocator * @return void */ - public function testCreateService($serviceLocator) + public function testCreateService() { + $serviceLocator = $this->serviceManager; $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('MyModule\Config')); $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('MyModule_Config')); $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('Config.MyModule')); @@ -127,17 +128,4 @@ public function testCreateService($serviceLocator) $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('phly-blog-config')); $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('config-phly-blog')); } - - /** - * @return array - */ - public function provideServiceLocator() - { - return array( - array( - $this->serviceManager, - $this->serviceManager - ) - ); - } } From 148000c2dc6eff9fdd4813ba77654d5980e10e0f Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 19:21:05 +0200 Subject: [PATCH 10/38] incorporated feedback from @weierophinney --- src/AbstractConfigFactory.php | 46 +++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/src/AbstractConfigFactory.php b/src/AbstractConfigFactory.php index 2a43dc8..4beaaa6 100644 --- a/src/AbstractConfigFactory.php +++ b/src/AbstractConfigFactory.php @@ -9,14 +9,19 @@ namespace Zend\Config; +use Traversable; use Zend\ServiceManager; -use Zend\Stdlib\ArrayUtils; /** * Class AbstractConfigFactory */ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface { + /** + * @var Config[] + */ + protected $configs = array(); + /** * @var string[] */ @@ -40,6 +45,10 @@ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface */ public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName) { + if (isset($this->configs[$requestedName])) { + return true; + } + if (!$serviceLocator->has('Config')) { return false; } @@ -59,24 +68,35 @@ public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface * @param ServiceManager\ServiceLocatorInterface $serviceLocator * @param string $name * @param string $requestedName - * @return mixed + * @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'); - return new Config($config[$key]); + $config = new Config($config[$key]); + $this->configs[$requestedName] = $this->configs[$key] = $config; + return $config; } /** * @param string $pattern * @return $this - * @throws \InvalidArgumentException + * @throws Exception\InvalidArgumentException */ public function addPattern($pattern) { if (!is_string($pattern)) { - throw new \InvalidArgumentException('pattern must be string'); + throw new Exception\InvalidArgumentException('pattern must be string'); } $patterns = $this->getPatterns(); @@ -86,18 +106,18 @@ public function addPattern($pattern) } /** - * @param array|\Traversable $patterns + * @param array|Traversable $patterns * @return $this - * @throws \InvalidArgumentException + * @throws Exception\InvalidArgumentException */ public function addPatterns($patterns) { - if ($patterns instanceof \Traversable) { - $patterns = ArrayUtils::iteratorToArray($patterns); + if ($patterns instanceof Traversable) { + $patterns = iterator_to_array($patterns); } if (!is_array($patterns)) { - throw new \InvalidArgumentException("patterns must be array or Traversable"); + throw new Exception\InvalidArgumentException("patterns must be array or Traversable"); } foreach ($patterns as $pattern) { @@ -108,14 +128,14 @@ public function addPatterns($patterns) } /** - * @param array|\Traversable $patterns + * @param array|Traversable $patterns * @return $this * @throws \InvalidArgumentException */ public function setPatterns($patterns) { - if ($patterns instanceof \Traversable) { - $patterns = ArrayUtils::iteratorToArray($patterns); + if ($patterns instanceof Traversable) { + $patterns = iterator_to_array($patterns); } if (!is_array($patterns)) { From d1753fd5dbdb7207f4f7e9fdc0cabae604187676 Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 23:17:35 +0200 Subject: [PATCH 11/38] removed instantiation of Zend\Config\Config, adjusted test --- src/AbstractConfigFactory.php | 5 ++--- test/AbstractFactoryTest.php | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/AbstractConfigFactory.php b/src/AbstractConfigFactory.php index 4beaaa6..29c2052 100644 --- a/src/AbstractConfigFactory.php +++ b/src/AbstractConfigFactory.php @@ -18,7 +18,7 @@ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface { /** - * @var Config[] + * @var array */ protected $configs = array(); @@ -83,8 +83,7 @@ public function createServiceWithName(ServiceManager\ServiceLocatorInterface $se } $config = $serviceLocator->get('Config'); - $config = new Config($config[$key]); - $this->configs[$requestedName] = $this->configs[$key] = $config; + $this->configs[$requestedName] = $this->configs[$key] = $config[$key]; return $config; } diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index 71a7d7e..ebcb048 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -121,11 +121,11 @@ public function testCanCreateService() public function testCreateService() { $serviceLocator = $this->serviceManager; - $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('MyModule\Config')); - $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('MyModule_Config')); - $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('Config.MyModule')); - $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('phly-blog.config')); - $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('phly-blog-config')); - $this->assertInstanceOf('Zend\Config\Config', $serviceLocator->get('config-phly-blog')); + $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')); } } From 7846df7f36ddf33cdf921d819bb29c355760dc4c Mon Sep 17 00:00:00 2001 From: Chris Raidler Date: Wed, 21 Aug 2013 23:26:20 +0200 Subject: [PATCH 12/38] replaced @return $this with @return self --- src/AbstractConfigFactory.php | 6 +++--- test/AbstractFactoryTest.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/AbstractConfigFactory.php b/src/AbstractConfigFactory.php index 29c2052..5345db8 100644 --- a/src/AbstractConfigFactory.php +++ b/src/AbstractConfigFactory.php @@ -89,7 +89,7 @@ public function createServiceWithName(ServiceManager\ServiceLocatorInterface $se /** * @param string $pattern - * @return $this + * @return self * @throws Exception\InvalidArgumentException */ public function addPattern($pattern) @@ -106,7 +106,7 @@ public function addPattern($pattern) /** * @param array|Traversable $patterns - * @return $this + * @return self * @throws Exception\InvalidArgumentException */ public function addPatterns($patterns) @@ -128,7 +128,7 @@ public function addPatterns($patterns) /** * @param array|Traversable $patterns - * @return $this + * @return self * @throws \InvalidArgumentException */ public function setPatterns($patterns) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index ebcb048..850dc13 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -8,7 +8,6 @@ namespace ZendTest\Config; use Zend\Config\AbstractConfigFactory; -use Zend\Config\Config; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager; From ca1f8c5878265210d0bcae22ce585ec6579cb536 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 10:23:31 -0700 Subject: [PATCH 13/38] Modified to build out formatted PHP code by hand for greater speed --- src/Writer/PhpArray.php | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index c2b46c8..f9c2364 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -19,25 +19,33 @@ class PhpArray extends AbstractWriter */ public function processConfig(array $config) { - $arrayString = "processIndented($config) . ");\n"; + } + + protected function processIndented(array $config, &$indentLevel = 1) + { + $arrayString = ""; - foreach (explode("\n", var_export($config, true)) as $line) { - $line = trim($line); + foreach ($config as $key => $value) { + $arrayString .= str_repeat(' ', $indentLevel); + $arrayString .= (is_numeric($key) ? $key : "'" . addslashes($key) . "'") . ' => '; - if ($line === '),' || $line === ')') { - $indentLevel--; - } else if (preg_match('/^\s*array \(/', $line)) { - $line = 'array('; - $indentLevel++; - $arrayString .= ' ' . $line; - continue; + if (is_array($value)) { + if ($value === array()) { + $arrayString .= "array(),\n"; + } else { + $indentLevel++; + $arrayString .= "array(\n" . $this->processIndented($value, $indentLevel) + . str_repeat(' ', --$indentLevel) . "),\n"; + } + } elseif (is_object($value)) { + $arrayString .= var_export($value, true) . ",\n"; + } else { + $arrayString .= "'" . addslashes($value) . "',\n"; } - - $arrayString .= "\n" . str_repeat(' ', $indentLevel) . $line; } - return $arrayString . ";\n"; + return $arrayString; } } From 4871b443f94e812b686e89a1a909e679d3a38d92 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 10:39:22 -0700 Subject: [PATCH 14/38] Optional support for PHP 5.4+ "[]" array syntax. --- src/Writer/PhpArray.php | 31 ++++++++++++++++++++++++++----- test/Writer/PhpArrayTest.php | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index f9c2364..88a8dd6 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -11,6 +11,11 @@ class PhpArray extends AbstractWriter { + /** + * @var bool + */ + protected $useBracketArraySyntax = false; + /** * processConfig(): defined by AbstractWriter. * @@ -19,11 +24,26 @@ class PhpArray extends AbstractWriter */ public function processConfig(array $config) { + $array = array( + 'open' => $this->useBracketArraySyntax ? '[' : 'array(', + 'close' => $this->useBracketArraySyntax ? ']' : ')' + ); + return "processIndented($config) . ");\n"; + "return " . $array['open'] . "\n" . $this->processIndented($config, $array) . $array['close'] . ";\n"; + } + + /** + * Sets whether or not to use the PHP 5.4+ "[]" array syntax. + * + * @param bool $value + */ + public function setUseBracketArraySyntax($value) + { + $this->useBracketArraySyntax = $value; } - protected function processIndented(array $config, &$indentLevel = 1) + protected function processIndented(array $config, array $array, &$indentLevel = 1) { $arrayString = ""; @@ -33,11 +53,12 @@ protected function processIndented(array $config, &$indentLevel = 1) if (is_array($value)) { if ($value === array()) { - $arrayString .= "array(),\n"; + $arrayString .= $array['open'] . $array['close'] . ",\n"; } else { $indentLevel++; - $arrayString .= "array(\n" . $this->processIndented($value, $indentLevel) - . str_repeat(' ', --$indentLevel) . "),\n"; + $arrayString .= $array['open'] . "\n" + . $this->processIndented($value, $array, $indentLevel) + . str_repeat(' ', --$indentLevel) . $array['close'] . ",\n"; } } elseif (is_object($value)) { $arrayString .= var_export($value, true) . ",\n"; diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 208bb97..016875e 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -47,4 +47,24 @@ public function testRender() $this->assertEquals($expected, $configString); } + + public function testRenderWithBracketArraySyntax() + { + $config = new Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'))); + + $this->writer->setUseBracketArraySyntax(true); + $configString = $this->writer->toString($config); + + // build string line by line as we are trailing-whitespace sensitive. + $expected = " 'foo',\n"; + $expected .= " 'bar' => [\n"; + $expected .= " 0 => 'baz',\n"; + $expected .= " 1 => 'foo',\n"; + $expected .= " ],\n"; + $expected .= "];\n"; + + $this->assertEquals($expected, $configString); + } } From 748ea10b0b64f240594cc5a0c18ef0e76debf782 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 10:57:17 -0700 Subject: [PATCH 15/38] Using is_int() instead of is_numeric() to avoid unintentional loss of string keys --- src/Writer/PhpArray.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 88a8dd6..531b3be 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -49,7 +49,7 @@ protected function processIndented(array $config, array $array, &$indentLevel = foreach ($config as $key => $value) { $arrayString .= str_repeat(' ', $indentLevel); - $arrayString .= (is_numeric($key) ? $key : "'" . addslashes($key) . "'") . ' => '; + $arrayString .= (is_int($key) ? $key : "'" . addslashes($key) . "'") . ' => '; if (is_array($value)) { if ($value === array()) { From b51ee971deb52391faa5cfa34f50de0bac59eb23 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 10:59:16 -0700 Subject: [PATCH 16/38] Using a constant for the four-space indent string --- src/Writer/PhpArray.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 531b3be..61628e7 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -11,6 +11,11 @@ class PhpArray extends AbstractWriter { + /** + * @var string + */ + const INDENT_STRING = ' '; + /** * @var bool */ @@ -48,7 +53,7 @@ protected function processIndented(array $config, array $array, &$indentLevel = $arrayString = ""; foreach ($config as $key => $value) { - $arrayString .= str_repeat(' ', $indentLevel); + $arrayString .= str_repeat(self::INDENT_STRING, $indentLevel); $arrayString .= (is_int($key) ? $key : "'" . addslashes($key) . "'") . ' => '; if (is_array($value)) { @@ -58,7 +63,7 @@ protected function processIndented(array $config, array $array, &$indentLevel = $indentLevel++; $arrayString .= $array['open'] . "\n" . $this->processIndented($value, $array, $indentLevel) - . str_repeat(' ', --$indentLevel) . $array['close'] . ",\n"; + . str_repeat(self::INDENT_STRING, --$indentLevel) . $array['close'] . ",\n"; } } elseif (is_object($value)) { $arrayString .= var_export($value, true) . ",\n"; From a85beb6c79432f9d8a3f3951eeee7e2333d3dabe Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 11:01:49 -0700 Subject: [PATCH 17/38] Added docblock for ::processIndented(...) --- src/Writer/PhpArray.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 61628e7..453371d 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -48,6 +48,14 @@ public function setUseBracketArraySyntax($value) $this->useBracketArraySyntax = $value; } + /** + * Recursively processes a PHP config array structure into a readable format. + * + * @param array $config + * @param array $array + * @param int $indentLevel + * @return string + */ protected function processIndented(array $config, array $array, &$indentLevel = 1) { $arrayString = ""; From 7b9c7a254d3d8c9ae20450bd1bce399f76d47538 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 11:03:09 -0700 Subject: [PATCH 18/38] s/$array/$arraySyntax/g for clarity --- src/Writer/PhpArray.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 453371d..0623cd5 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -29,13 +29,14 @@ class PhpArray extends AbstractWriter */ public function processConfig(array $config) { - $array = array( + $arraySyntax = array( 'open' => $this->useBracketArraySyntax ? '[' : 'array(', 'close' => $this->useBracketArraySyntax ? ']' : ')' ); return "processIndented($config, $array) . $array['close'] . ";\n"; + "return " . $arraySyntax['open'] . "\n" . $this->processIndented($config, $arraySyntax) . + $arraySyntax['close'] . ";\n"; } /** @@ -52,11 +53,11 @@ public function setUseBracketArraySyntax($value) * Recursively processes a PHP config array structure into a readable format. * * @param array $config - * @param array $array + * @param array $arraySyntax * @param int $indentLevel * @return string */ - protected function processIndented(array $config, array $array, &$indentLevel = 1) + protected function processIndented(array $config, array $arraySyntax, &$indentLevel = 1) { $arrayString = ""; @@ -66,12 +67,12 @@ protected function processIndented(array $config, array $array, &$indentLevel = if (is_array($value)) { if ($value === array()) { - $arrayString .= $array['open'] . $array['close'] . ",\n"; + $arrayString .= $arraySyntax['open'] . $arraySyntax['close'] . ",\n"; } else { $indentLevel++; - $arrayString .= $array['open'] . "\n" - . $this->processIndented($value, $array, $indentLevel) - . str_repeat(self::INDENT_STRING, --$indentLevel) . $array['close'] . ",\n"; + $arrayString .= $arraySyntax['open'] . "\n" + . $this->processIndented($value, $arraySyntax, $indentLevel) + . str_repeat(self::INDENT_STRING, --$indentLevel) . $arraySyntax['close'] . ",\n"; } } elseif (is_object($value)) { $arrayString .= var_export($value, true) . ",\n"; From 9337da4c9ad37fd384885e815e98d25c4dc774b6 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 11:04:30 -0700 Subject: [PATCH 19/38] Added @return void for consistency. Aligned docblock variable names --- src/Writer/PhpArray.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 0623cd5..8689f18 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -42,7 +42,8 @@ public function processConfig(array $config) /** * Sets whether or not to use the PHP 5.4+ "[]" array syntax. * - * @param bool $value + * @param bool $value + * @return void */ public function setUseBracketArraySyntax($value) { @@ -52,9 +53,9 @@ public function setUseBracketArraySyntax($value) /** * Recursively processes a PHP config array structure into a readable format. * - * @param array $config - * @param array $arraySyntax - * @param int $indentLevel + * @param array $config + * @param array $arraySyntax + * @param int $indentLevel * @return string */ protected function processIndented(array $config, array $arraySyntax, &$indentLevel = 1) From 7b7433509d6f95cf3be8b906efbedd8ee2cb6e32 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 12 Oct 2013 14:26:16 -0700 Subject: [PATCH 20/38] Removed the extra line break --- src/Writer/PhpArray.php | 2 +- test/FactoryTest.php | 2 +- test/Writer/PhpArrayTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 8689f18..19bd347 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -34,7 +34,7 @@ public function processConfig(array $config) 'close' => $this->useBracketArraySyntax ? ']' : ')' ); - return "processIndented($config, $arraySyntax) . $arraySyntax['close'] . ";\n"; } diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 49c51e6..a19f5e3 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -176,7 +176,7 @@ public function testFactoryWriteToFile() $result = Factory::toFile($file, $config); // build string line by line as we are trailing-whitespace sensitive. - $expected = " 'foo',\n"; $expected .= " 'bar' => array(\n"; diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 016875e..f23b7fa 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -36,7 +36,7 @@ public function testRender() $configString = $this->writer->toString($config); // build string line by line as we are trailing-whitespace sensitive. - $expected = " 'foo',\n"; $expected .= " 'bar' => array(\n"; @@ -56,7 +56,7 @@ public function testRenderWithBracketArraySyntax() $configString = $this->writer->toString($config); // build string line by line as we are trailing-whitespace sensitive. - $expected = " 'foo',\n"; $expected .= " 'bar' => [\n"; From c0cb7909a17269a1cb04d58b20df001d26ceeaaa Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Tue, 15 Oct 2013 14:37:49 -0400 Subject: [PATCH 21/38] Added missing test case --- test/Writer/PhpArrayTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index f23b7fa..6404780 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -31,7 +31,10 @@ public function setUp() */ public function testRender() { - $config = new Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'))); + $config = new Config(array( + 'test' => 'foo', + 'bar' => array(0 => 'baz', 1 => 'foo'), + 'emptyArray' => array())); $configString = $this->writer->toString($config); @@ -43,6 +46,7 @@ public function testRender() $expected .= " 0 => 'baz',\n"; $expected .= " 1 => 'foo',\n"; $expected .= " ),\n"; + $expected .= " 'emptyArray' => array(),\n"; $expected .= ");\n"; $this->assertEquals($expected, $configString); @@ -50,7 +54,7 @@ public function testRender() public function testRenderWithBracketArraySyntax() { - $config = new Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'))); + $config = new Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'), 'emptyArray' => array())); $this->writer->setUseBracketArraySyntax(true); $configString = $this->writer->toString($config); @@ -63,6 +67,7 @@ public function testRenderWithBracketArraySyntax() $expected .= " 0 => 'baz',\n"; $expected .= " 1 => 'foo',\n"; $expected .= " ],\n"; + $expected .= " 'emptyArray' => [],\n"; $expected .= "];\n"; $this->assertEquals($expected, $configString); From 47835db76968c2c28d8a48c7aa5c2cd79e5921d1 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Tue, 15 Oct 2013 14:39:58 -0400 Subject: [PATCH 22/38] Added Fluent interface for the bracket array syntax setter --- src/Writer/PhpArray.php | 3 ++- test/Writer/PhpArrayTest.php | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 19bd347..3436107 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -43,11 +43,12 @@ public function processConfig(array $config) * Sets whether or not to use the PHP 5.4+ "[]" array syntax. * * @param bool $value - * @return void + * @return self */ public function setUseBracketArraySyntax($value) { $this->useBracketArraySyntax = $value; + return $this; } /** diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 6404780..8070983 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -72,4 +72,9 @@ public function testRenderWithBracketArraySyntax() $this->assertEquals($expected, $configString); } + + public function testSetUseBracketArraySyntaxReturnsFluentInterface() + { + $this->assertSame($this->writer, $this->writer->setUseBracketArraySyntax(true)); + } } From bd9f309ce685d11caf2b37ac208cd99014ed825e Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Tue, 15 Oct 2013 14:56:50 -0400 Subject: [PATCH 23/38] Added missing test case --- test/Writer/PhpArrayTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 8070983..06d62a6 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -31,10 +31,13 @@ public function setUp() */ public function testRender() { - $config = new Config(array( + $configArray = array( 'test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'), - 'emptyArray' => array())); + 'emptyArray' => array(), + 'object' => (object) array('foo' => 'bar') + ); + $config = new Config($configArray); $configString = $this->writer->toString($config); @@ -47,6 +50,9 @@ public function testRender() $expected .= " 1 => 'foo',\n"; $expected .= " ),\n"; $expected .= " 'emptyArray' => array(),\n"; + $expected .= " 'object' => stdClass::__set_state(array(\n"; + $expected .= " 'foo' => 'bar',\n"; + $expected .= ")),\n"; $expected .= ");\n"; $this->assertEquals($expected, $configString); From 5fff600bde2d4f4c61d4a484db33f278d85f2ede Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Tue, 15 Oct 2013 15:19:37 -0400 Subject: [PATCH 24/38] Did not mean to commit this - removing. --- test/Writer/PhpArrayTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 06d62a6..72fb475 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -31,13 +31,12 @@ public function setUp() */ public function testRender() { - $configArray = array( + $config = new Config(array( 'test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'), 'emptyArray' => array(), 'object' => (object) array('foo' => 'bar') - ); - $config = new Config($configArray); + )); $configString = $this->writer->toString($config); From f058b33d0be0c6117ba0b532e4dc349bf0196e44 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Sat, 30 Nov 2013 16:32:30 -0500 Subject: [PATCH 25/38] Fixed boolean/integer BC break. Booleans and integers were being inadvertently converted to strings when saved in the config array. This preserves the datatype. --- src/Writer/PhpArray.php | 6 +++++- test/Writer/PhpArrayTest.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 3436107..3d78a8d 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -78,8 +78,12 @@ protected function processIndented(array $config, array $arraySyntax, &$indentLe } } elseif (is_object($value)) { $arrayString .= var_export($value, true) . ",\n"; - } else { + } else if (is_bool($value)) { + $arrayString .= ($value ? 'true' : 'false') . ",\n"; + } elseif (is_string($value)) { $arrayString .= "'" . addslashes($value) . "',\n"; + } else { + $arrayString .= $value . ",\n"; } } diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 72fb475..0a00e47 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -35,7 +35,9 @@ public function testRender() 'test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'), 'emptyArray' => array(), - 'object' => (object) array('foo' => 'bar') + 'object' => (object) array('foo' => 'bar'), + 'integer' => 123, + 'boolean' => false, )); $configString = $this->writer->toString($config); @@ -52,6 +54,8 @@ public function testRender() $expected .= " 'object' => stdClass::__set_state(array(\n"; $expected .= " 'foo' => 'bar',\n"; $expected .= ")),\n"; + $expected .= " 'integer' => 123,\n"; + $expected .= " 'boolean' => false,\n"; $expected .= ");\n"; $this->assertEquals($expected, $configString); From 118fc7eeb48d27972e329ec51a3def94090baff7 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 6 Dec 2013 13:54:28 -0600 Subject: [PATCH 26/38] [zendframework/zf2#5569] CS fixes - elseif -> else if --- src/Writer/PhpArray.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 3d78a8d..0eed213 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -78,7 +78,7 @@ protected function processIndented(array $config, array $arraySyntax, &$indentLe } } elseif (is_object($value)) { $arrayString .= var_export($value, true) . ",\n"; - } else if (is_bool($value)) { + } elseif (is_bool($value)) { $arrayString .= ($value ? 'true' : 'false') . ",\n"; } elseif (is_string($value)) { $arrayString .= "'" . addslashes($value) . "',\n"; From b0794282a6ca58b3ee46910a6420f2301d6aa193 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sat, 7 Dec 2013 11:05:49 -0600 Subject: [PATCH 27/38] [zendframework/zf2#5569] Handle null values properly - Previous fix led to: ```php 'null' => , ``` - Fix properly echoes the null value: ```php 'null' => null, ``` --- src/Writer/PhpArray.php | 2 ++ test/Writer/PhpArrayTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 0eed213..ee9c724 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -82,6 +82,8 @@ protected function processIndented(array $config, array $arraySyntax, &$indentLe $arrayString .= ($value ? 'true' : 'false') . ",\n"; } elseif (is_string($value)) { $arrayString .= "'" . addslashes($value) . "',\n"; + } elseif ($value === null) { + $arrayString .= "null,\n"; } else { $arrayString .= $value . ",\n"; } diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 0a00e47..9ed3e05 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -38,6 +38,7 @@ public function testRender() 'object' => (object) array('foo' => 'bar'), 'integer' => 123, 'boolean' => false, + 'null' => null, )); $configString = $this->writer->toString($config); @@ -56,6 +57,7 @@ public function testRender() $expected .= ")),\n"; $expected .= " 'integer' => 123,\n"; $expected .= " 'boolean' => false,\n"; + $expected .= " 'null' => null,\n"; $expected .= ");\n"; $this->assertEquals($expected, $configString); From 535015e5dc65391e14341e6dd9696d9baa544347 Mon Sep 17 00:00:00 2001 From: Kathryn Reeve Date: Tue, 31 Dec 2013 16:11:41 +0000 Subject: [PATCH 28/38] copyright update for 2014 - Zend Library --- src/AbstractConfigFactory.php | 2 +- src/Config.php | 2 +- src/Exception/ExceptionInterface.php | 2 +- src/Exception/InvalidArgumentException.php | 2 +- src/Exception/RuntimeException.php | 2 +- src/Factory.php | 2 +- src/Processor/Constant.php | 2 +- src/Processor/Filter.php | 2 +- src/Processor/ProcessorInterface.php | 2 +- src/Processor/Queue.php | 2 +- src/Processor/Token.php | 2 +- src/Processor/Translator.php | 2 +- src/Reader/Ini.php | 2 +- src/Reader/JavaProperties.php | 2 +- src/Reader/Json.php | 2 +- src/Reader/ReaderInterface.php | 2 +- src/Reader/Xml.php | 2 +- src/Reader/Yaml.php | 2 +- src/ReaderPluginManager.php | 2 +- src/Writer/AbstractWriter.php | 2 +- src/Writer/Ini.php | 2 +- src/Writer/Json.php | 2 +- src/Writer/PhpArray.php | 2 +- src/Writer/WriterInterface.php | 2 +- src/Writer/Xml.php | 2 +- src/Writer/Yaml.php | 2 +- src/WriterPluginManager.php | 2 +- 27 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/AbstractConfigFactory.php b/src/AbstractConfigFactory.php index 5345db8..20f04be 100644 --- a/src/AbstractConfigFactory.php +++ b/src/AbstractConfigFactory.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Config.php b/src/Config.php index a1e1f7d..dd85f3d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 92272f8..5824f01 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 0d15e1f..ed73392 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index d91923c..d247775 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Factory.php b/src/Factory.php index 1717469..3cadb81 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Processor/Constant.php b/src/Processor/Constant.php index 265c937..28f76b9 100644 --- a/src/Processor/Constant.php +++ b/src/Processor/Constant.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Processor/Filter.php b/src/Processor/Filter.php index 7740a26..99909f2 100644 --- a/src/Processor/Filter.php +++ b/src/Processor/Filter.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Processor/ProcessorInterface.php b/src/Processor/ProcessorInterface.php index aafe099..6aa28e9 100644 --- a/src/Processor/ProcessorInterface.php +++ b/src/Processor/ProcessorInterface.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Processor/Queue.php b/src/Processor/Queue.php index 386e251..9a7905f 100644 --- a/src/Processor/Queue.php +++ b/src/Processor/Queue.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Processor/Token.php b/src/Processor/Token.php index fb85b53..cff098b 100644 --- a/src/Processor/Token.php +++ b/src/Processor/Token.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Processor/Translator.php b/src/Processor/Translator.php index db9691d..62a2521 100644 --- a/src/Processor/Translator.php +++ b/src/Processor/Translator.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Reader/Ini.php b/src/Reader/Ini.php index 8f682d2..b44ab53 100644 --- a/src/Reader/Ini.php +++ b/src/Reader/Ini.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Reader/JavaProperties.php b/src/Reader/JavaProperties.php index 7c6e8bf..647aea2 100644 --- a/src/Reader/JavaProperties.php +++ b/src/Reader/JavaProperties.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Reader/Json.php b/src/Reader/Json.php index 3166e6b..407e2aa 100644 --- a/src/Reader/Json.php +++ b/src/Reader/Json.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Reader/ReaderInterface.php b/src/Reader/ReaderInterface.php index 9061c57..0393fe0 100644 --- a/src/Reader/ReaderInterface.php +++ b/src/Reader/ReaderInterface.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Reader/Xml.php b/src/Reader/Xml.php index ad81764..369b0b4 100644 --- a/src/Reader/Xml.php +++ b/src/Reader/Xml.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Reader/Yaml.php b/src/Reader/Yaml.php index 9ccbb96..67fe6fc 100644 --- a/src/Reader/Yaml.php +++ b/src/Reader/Yaml.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/ReaderPluginManager.php b/src/ReaderPluginManager.php index 8b0a3ee..3e74a56 100644 --- a/src/ReaderPluginManager.php +++ b/src/ReaderPluginManager.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index 5c7d698..fa94a6b 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/Ini.php b/src/Writer/Ini.php index 1fffa8f..9244f73 100644 --- a/src/Writer/Ini.php +++ b/src/Writer/Ini.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/Json.php b/src/Writer/Json.php index 4048ee8..0454849 100644 --- a/src/Writer/Json.php +++ b/src/Writer/Json.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index ee9c724..9fc457c 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/WriterInterface.php b/src/Writer/WriterInterface.php index d121b88..afbecd1 100644 --- a/src/Writer/WriterInterface.php +++ b/src/Writer/WriterInterface.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/Xml.php b/src/Writer/Xml.php index 41a5ca5..3f46c7d 100644 --- a/src/Writer/Xml.php +++ b/src/Writer/Xml.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/Writer/Yaml.php b/src/Writer/Yaml.php index be2aa07..7170841 100644 --- a/src/Writer/Yaml.php +++ b/src/Writer/Yaml.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ diff --git a/src/WriterPluginManager.php b/src/WriterPluginManager.php index d3d709e..d673345 100755 --- a/src/WriterPluginManager.php +++ b/src/WriterPluginManager.php @@ -3,7 +3,7 @@ * 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) + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ From 1e8fe968e4217e6c2c9b9bd34ac320457be040f0 Mon Sep 17 00:00:00 2001 From: Kathryn Reeve Date: Thu, 2 Jan 2014 16:26:04 +0000 Subject: [PATCH 29/38] Updated Test to have correct header --- test/AbstractFactoryTest.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/AbstractFactoryTest.php b/test/AbstractFactoryTest.php index 850dc13..a7a2957 100644 --- a/test/AbstractFactoryTest.php +++ b/test/AbstractFactoryTest.php @@ -1,10 +1,14 @@ - * @copyright Copyright 2012 - 2013, raidler dot com + * @author Chris Raidler + * @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 ZendTest\Config; use Zend\Config\AbstractConfigFactory; @@ -46,9 +50,9 @@ public function setUp() $sm = $this->serviceManager = new ServiceManager\ServiceManager( new ServiceManagerConfig(array( - 'abstract_factories' => array( - 'Zend\Config\AbstractConfigFactory', - ) + 'abstract_factories' => array( + 'Zend\Config\AbstractConfigFactory', + ) )) ); From 86d7e988ed6ef32f704a6137ca222e44735deb94 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 31 Jan 2014 22:04:06 +0100 Subject: [PATCH 30/38] Applying hotfix for zendframework/zf2#5772 - key/value replacements also apply recursively --- src/Processor/Token.php | 98 +++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/src/Processor/Token.php b/src/Processor/Token.php index cff098b..8a43829 100644 --- a/src/Processor/Token.php +++ b/src/Processor/Token.php @@ -12,6 +12,7 @@ use Traversable; use Zend\Config\Config; use Zend\Config\Exception; +use Zend\Stdlib\ArrayUtils; class Token implements ProcessorInterface { @@ -176,17 +177,24 @@ public function setToken($token, $value) /** * Build replacement map + * + * @return array */ protected function buildMap() { - if (!$this->suffix && !$this->prefix) { - $this->map = $this->tokens; - } else { - $this->map = array(); - foreach ($this->tokens as $token => $value) { - $this->map[$this->prefix . $token . $this->suffix] = $value; + if (null === $this->map) { + if (!$this->suffix && !$this->prefix) { + $this->map = $this->tokens; + } else { + $this->map = array(); + + foreach ($this->tokens as $token => $value) { + $this->map[$this->prefix . $token . $this->suffix] = $value; + } } } + + return $this->map; } /** @@ -198,28 +206,7 @@ protected function buildMap() */ public function process(Config $config) { - if ($config->isReadOnly()) { - throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); - } - - if ($this->map === null) { - $this->buildMap(); - } - - /** - * Walk through config and replace values - */ - $keys = array_keys($this->map); - $values = array_values($this->map); - foreach ($config as $key => $val) { - if ($val instanceof Config) { - $this->process($val); - } else { - $config->$key = str_replace($keys, $values, $val); - } - } - - return $config; + return $this->doProcess($config, $this->buildMap()); } /** @@ -230,12 +217,57 @@ public function process(Config $config) */ public function processValue($value) { - if ($this->map === null) { - $this->buildMap(); + return $this->doProcess($value, $this->buildMap()); + } + + /** + * Applies replacement map to the given value by modifying the value itself + * + * @param mixed $value + * @param array $replacements + * + * @return mixed + * + * @throws Exception\InvalidArgumentException if the provided value is a read-only {@see Config} + */ + private function doProcess($value, array $replacements) + { + if (is_string($value)) { + return strtr($value, $this->map); + } + + if ($value instanceof Config) { + if ($value->isReadOnly()) { + throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); + } + + foreach ($value as $key => $val) { + $value->$key = $this->doProcess($val, $replacements); + } + + return $value; + } + + if ($value instanceof Traversable || is_array($value)) { + foreach ($value as & $val) { + $val = $this->doProcess($val, $replacements); + } + + return $value; + } + + if (!is_string($value) && (is_bool($value) || is_numeric($value))) { + $stringVal = (string) $value; + $changedVal = strtr($value, $this->map); + + // replace the value only if a string replacement occurred + if ($changedVal !== $stringVal) { + return $changedVal; + } + + return $value; } - $keys = array_keys($this->map); - $values = array_values($this->map); - return str_replace($keys, $values, $value); + return strtr((string) $value, $this->map); } } From 9b8416cf94b917c62d8833fe6216658284ea0bd4 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 31 Jan 2014 22:22:28 +0100 Subject: [PATCH 31/38] Removing empty string replacements, verifying replacements in numerics --- src/Processor/Token.php | 10 +++++---- test/ProcessorTest.php | 45 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Processor/Token.php b/src/Processor/Token.php index 8a43829..806af72 100644 --- a/src/Processor/Token.php +++ b/src/Processor/Token.php @@ -192,6 +192,12 @@ protected function buildMap() $this->map[$this->prefix . $token . $this->suffix] = $value; } } + + foreach (array_keys($this->map) as $key) { + if (empty($key)) { + unset($this->map[$key]); + } + } } return $this->map; @@ -232,10 +238,6 @@ public function processValue($value) */ private function doProcess($value, array $replacements) { - if (is_string($value)) { - return strtr($value, $this->map); - } - if ($value instanceof Config) { if ($value->isReadOnly()) { throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); diff --git a/test/ProcessorTest.php b/test/ProcessorTest.php index 1ab82ea..dc6b504 100644 --- a/test/ProcessorTest.php +++ b/test/ProcessorTest.php @@ -280,7 +280,8 @@ public function testTokenChangeParamsRetainsType() 'intKey' => 123, 'floatKey' => (float) 123.456, 'doubleKey' => (double) 456.789, - ), true + ), + true ); $processor = new TokenProcessor(); @@ -294,6 +295,48 @@ public function testTokenChangeParamsRetainsType() $this->assertSame((double) 456.789, $config['doubleKey']); } + /** + * @group ZF2-5772 + */ + public function testTokenChangeParamsReplacesInNumerics() + { + $config = new Config( + array( + 'foo' => 'bar1', + 'trueBoolKey' => true, + 'falseBoolKey' => false, + 'intKey' => 123, + 'floatKey' => (float) 123.456, + 'doubleKey' => (double) 456.789, + ), + true + ); + + $processor = new TokenProcessor(array('1' => 'R', '9' => 'R')); + + $processor->process($config); + + $this->assertSame('R', $config['trueBoolKey']); + $this->assertSame('barR', $config['foo']); + $this->assertSame(false, $config['falseBoolKey']); + $this->assertSame('R23', $config['intKey']); + $this->assertSame('R23.456', $config['floatKey']); + $this->assertSame('456.78R', $config['doubleKey']); + } + + /** + * @group ZF2-5772 + */ + public function testIgnoresEmptyStringReplacement() + { + $config = new Config(array('foo' => 'bar'), true);; + $processor = new TokenProcessor(array('' => 'invalid')); + + $processor->process($config); + + $this->assertSame('bar', $config['foo']); + } + /** * @depends testTokenSurround */ From 747499dde1f7007fe45ae95bc7fc8fd545726272 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 1 Feb 2014 20:23:28 +0100 Subject: [PATCH 32/38] Removed unused import --- src/Processor/Token.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Processor/Token.php b/src/Processor/Token.php index 806af72..2af2e1b 100644 --- a/src/Processor/Token.php +++ b/src/Processor/Token.php @@ -12,7 +12,6 @@ use Traversable; use Zend\Config\Config; use Zend\Config\Exception; -use Zend\Stdlib\ArrayUtils; class Token implements ProcessorInterface { From 71af17bed3d54160dc3f98e44e5b639d20ae85bd Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Fri, 28 Feb 2014 10:28:06 -0600 Subject: [PATCH 33/38] Test Coverage on ConfigWrite for __DIR__. --- src/Writer/PhpArray.php | 6 ++++-- test/Writer/PhpArrayTest.php | 17 +++++++++++++++++ test/Writer/_files/array.php | 4 ++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/Writer/_files/array.php diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index 6aa42c9..d81a852 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -85,8 +85,10 @@ function ($error, $message = '', $file = '', $line = 0) use ($filename) { ); try { - $string = $this->toString($config); - $string = str_replace("'" . dirname($filename), "__DIR__ . '", $string); + $dirname = str_replace('\\', '\\\\', dirname($filename)); + + $string = $this->toString($config); + $string = str_replace("'" . $dirname, "__DIR__ . '", $string); file_put_contents($filename, $string, $flags); } catch (\Exception $e) { diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 385905d..cd6cf4e 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -99,6 +99,23 @@ public function testRenderWithQuotesInString() $this->assertEquals($expected, $configString); } + public function testWriteConvertsPathToDirWhenWritingBackToFile() + { + $filename = $this->getTestAssetFileName(); + file_put_contents($filename, file_get_contents(__DIR__ . '/_files/array.php')); + + $this->writer->toFile($filename, include $filename); + + // Ensure file endings are same + $expected = trim(file_get_contents(__DIR__ . '/_files/array.php')); + $expected = preg_replace("~\r\n|\n|\r~", PHP_EOL, $expected); + + $result = trim(file_get_contents($filename)); + $result = preg_replace("~\r\n|\n|\r~", PHP_EOL, $result); + + $this->assertSame($expected, $result); + } + public function testSetUseBracketArraySyntaxReturnsFluentInterface() { $this->assertSame($this->writer, $this->writer->setUseBracketArraySyntax(true)); diff --git a/test/Writer/_files/array.php b/test/Writer/_files/array.php new file mode 100644 index 0000000..df0469b --- /dev/null +++ b/test/Writer/_files/array.php @@ -0,0 +1,4 @@ + __DIR__ . '/foobar.php', +); \ No newline at end of file From 231aa067c39e61abbaed5409b3c5beac114a6354 Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Fri, 28 Feb 2014 10:30:33 -0600 Subject: [PATCH 34/38] Added comment for dirname replace. --- src/Writer/PhpArray.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Writer/PhpArray.php b/src/Writer/PhpArray.php index d81a852..818a353 100644 --- a/src/Writer/PhpArray.php +++ b/src/Writer/PhpArray.php @@ -85,6 +85,7 @@ function ($error, $message = '', $file = '', $line = 0) use ($filename) { ); try { + // for Windows, paths are escaped. $dirname = str_replace('\\', '\\\\', dirname($filename)); $string = $this->toString($config); From a1c0f25bcfba40595d1c94e317a26bcb6fa42885 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 28 Feb 2014 11:12:01 -0600 Subject: [PATCH 35/38] [zendframework/zf2#5880] CS fixes - EOF ending --- test/Writer/_files/array.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Writer/_files/array.php b/test/Writer/_files/array.php index df0469b..9d61beb 100644 --- a/test/Writer/_files/array.php +++ b/test/Writer/_files/array.php @@ -1,4 +1,4 @@ __DIR__ . '/foobar.php', -); \ No newline at end of file +); From 69baef8a8d22d4d2d7accd05a47a04070556b394 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 13 Mar 2014 12:51:17 +0100 Subject: [PATCH 36/38] Upgrading branch aliases for components: 2.2-dev -> 2.3-dev, 2.3-dev -> 2.4-dev --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 143973f..bb807e0 100644 --- a/composer.json +++ b/composer.json @@ -33,8 +33,8 @@ }, "extra": { "branch-alias": { - "dev-master": "2.2-dev", - "dev-develop": "2.3-dev" + "dev-master": "2.3-dev", + "dev-develop": "2.4-dev" } }, "autoload-dev": { From 0e4dc4f829d435c8b5fca292879c2b80581acec0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 25 Feb 2014 02:20:21 +0700 Subject: [PATCH 37/38] patch zendframework/zf2#5860 : on Config component --- src/Processor/Translator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Processor/Translator.php b/src/Processor/Translator.php index 62a2521..48c0985 100644 --- a/src/Processor/Translator.php +++ b/src/Processor/Translator.php @@ -130,7 +130,7 @@ public function process(Config $config) * Process a single value * * @param $value - * @return mixed + * @return string */ public function processValue($value) { From 0c2506ccfe21bc29edd1c9950fee0ae5a28df0d0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Mar 2014 04:34:29 +0700 Subject: [PATCH 38/38] remove double semicolon --- test/ProcessorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ProcessorTest.php b/test/ProcessorTest.php index 93f5433..b20bc23 100644 --- a/test/ProcessorTest.php +++ b/test/ProcessorTest.php @@ -329,7 +329,7 @@ public function testTokenChangeParamsReplacesInNumerics() */ public function testIgnoresEmptyStringReplacement() { - $config = new Config(array('foo' => 'bar'), true);; + $config = new Config(array('foo' => 'bar'), true); $processor = new TokenProcessor(array('' => 'invalid')); $processor->process($config);