Skip to content

Commit

Permalink
Merge branch 'feature/module-config-listener-factory' of https://gith…
Browse files Browse the repository at this point in the history
…ub.com/EvanDotPro/zf2 into feature/module-config-use_factory
  • Loading branch information
weierophinney committed Mar 1, 2012
2 parents bbf018d + a7df0ec commit 31d8379
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 92 deletions.
54 changes: 6 additions & 48 deletions library/Zend/Module/Listener/ConfigListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
49 changes: 5 additions & 44 deletions tests/Zend/Module/Listener/ConfigListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'));
Expand All @@ -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()
Expand All @@ -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'));
Expand All @@ -203,22 +185,21 @@ 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()
{
$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;
Expand All @@ -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()
Expand All @@ -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();
}
}

0 comments on commit 31d8379

Please sign in to comment.