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

Commit

Permalink
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 64 deletions.
21 changes: 21 additions & 0 deletions src/Feature/HydratorProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ModuleManager\Feature;

interface HydratorProviderInterface
{
/**
* Expected to return \Zend\ServiceManager\Config object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Config
*/
public function getHydratorConfig();
}
21 changes: 21 additions & 0 deletions src/Feature/InputFilterProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ModuleManager\Feature;

interface InputFilterProviderInterface
{
/**
* Expected to return \Zend\ServiceManager\Config object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Config
*/
public function getInputFilterConfig();
}
27 changes: 10 additions & 17 deletions src/Listener/ConfigListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ConfigListener extends AbstractListener implements
/**
* @var array
*/
protected $listeners = array();
protected $callbacks = array();

/**
* @var array
Expand Down Expand Up @@ -77,22 +77,19 @@ public function __construct(ListenerOptions $options = null)
}

/**
* Attach one or more listeners
*
* @param EventManagerInterface $events
* @return ConfigListener
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($this, 'onloadModulesPre'), 1000);
$this->callbacks[] = $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);
$this->callbacks[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'onLoadModule'));
$this->callbacks[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($this, 'onLoadModulesPost'), -1000);

return $this;
}
Expand Down Expand Up @@ -166,19 +163,15 @@ public function onLoadModulesPost(ModuleEvent $e)
}

/**
* Detach all previously attached listeners
*
* @param EventManagerInterface $events
* @return ConfigListener
* {@inheritDoc}
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $key => $listener) {
$events->detach($listener);
unset($this->listeners[$key]);
foreach ($this->callbacks as $index => $callback) {
if ($events->detach($callback)) {
unset($this->callbacks[$index]);
}
}
$this->listeners = array();
return $this;
}

/**
Expand Down
22 changes: 8 additions & 14 deletions src/Listener/LocatorRegistrationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LocatorRegistrationListener extends AbstractListener implements
/**
* @var array
*/
protected $listeners = array();
protected $callbacks = array();

/**
* loadModule
Expand Down Expand Up @@ -109,29 +109,23 @@ public function onBootstrap(Event $e)
}

/**
* Attach one or more listeners
*
* @param EventManagerInterface $events
* @return LocatorRegistrationListener
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'onLoadModule'));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($this, 'onLoadModules'), -1000);
$this->callbacks[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, array($this, 'onLoadModule'));
$this->callbacks[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULES, array($this, 'onLoadModules'), -1000);
return $this;
}

/**
* Detach all previously attached listeners
*
* @param EventManagerInterface $events
* @return void
* {@inheritDoc}
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $key => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$key]);
foreach ($this->callbacks as $index => $callback) {
if ($events->detach($callback)) {
unset($this->callbacks[$index]);
}
}
}
Expand Down
28 changes: 10 additions & 18 deletions src/Listener/ModuleLoaderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

namespace Zend\ModuleManager\Listener;

use Zend\Loader\ModuleAutoloader;
use Zend\ModuleManager\ModuleEvent;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Loader\ModuleAutoloader;
use Zend\ModuleManager\ModuleEvent;

/**
* Module loader listener
Expand All @@ -32,7 +32,7 @@ class ModuleLoaderListener extends AbstractListener implements ListenerAggregate
/**
* @var array
*/
protected $listeners = array();
protected $callbacks = array();

/**
* Constructor.
Expand All @@ -56,40 +56,32 @@ public function __construct(ListenerOptions $options = null)
}

/**
* Attach one or more listeners
*
* @param EventManagerInterface $events
* @return LocatorRegistrationListener
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(
$this->callbacks[] = $events->attach(
ModuleEvent::EVENT_LOAD_MODULES,
array($this->moduleLoader, 'register'),
9000
);

if ($this->generateCache) {
$this->listeners[] = $events->attach(
$this->callbacks[] = $events->attach(
ModuleEvent::EVENT_LOAD_MODULES_POST,
array($this, 'onLoadModulesPost')
);
}

return $this;
}

/**
* Detach all previously attached listeners
*
* @param EventManagerInterface $events
* @return void
* {@inheritDoc}
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $key => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$key]);
foreach ($this->callbacks as $index => $callback) {
if ($events->detach($callback)) {
unset($this->callbacks[$index]);
}
}
}
Expand Down
60 changes: 45 additions & 15 deletions src/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,17 @@ public function onLoadModules()
return $this;
}

foreach ($this->getModules() as $moduleName) {
$this->loadModule($moduleName);
foreach ($this->getModules() as $moduleName => $module) {
if (is_object($module)) {
if (!is_string($moduleName)) {
throw new Exception\RuntimeException(sprintf(
'Module (%s) must have a key identifier.',
get_class($module)
));
}
$module = array($moduleName => $module);
}
$this->loadModule($module);
}

$this->modulesAreLoaded = true;
Expand Down Expand Up @@ -113,14 +122,20 @@ public function loadModules()
/**
* Load a specific module by name.
*
* @param string $moduleName
* @throws Exception\RuntimeException
* @param string|array $module
* @throws Exception\RuntimeException
* @triggers loadModule.resolve
* @triggers loadModule
* @return mixed Module's Module class
* @return mixed Module's Module class
*/
public function loadModule($moduleName)
public function loadModule($module)
{
$moduleName = $module;
if (is_array($module)) {
$moduleName = key($module);
$module = current($module);
}

if (isset($this->loadedModules[$moduleName])) {
return $this->loadedModules[$moduleName];
}
Expand All @@ -130,39 +145,54 @@ public function loadModule($moduleName)

$this->loadFinished = false;

if (!is_object($module)) {
$module = $this->loadModuleByName($event);
}
$event->setModule($module);

$this->loadedModules[$moduleName] = $module;
$this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULE, $this, $event);

