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

Commit

Permalink
Merge branch 'feature/4' into develop
Browse files Browse the repository at this point in the history
Close #4
  • Loading branch information
weierophinney committed Jul 11, 2017
2 parents 48e7243 + 0161951 commit 86473f4
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#44](https://github.com/zendframework/zend-modulemanager/pull/44) adds a new
`ListenerOptions` option, `use_zend_loader`. The option defaults to `true`,
which keeps the current behavior of registering the `ModuleAutoloader` and
`AutoloaderProvider`. If you disable it, these features will no longer be
loaded, allowing `ModuleManager` to be used without zend-loader.

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"suggest": {
"zendframework/zend-console": "Zend\\Console component",
"zendframework/zend-loader": "Zend\\Loader component",
"zendframework/zend-loader": "Zend\\Loader component if you are not using Composer autoloading for your modules",
"zendframework/zend-mvc": "Zend\\Mvc component",
"zendframework/zend-servicemanager": "Zend\\ServiceManager component"
},
Expand Down
27 changes: 27 additions & 0 deletions doc/book/module-autoloader.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ $moduleManager->loadModules();
> than one registered module path, the module autoloader will return the first
> one it finds.
> ### Disabling the ModuleAutoloader
>
> - Since 2.8.0
>
> If you are using Composer to autoload, you may not need to use the
> `ModuleAutolaoder`. As such, you can disable it by passing the following
> option to the `ListenerOptions` class:
>
> ```php
> 'use_zend_loader' => false,
> ```
>
> If your project was begun from the [skeleton application](https://github.com/zendframework/ZendSkeletonApplication),
> place the above within the `module_listener_options` configuration of your
> `config/application.config.php` file:
>
> ```php
> return [
> /* ... */
> 'module_listener_options' => [
> 'use_zend_loader' => false,
> /* ... */
> ],
> /* ... */
> ];
> ```
## Non-Standard / Explicit Module Paths

Sometimes you may want to specify exactly where a module is instead of having
Expand Down
20 changes: 13 additions & 7 deletions src/Listener/DefaultListenerAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@ public function attach(EventManagerInterface $events, $priority = 1)
$options = $this->getOptions();
$configListener = $this->getConfigListener();
$locatorRegistrationListener = new LocatorRegistrationListener($options);
$moduleLoaderListener = new ModuleLoaderListener($options);

// High priority, we assume module autoloading (for FooNamespace\Module
// classes) should be available before anything else
$moduleLoaderListener->attach($events);
$this->listeners[] = $moduleLoaderListener;
// classes) should be available before anything else.
// Register it only if use_zend_loader config is true, however.
if ($options->useZendLoader()) {
$moduleLoaderListener = new ModuleLoaderListener($options);
$moduleLoaderListener->attach($events);
$this->listeners[] = $moduleLoaderListener;
}
$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);
if ($options->useZendLoader()) {
// High priority, because most other loadModule listeners will assume
// the module's classes are available via autoloading
// Register it only if use_zend_loader config is true, however.
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new AutoloaderListener($options), 9000);
}

if ($options->getCheckDependencies()) {
$this->listeners[] = $events->attach(
Expand Down
32 changes: 32 additions & 0 deletions src/Listener/ListenerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class ListenerOptions extends AbstractOptions
*/
protected $moduleMapCacheKey;

/**
* @var bool
*/
protected $useZendLoader = true;

/**
* Get an array of paths where modules reside
*
Expand Down Expand Up @@ -381,6 +386,33 @@ public function setCheckDependencies($checkDependencies)
return $this;
}

/**
* Whether or not to use zend-loader to autoload modules.
*
* @return bool
*/
public function useZendLoader()
{
return $this->useZendLoader;
}

/**
* Set a flag indicating if the module manager should use zend-loader
*
* Setting this option to false will disable ModuleAutoloader, requiring
* other means of autoloading to be used (e.g., Composer).
*
* If disabled, the AutoloaderProvider feature will be disabled as well
*
* @param bool $flag
* @return ListenerOptions
*/
public function setUseZendLoader($flag)
{
$this->useZendLoader = (bool) $flag;
return $this;
}

/**
* Normalize a path for insertion in the stack
*
Expand Down
45 changes: 44 additions & 1 deletion test/Listener/DefaultListenerAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testDefaultListenerAggregateCanAttachItself()
];
foreach ($expectedEvents as $event => $expectedListeners) {
$this->assertContains($event, $events);
$count = 0;
$count = 0;
foreach ($this->getListenersForEvent($event, $moduleManager->getEventManager()) as $listener) {
if (is_array($listener)) {
$listener = $listener[0];
Expand Down Expand Up @@ -94,4 +94,47 @@ public function testDefaultListenerAggregateCanDetachItself()
$listenerAggregate->detach($events);
$this->assertEquals(1, count($this->getEventsFromEventManager($events)));
}

public function testDefaultListenerAggregateSkipsAutoloadingListenersIfZendLoaderIsNotUsed()
{
$moduleManager = new ModuleManager(['ListenerTestModule']);
$eventManager = $moduleManager->getEventManager();
$listenerAggregate = new DefaultListenerAggregate(new ListenerOptions([
'use_zend_loader' => false,
]));
$listenerAggregate->attach($eventManager);

$events = $this->getEventsFromEventManager($eventManager);
$expectedEvents = [
'loadModules' => [
'config-pre' => 'Zend\ModuleManager\Listener\ConfigListener',
'config-post' => 'Zend\ModuleManager\Listener\ConfigListener',
'Zend\ModuleManager\Listener\LocatorRegistrationListener',
'Zend\ModuleManager\ModuleManager',
],
'loadModule.resolve' => [
'Zend\ModuleManager\Listener\ModuleResolverListener',
],
'loadModule' => [
'Zend\ModuleManager\Listener\ModuleDependencyCheckerListener',
'Zend\ModuleManager\Listener\InitTrigger',
'Zend\ModuleManager\Listener\OnBootstrapListener',
'Zend\ModuleManager\Listener\ConfigListener',
'Zend\ModuleManager\Listener\LocatorRegistrationListener',
],
];
foreach ($expectedEvents as $event => $expectedListeners) {
$this->assertContains($event, $events);
$count = 0;
foreach ($this->getListenersForEvent($event, $eventManager) as $listener) {
if (is_array($listener)) {
$listener = $listener[0];
}
$listenerClass = get_class($listener);
$this->assertContains($listenerClass, $expectedListeners);
$count += 1;
}
$this->assertSame(count($expectedListeners), $count);
}
}
}

0 comments on commit 86473f4

Please sign in to comment.