Skip to content

Commit

Permalink
Fix bug where cached config would prevent the config listener from pa…
Browse files Browse the repository at this point in the history
…ssing itself to MvcEvent

Discovered by @denniswinter. This commit also cleans up the method names in the
config listener to match the event names they are meant to be attached to.
  • Loading branch information
EvanDotPro committed Jan 19, 2012
1 parent 2364469 commit 38440c0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
84 changes: 56 additions & 28 deletions library/Zend/Module/Listener/ConfigListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,50 @@ public function __construct(ListenerOptions $options = null)
}
}

/**
* __invoke proxy to loadModule for easier attaching
*
* @param ModuleEvent $e
* @return ConfigListener
*/
public function __invoke(ModuleEvent $e)
{
$this->loadModule($e);
}

/**
* Attach one or more listeners
*
* @param EventCollection $events
* @return ConfigListener
*/
public function attach(EventCollection $events)
{
$this->listeners[] = $events->attach('loadModule', array($this, 'loadModule'), 1000);
$this->listeners[] = $events->attach('loadModules.pre', array($this, 'loadModulesPre'), 9000);
$this->listeners[] = $events->attach('loadModules.post', array($this, 'loadModulesPost'), 9000);
return $this;
}

/**
* Pass self to the ModuleEvent object early so everyone has access.
*
* @param ModuleEvent $e
* @return ConfigListener
*/
public function loadModulesPre(ModuleEvent $e)
{
$e->setConfigListener($this);
return $this;
}

/**
* Merge the config for each module
*
* @param ModuleEvent $e
* @return ConfigListener
*/
public function loadModule(ModuleEvent $e)
{
if (true === $this->skipConfig) {
return;
Expand All @@ -66,18 +109,25 @@ public function __invoke(ModuleEvent $e)
if (is_callable(array($module, 'getConfig'))) {
$this->mergeModuleConfig($module);
}
return $this;
}

/**
* Attach one or more listeners
* Merge all config files matched by the given glob()s
*
* @param EventCollection $events
* @return void
* This should really only be called by the module manager.
*
* @param ModuleEvent $e
* @return ConfigListener
*/
public function attach(EventCollection $events)
public function loadModulesPost(ModuleEvent $e)
{
$this->listeners[] = $events->attach('loadModule', $this, 1000);
$this->listeners[] = $events->attach('loadModules.post', array($this, 'mergeConfigGlobPaths'), 9000);
if (true === $this->skipConfig) {
return $this;
}
foreach ($this->globPaths as $globPath) {
$this->mergeGlobPath($globPath);
}
return $this;
}

Expand Down Expand Up @@ -174,28 +224,6 @@ public function addConfigGlobPaths($globPaths)
return $this;
}

/**
* Merge all config files matched by the given glob()s
*
* This should really only be called by the module manager.
*
* @param mixed $e
* @return ConfigListener
*/
public function mergeConfigGlobPaths($e = null)
{
if (true === $this->skipConfig) {
return $this;
}
foreach ($this->globPaths as $globPath) {
$this->mergeGlobPath($globPath);
}
if ($e instanceof ModuleEvent) {
$e->setConfigListener($this);
}
return $this;
}

/**
* Merge all config files matching a glob
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Zend/Module/Listener/ConfigListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function testBadConfigFileExtensionThrowsRuntimeException()
$moduleManager = $this->moduleManager;
$moduleManager->setModules(array('SomeModule'));
$moduleManager->events()->attach('loadModule', $configListener);
$moduleManager->events()->attach('loadModules.post', array($configListener, 'mergeConfigGlobPaths'), 1000);
$moduleManager->events()->attach('loadModules.post', array($configListener, 'loadModulesPost'), 1000);
$moduleManager->loadModules();
}

Expand Down Expand Up @@ -244,7 +244,7 @@ public function testConfigListenerFunctionsAsAggregateListener()
$this->assertEquals(1, count($moduleManager->events()->getEvents()));

$configListener->attach($moduleManager->events());
$this->assertEquals(3, count($moduleManager->events()->getEvents()));
$this->assertEquals(4, count($moduleManager->events()->getEvents()));

$configListener->detach($moduleManager->events());
$this->assertEquals(1, count($moduleManager->events()->getEvents()));
Expand All @@ -263,7 +263,7 @@ public function testPhpConfigFileReturningInvalidConfigRaisesException()
$moduleManager->setModules(array('SomeModule'));

$moduleManager->events()->attach('loadModule', $configListener);
$moduleManager->events()->attach('loadModules.post', array($configListener, 'mergeConfigGlobPaths'), 1000);
$moduleManager->events()->attach('loadModules.post', array($configListener, 'loadModulesPost'), 1000);

$moduleManager->loadModules();
}
Expand Down

0 comments on commit 38440c0

Please sign in to comment.