$this->loadFinished = true;

return $module;
}

/**
* Load a module with the name
* @param Zend\EventManager\EventInterface $event
* @return mixed module instance
* @throws Exception\RuntimeException
*/
protected function loadModuleByName($event)
{
$result = $this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, $this, $event, function ($r) {
return (is_object($r));
});

$module = $result->last();

if (!is_object($module)) {
throw new Exception\RuntimeException(sprintf(
'Module (%s) could not be initialized.',
$moduleName
$event->getModuleName()
));
}
$event->setModule($module);

$this->loadedModules[$moduleName] = $module;
$this->getEventManager()->trigger(ModuleEvent::EVENT_LOAD_MODULE, $this, $event);

$this->loadFinished = true;

return $module;
}

/**
* Get an array of the loaded modules.
*
* @param bool $loadModules If true, load modules if they're not already
* @param bool $loadModules If true, load modules if they're not already
* @return array An array of Module objects, keyed by module name
*/
public function getLoadedModules($loadModules = false)
{
if (true === $loadModules) {
$this->loadModules();
}

return $this->loadedModules;
}

Expand Down
40 changes: 40 additions & 0 deletions test/ModuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,44 @@ public function testModuleIsMarkedAsLoadedWhenLoadModuleEventIsTriggered()
$this->assertArrayHasKey('BarModule', $test->modules);
$this->assertInstanceOf('BarModule\Module', $test->modules['BarModule']);
}

public function testCanLoadSomeObjectModule()
{
require_once __DIR__ . '/TestAsset/SomeModule/Module.php';
require_once __DIR__ . '/TestAsset/SubModule/Sub/Module.php';
$configListener = $this->defaultListeners->getConfigListener();
$moduleManager = new ModuleManager(array(
'SomeModule' => new \SomeModule\Module(),
'SubModule' => new \SubModule\Sub\Module(),
), new EventManager);
$moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
$moduleManager->loadModules();
$loadedModules = $moduleManager->getLoadedModules();
$this->assertInstanceOf('SomeModule\Module', $loadedModules['SomeModule']);
$config = $configListener->getMergedConfig();
$this->assertSame($config->some, 'thing');
}

public function testCanLoadMultipleModulesObjectWithString()
{
require_once __DIR__ . '/TestAsset/SomeModule/Module.php';
$configListener = $this->defaultListeners->getConfigListener();
$moduleManager = new ModuleManager(array('SomeModule' => new \SomeModule\Module(), 'BarModule'), new EventManager);
$moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
$moduleManager->loadModules();
$loadedModules = $moduleManager->getLoadedModules();
$this->assertInstanceOf('SomeModule\Module', $loadedModules['SomeModule']);
$config = $configListener->getMergedConfig();
$this->assertSame($config->some, 'thing');
}

public function testCanNotLoadSomeObjectModuleWithoutIdentifier()
{
require_once __DIR__ . '/TestAsset/SomeModule/Module.php';
$configListener = $this->defaultListeners->getConfigListener();
$moduleManager = new ModuleManager(array(new \SomeModule\Module()), new EventManager);
$moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
$this->setExpectedException('Zend\ModuleManager\Exception\RuntimeException');
$moduleManager->loadModules();
}
}
16 changes: 16 additions & 0 deletions test/TestAsset/SubModule/Sub/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ModuleManager
*/

namespace SubModule\Sub;

class Module
{

}

0 comments on commit df27e78

Please sign in to comment.