Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'remotes/zf2/master' into test/feed-entr…
Browse files Browse the repository at this point in the history
…y-type-detection
  • Loading branch information
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 185 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

189 changes: 93 additions & 96 deletions src/Listener/ConfigListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,28 @@

/**
* Config listener
*
*
* @category Zend
* @package Zend_ModuleManager
* @subpackage Listener
*/
class ConfigListener extends AbstractListener implements
ConfigMergerInterface,
class ConfigListener extends AbstractListener implements
ConfigMergerInterface,
ListenerAggregateInterface
{
const STATIC_PATH = 'static_path';
const GLOB_PATH = 'glob_path';
const STATIC_PATH = 'static_path';
const GLOB_PATH = 'glob_path';

/**
* @var array
*/
protected $listeners = array();

/**
* @var array
*/
protected $configs = array();

/**
* @var array
*/
Expand Down Expand Up @@ -95,9 +100,16 @@ public function __invoke(ModuleEvent $e)
*/
public function attach(EventManagerInterface $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);
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($this, 'onloadModulesPre'), 1000);

if ($this->skipConfig) {
// We already have the config from cache, no need to collect or merge.
return $this;
}

$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'onLoadModule'));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($this, 'onLoadModulesPost'), -1000);

return $this;
}

Expand All @@ -107,9 +119,10 @@ public function attach(EventManagerInterface $events)
* @param ModuleEvent $e
* @return ConfigListener
*/
public function loadModulesPre(ModuleEvent $e)
public function onloadModulesPre(ModuleEvent $e)
{
$e->setConfigListener($this);

return $this;
}

Expand All @@ -119,34 +132,48 @@ public function loadModulesPre(ModuleEvent $e)
* @param ModuleEvent $e
* @return ConfigListener
*/
public function loadModule(ModuleEvent $e)
public function onLoadModule(ModuleEvent $e)
{
if (true === $this->skipConfig) {
return;
}
$module = $e->getParam('module');
if (is_callable(array($module, 'getConfig'))) {
$this->mergeModuleConfig($module);

if (!$module instanceof ConfigProviderInterface
&& !is_callable(array($module, 'getConfig'))
) {
return $this;
}

$config = $module->getConfig();
$this->addConfig($e->getModuleName(), $config);

return $this;
}

/**
* Merge all config files matched by the given glob()s
*
* This should really only be called by the module manager.
* This is only attached if config is not cached.
*
* @param ModuleEvent $e
* @return ConfigListener
*/
public function loadModulesPost(ModuleEvent $e)
public function onLoadModulesPost(ModuleEvent $e)
{
if (true === $this->skipConfig) {
return $this;
}
// Load the config files
foreach ($this->paths as $path) {
$this->mergePath($path);
$this->addConfigByPath($path['path'], $path['type']);
}

// Merge all of the collected configs
$this->mergedConfig = $this->getOptions()->getExtraConfig() ?: array();
foreach ($this->configs as $key => $config) {
$this->mergedConfig = ArrayUtils::merge($this->mergedConfig, $config);
}

// If enabled, update the config cache
if ($this->getOptions()->getConfigCacheEnabled()) {
$this->updateCache();
}

return $this;
}

Expand Down Expand Up @@ -197,28 +224,10 @@ public function setMergedConfig(array $config)
return $this;
}

/**
* Add a path of config files to merge after loading modules
*
* @param string $path
* @return ConfigListener
*/
protected function addConfigPath($path, $type)
{
if (!is_string($path)) {
throw new Exception\InvalidArgumentException(
sprintf('Parameter to %s::%s() must be a string; %s given.',
__CLASS__, __METHOD__, gettype($path))
);
}
$this->paths[] = array('type' => $type, 'path' => $path);
return $this;
}

/**
* Add an array of glob paths of config files to merge after loading modules
*
* @param mixed $globPaths
* @param array|Traversable $globPaths
* @return ConfigListener
*/
public function addConfigGlobPaths($globPaths)
Expand All @@ -242,12 +251,12 @@ public function addConfigGlobPath($globPath)
/**
* Add an array of static paths of config files to merge after loading modules
*
* @param mixed $staticPaths
* @param array|Traversable $staticPaths
* @return ConfigListener
*/
public function addConfigStaticPaths($staticPaths)
{
$this->addConfigPaths($staticPaths, self::STATIC_PATH);
$this->addConfigPaths($staticPaths, self::STATIC_PATH);
return $this;
}

Expand All @@ -259,7 +268,7 @@ public function addConfigStaticPaths($staticPaths)
*/
public function addConfigStaticPath($staticPath)
{
$this->addConfigPath($staticPath, self::STATIC_PATH);
$this->addConfigPath($staticPath, self::STATIC_PATH);
return $this;
}

