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

Make Zend\Loader soft dependency #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Listener/DefaultListenerAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ public function attach(EventManagerInterface $events)
$locatorRegistrationListener = new LocatorRegistrationListener($options);

// High priority, we assume module autoloading (for FooNamespace\Module classes) should be available before anything else
$this->listeners[] = $events->attach(new ModuleLoaderListener($options));
if ($options->getUseZendLoader()) {
// Register listener only if Zend\Loader is used, otherwise depend
// on other means of module autoloading. Eg Composer
$this->listeners[] = $events->attach(new ModuleLoaderListener($options));
}
$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->getUseZendLoader()) {
// Only listen for AutoloaderProvider feature if Zend\Loader is used.
// 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->getCheckDependencies()) {
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new ModuleDependencyCheckerListener, 8000);
Expand Down
26 changes: 26 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,27 @@ public function setCheckDependencies($checkDependencies)
return $this;
}

public function getUseZendLoader()
{
return $this->useZendLoader;
}

/**
* Set if the module manager should use Zend\Loader
*
* Setting this option to false will disable ModuleAutoloader, requiring
* other means of autoloading to be used. Eg Composer.
* 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
43 changes: 43 additions & 0 deletions test/Listener/DefaultListenerAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,47 @@ public function testDefaultListenerAggregateCanDetachItself()
$listenerAggregate->detach($moduleManager->getEventManager());
$this->assertEquals(1, count($moduleManager->getEventManager()->getEvents()));
}

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

$events = $moduleManager->getEventManager()->getEvents();
$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);
$listeners = $moduleManager->getEventManager()->getListeners($event);
$this->assertSame(count($expectedListeners), count($listeners));
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if (is_array($callback)) {
$callback = $callback[0];
}
$listenerClass = get_class($callback);
$this->assertContains($listenerClass, $expectedListeners);
}
}
}
}