From 9148d332fc0fc28530656d392f82644fdcb46c80 Mon Sep 17 00:00:00 2001 From: Aleksey Khudyakov Date: Sun, 6 Sep 2015 15:32:06 +1000 Subject: [PATCH] Add module manager option not to use Zend\Loader New `use_zend_loader` module manager option controls if Zend\Loader should be used, it is enabled by default. Disabling it removes dependency on zendframework/zend-loader. This option is useful if alternative autoloading is used, eg Composer. Setting it to false will also disable AutoloaderProvider feature. --- src/Listener/DefaultListenerAggregate.php | 13 ++++-- src/Listener/ListenerOptions.php | 26 +++++++++++ .../Listener/DefaultListenerAggregateTest.php | 43 +++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/Listener/DefaultListenerAggregate.php b/src/Listener/DefaultListenerAggregate.php index 28cb032..7808d66 100644 --- a/src/Listener/DefaultListenerAggregate.php +++ b/src/Listener/DefaultListenerAggregate.php @@ -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); diff --git a/src/Listener/ListenerOptions.php b/src/Listener/ListenerOptions.php index 03c6ccd..19a5c29 100644 --- a/src/Listener/ListenerOptions.php +++ b/src/Listener/ListenerOptions.php @@ -67,6 +67,11 @@ class ListenerOptions extends AbstractOptions */ protected $moduleMapCacheKey; + /** + * @var bool + */ + protected $useZendLoader = true; + /** * Get an array of paths where modules reside * @@ -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 * diff --git a/test/Listener/DefaultListenerAggregateTest.php b/test/Listener/DefaultListenerAggregateTest.php index 379fb36..d9ba4bc 100644 --- a/test/Listener/DefaultListenerAggregateTest.php +++ b/test/Listener/DefaultListenerAggregateTest.php @@ -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); + } + } + } }