diff --git a/library/Zend/Module/Listener/ConfigListener.php b/library/Zend/Module/Listener/ConfigListener.php index c12dd9ff2fb..dc554e6036e 100644 --- a/library/Zend/Module/Listener/ConfigListener.php +++ b/library/Zend/Module/Listener/ConfigListener.php @@ -4,11 +4,7 @@ use ArrayAccess, Traversable, - Zend\Config\Config, - Zend\Config\Xml as XmlConfig, - Zend\Config\Ini as IniConfig, - Zend\Config\Yaml as YamlConfig, - Zend\Config\Json as JsonConfig, + Zend\Config\Factory as ConfigFactory, Zend\Module\ModuleEvent, Zend\Stdlib\IteratorToArray, Zend\EventManager\EventCollection, @@ -232,49 +228,11 @@ public function addConfigGlobPaths($globPaths) */ protected function mergeGlobPath($globPath) { - foreach (glob($globPath, GLOB_BRACE) as $path) { - $pathInfo = pathinfo($path); - switch (strtolower($pathInfo['extension'])) { - case 'php': - case 'inc': - $config = include $path; - if (!is_array($config) && !$config instanceof ArrayAccess) { - throw new Exception\RuntimeException(sprintf( - 'Invalid configuration type returned by file at "%s"; received "%s"', - $path, - (is_object($config) ? get_class($config) : gettype($config)) - )); - } - break; - - case 'xml': - $config = new XmlConfig($path); - break; - - case 'json': - $config = new JsonConfig($path); - break; - - case 'ini': - $config = new IniConfig($path); - break; - - case 'yaml': - case 'yml': - $config = new YamlConfig($path); - break; - - default: - throw new Exception\RuntimeException(sprintf( - 'Unable to detect config file type by extension: %s', - $path - )); - break; - } - $this->mergeTraversableConfig($config); - if ($this->getOptions()->getConfigCacheEnabled()) { - $this->updateCache(); - } + // @TODO Use GlobIterator + $config = ConfigFactory::fromFiles(glob($globPath, GLOB_BRACE)); + $this->mergeTraversableConfig($config); + if ($this->getOptions()->getConfigCacheEnabled()) { + $this->updateCache(); } return $this; } diff --git a/tests/Zend/Module/Listener/ConfigListenerTest.php b/tests/Zend/Module/Listener/ConfigListenerTest.php index fe029fd2bb2..14220a7b946 100644 --- a/tests/Zend/Module/Listener/ConfigListenerTest.php +++ b/tests/Zend/Module/Listener/ConfigListenerTest.php @@ -117,20 +117,6 @@ public function testBadConfigValueThrowsInvalidArgumentException() $moduleManager->loadModules(); } - public function testBadConfigFileExtensionThrowsRuntimeException() - { - $this->setExpectedException('RuntimeException'); - - $configListener = new ConfigListener; - $configListener->addConfigGlobPath(__DIR__ . '/_files/bad/*.badext'); - - $moduleManager = $this->moduleManager; - $moduleManager->setModules(array('SomeModule')); - $moduleManager->events()->attach('loadModule', $configListener); - $moduleManager->events()->attach('loadModules.post', array($configListener, 'loadModulesPost'), 1000); - $moduleManager->loadModules(); - } - public function testBadGlobPathTrowsInvalidArgumentException() { $this->setExpectedException('InvalidArgumentException'); @@ -148,7 +134,7 @@ public function testBadGlobPathArrayTrowsInvalidArgumentException() public function testCanMergeConfigFromGlob() { $configListener = new ConfigListener; - $configListener->addConfigGlobPath(__DIR__ . '/_files/good/*.{ini,json,php,xml,yml}'); + $configListener->addConfigGlobPath(__DIR__ . '/_files/good/*.{ini,php,xml}'); $moduleManager = $this->moduleManager; $moduleManager->setModules(array('SomeModule')); @@ -163,16 +149,12 @@ public function testCanMergeConfigFromGlob() $this->assertSame(spl_object_hash($configObjectCheck), spl_object_hash($configObject)); $this->assertSame('loaded', $configObject->ini); $this->assertSame('loaded', $configObject->php); - $this->assertSame('loaded', $configObject->json); $this->assertSame('loaded', $configObject->xml); - $this->assertSame('loaded', $configObject->yml); // Test as array $config = $configListener->getMergedConfig(false); $this->assertSame('loaded', $config['ini']); - $this->assertSame('loaded', $config['json']); $this->assertSame('loaded', $config['php']); $this->assertSame('loaded', $config['xml']); - $this->assertSame('loaded', $config['yml']); } public function testCanCacheMergedConfigFromGlob() @@ -182,7 +164,7 @@ public function testCanCacheMergedConfigFromGlob() 'config_cache_enabled' => true, )); $configListener = new ConfigListener($options); - $configListener->addConfigGlobPath(__DIR__ . '/_files/good/*.{ini,json,php,xml,yml}'); + $configListener->addConfigGlobPath(__DIR__ . '/_files/good/*.{ini,php,xml}'); $moduleManager = $this->moduleManager; $moduleManager->setModules(array('SomeModule')); @@ -203,11 +185,12 @@ public function testCanCacheMergedConfigFromGlob() // Check if values from glob object and cache object are the same $configObjectFromCache = $configListener->getMergedConfig(); + $this->assertNotNull($configObjectFromGlob->ini); $this->assertSame($configObjectFromGlob->ini, $configObjectFromCache->ini); + $this->assertNotNull($configObjectFromGlob->php); $this->assertSame($configObjectFromGlob->php, $configObjectFromCache->php); - $this->assertSame($configObjectFromGlob->json, $configObjectFromCache->json); + $this->assertNotNull($configObjectFromGlob->xml); $this->assertSame($configObjectFromGlob->xml, $configObjectFromCache->xml); - $this->assertSame($configObjectFromGlob->yml, $configObjectFromCache->yml); } public function testCanMergeConfigFromArrayOfGlobs() @@ -215,10 +198,8 @@ public function testCanMergeConfigFromArrayOfGlobs() $configListener = new ConfigListener; $configListener->addConfigGlobPaths(new ArrayObject(array( __DIR__ . '/_files/good/*.ini', - __DIR__ . '/_files/good/*.json', __DIR__ . '/_files/good/*.php', __DIR__ . '/_files/good/*.xml', - __DIR__ . '/_files/good/*.yml', ))); $moduleManager = $this->moduleManager; @@ -231,9 +212,7 @@ public function testCanMergeConfigFromArrayOfGlobs() $configObject = $configListener->getMergedConfig(); $this->assertSame('loaded', $configObject->ini); $this->assertSame('loaded', $configObject->php); - $this->assertSame('loaded', $configObject->json); $this->assertSame('loaded', $configObject->xml); - $this->assertSame('loaded', $configObject->yml); } public function testConfigListenerFunctionsAsAggregateListener() @@ -249,22 +228,4 @@ public function testConfigListenerFunctionsAsAggregateListener() $configListener->detach($moduleManager->events()); $this->assertEquals(1, count($moduleManager->events()->getEvents())); } - - public function testPhpConfigFileReturningInvalidConfigRaisesException() - { - $this->setExpectedException('Zend\Module\Listener\Exception\RuntimeException', 'Invalid configuration'); - - $configListener = new ConfigListener; - $configListener->addConfigGlobPaths(new ArrayObject(array( - __DIR__ . '/_files/bad/*.php', - ))); - - $moduleManager = $this->moduleManager; - $moduleManager->setModules(array('SomeModule')); - - $moduleManager->events()->attach('loadModule', $configListener); - $moduleManager->events()->attach('loadModules.post', array($configListener, 'loadModulesPost'), 1000); - - $moduleManager->loadModules(); - } }