Expand All @@ -271,7 +280,7 @@ public function addConfigStaticPath($staticPath)
*/
protected function addConfigPaths($paths, $type)
{
if ($paths instanceof Traversable) {
if ($paths instanceof Traversable) {
$paths = ArrayUtils::iteratorToArray($paths);
}

Expand All @@ -290,82 +299,70 @@ protected function addConfigPaths($paths, $type)
}

/**
* Merge all config files matching a glob
* Add a path of config files to load and merge after loading modules
*
* @param mixed $path
* @param string $path
* @param string $type
* @return ConfigListener
*/
protected function mergePath($path)
protected function addConfigPath($path, $type)
{
switch ($path['type']) {
case self::STATIC_PATH:
$config = ConfigFactory::fromFile($path['path']);
break;

case self::GLOB_PATH:
$config = ConfigFactory::fromFiles(Glob::glob($path['path'], Glob::GLOB_BRACE));
break;
}
$this->mergeTraversableConfig($config);
if ($this->getOptions()->getConfigCacheEnabled()) {
$this->updateCache();
if (!is_string($path)) {
throw new Exception\InvalidArgumentException(
sprintf('Parameter to %s::%s() must be a string; %s given.',
__CLASS__, __METHOD__, gettype($path))
);
}
$this->paths[] = array('type' => $type, 'path' => $path);
return $this;
}

/**
* mergeModuleConfig
*
* @param mixed $module
* @return ConfigListener
*/
protected function mergeModuleConfig($module)


protected function addConfig($key, $config)
{
if (false !== $this->skipConfig
|| (!$module instanceof ConfigProviderInterface
&& !is_callable(array($module, 'getConfig')))
) {
return $this;
if ($config instanceof Traversable) {
$config = ArrayUtils::iteratorToArray($config);
}

$config = $module->getConfig();
try {
$this->mergeTraversableConfig($config);
} catch (Exception\InvalidArgumentException $e) {
// Throw a more descriptive exception
if (!is_array($config)) {
throw new Exception\InvalidArgumentException(
sprintf('getConfig() method of %s must be an array, '
sprintf('Config being merged must be an array, '
. 'implement the \Traversable interface, or be an '
. 'instance of Zend\Config\Config. %s given.',
get_class($module), gettype($config))
. 'instance of Zend\Config\Config. %s given.', gettype($config))
);
}

if ($this->getOptions()->getConfigCacheEnabled()) {
$this->updateCache();
}
$this->configs[$key] = $config;

return $this;
}

/**
* @param $config
* @throws Exception\InvalidArgumentException
* @return void
* Given a path (glob or static), fetch the config and add it to the array
* of configs to merge.
*
* @param string $path
* @param string $type
* @return ConfigListener
*/
protected function mergeTraversableConfig($config)
protected function addConfigByPath($path, $type)
{
if ($config instanceof Traversable) {
$config = ArrayUtils::iteratorToArray($config);
}
if (!is_array($config)) {
throw new Exception\InvalidArgumentException(
sprintf('Config being merged must be an array, '
. 'implement the \Traversable interface, or be an '
. 'instance of Zend\Config\Config. %s given.', gettype($config))
);
switch ($type) {
case self::STATIC_PATH:
$this->addConfig($path, ConfigFactory::fromFile($path));
break;

case self::GLOB_PATH:
// We want to keep track of where each value came from so we don't
// use ConfigFactory::fromFiles() since it does merging internally.
foreach(Glob::glob($path, Glob::GLOB_BRACE) as $file) {
$this->addConfig($file, ConfigFactory::fromFile($file));
}
break;
}
$this->setMergedConfig(ArrayUtils::merge($this->mergedConfig, $config));

return $this;
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/Listener/DefaultListenerAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Loader\ModuleAutoloader;
use Zend\ModuleManager\ModuleEvent;
use Zend\Stdlib\CallbackHandler;

/**
* Default listener aggregate
*
*
* @category Zend
* @package Zend_ModuleManager
* @subpackage Listener
*/
class DefaultListenerAggregate extends AbstractListener implements
class DefaultListenerAggregate extends AbstractListener implements
ListenerAggregateInterface
{
/**
Expand All @@ -48,11 +49,13 @@ public function attach(EventManagerInterface $events)
$locatorRegistrationListener = new LocatorRegistrationListener($options);
$moduleAutoloader = new ModuleAutoloader($options->getModulePaths());

$this->listeners[] = $events->attach('loadModules.pre', array($moduleAutoloader, 'register'), 1000);
$this->listeners[] = $events->attach('loadModule.resolve', new ModuleResolverListener, 1000);
$this->listeners[] = $events->attach('loadModule', new AutoloaderListener($options), 2000);
$this->listeners[] = $events->attach('loadModule', new InitTrigger($options), 1000);
$this->listeners[] = $events->attach('loadModule', new OnBootstrapListener($options), 1000);
// High priority, we assume module autoloading (for FooNamespace\Module classes) should be available before anything else
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($moduleAutoloader, 'register'), 9000);
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener);
// High priority, because most other loadModule listeners will assume the module's classes are available via autoloading
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new AutoloaderListener($options), 9000);
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new InitTrigger($options));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new OnBootstrapListener($options));
$this->listeners[] = $events->attach($locatorRegistrationListener);
$this->listeners[] = $events->attach($configListener);
return $this;
Expand Down
Loading

0 comments on commit 7233001

Please sign in to comment.