From f308d8b6772cee3f35f5bce1d09c7a835337add6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 23 Jul 2020 17:10:37 +0700 Subject: [PATCH] Fixes #4 : Drop laminas loader, module loader and autoloader provider features Signed-off-by: Abdul Malik Ikhsan --- composer.json | 8 +- docs/book/intro.md | 38 +---- docs/book/migration/to-v3-0.md | 87 ++++++++++ docs/book/module-autoloader.md | 161 ------------------ docs/book/module-class.md | 28 +-- docs/book/module-manager.md | 8 - mkdocs.yml | 3 +- src/Feature/AutoloaderProviderInterface.php | 22 --- src/Listener/AutoloaderListener.php | 37 ---- src/Listener/DefaultListenerAggregate.php | 19 --- src/Listener/ListenerOptions.php | 62 ------- src/Listener/ModuleLoaderListener.php | 127 -------------- test/Listener/AbstractListenerTestCase.php | 14 -- test/Listener/AutoloaderListenerTest.php | 57 ------- .../Listener/DefaultListenerAggregateTest.php | 144 ---------------- test/Listener/ListenerOptionsTest.php | 161 ------------------ test/Listener/ModuleLoaderListenerTest.php | 120 ------------- test/ModuleManagerTest.php | 7 +- test/ResetAutoloadFunctionsTrait.php | 76 --------- test/TestAsset/ListenerTestModule/Module.php | 15 -- test/TestAsset/NotAutoloaderModule/Module.php | 26 --- .../NotAutoloaderModule/src/Foo/Bar.php | 15 -- 22 files changed, 110 insertions(+), 1125 deletions(-) create mode 100644 docs/book/migration/to-v3-0.md delete mode 100644 docs/book/module-autoloader.md delete mode 100644 src/Feature/AutoloaderProviderInterface.php delete mode 100644 src/Listener/AutoloaderListener.php delete mode 100644 src/Listener/ModuleLoaderListener.php delete mode 100644 test/Listener/AutoloaderListenerTest.php delete mode 100644 test/Listener/DefaultListenerAggregateTest.php delete mode 100644 test/Listener/ListenerOptionsTest.php delete mode 100644 test/Listener/ModuleLoaderListenerTest.php delete mode 100644 test/ResetAutoloadFunctionsTrait.php delete mode 100644 test/TestAsset/NotAutoloaderModule/Module.php delete mode 100644 test/TestAsset/NotAutoloaderModule/src/Foo/Bar.php diff --git a/composer.json b/composer.json index 5edd1bd1..1330a761 100644 --- a/composer.json +++ b/composer.json @@ -33,14 +33,12 @@ "laminas/laminas-coding-standard": "~1.0.0", "laminas/laminas-console": "^2.8", "laminas/laminas-di": "^2.6.1", - "laminas/laminas-loader": "^2.6.1", "laminas/laminas-mvc": "^3.1.1", "laminas/laminas-servicemanager": "^3.4.1", "phpunit/phpunit": "^9.3.7" }, "suggest": { "laminas/laminas-console": "Laminas\\Console component", - "laminas/laminas-loader": "Laminas\\Loader component if you are not using Composer autoloading for your modules", "laminas/laminas-mvc": "Laminas\\Mvc component", "laminas/laminas-servicemanager": "Laminas\\ServiceManager component" }, @@ -51,8 +49,10 @@ }, "autoload-dev": { "files": [ - "test/autoload.php", - "test/TestAsset/ModuleAsClass.php" + "test/autoload.php" + ], + "classmap": [ + "test/TestAsset/" ], "psr-4": { "ListenerTestModule\\": "test/TestAsset/ListenerTestModule/", diff --git a/docs/book/intro.md b/docs/book/intro.md index 6483a355..8eccecbc 100644 --- a/docs/book/intro.md +++ b/docs/book/intro.md @@ -15,10 +15,6 @@ CSS, and JavaScript. The possibilities are endless. The module system is made up of the following: -- [The Module Autoloader](https://docs.laminas.dev/laminas-loader/module-autoloader/) - - `Laminas\Loader\ModuleAutoloader` is a specialized autoloader that is responsible - for the locating and loading of modules' `Module` classes from a variety of - sources. - [The Module Manager](module-manager.md) - `Laminas\ModuleManager\ModuleManager` takes an array of module names and fires a sequence of events for each one, allowing the behavior of the module system to be defined entirely by the @@ -38,10 +34,6 @@ The recommended structure for an MVC-oriented Laminas module is as follows: ```text module_root/ - Module.php - autoload_classmap.php - autoload_function.php - autoload_register.php config/ module.config.php public/ @@ -49,32 +41,20 @@ module_root/ css/ js/ src/ - / - + Module.php + test/ - phpunit.xml - bootstrap.php - / - + view/ / / <.phtml files> + phpunit.xml.dist + composer.json ``` -## The autoload\_\*.php Files +## Autoloading -The three `autoload_*.php` files are not required, but recommended. They provide the following: - -- `autoload_classmap.php` should return an array classmap of class name/filename - pairs (with the filenames resolved via the `__DIR__` magic constant). -- `autoload_function.php` should return a PHP callback that can be passed to - `spl_autoload_register()`. Typically, this callback should utilize the map - returned by `autoload_classmap.php`. -- `autoload_register.php` should register a PHP callback (typically that - returned by `autoload_function.php` with `spl_autoload_register()`. - -The purpose of these three files is to provide reasonable default mechanisms for -autoloading the classes contained in the module, thus providing a trivial way to -consume the module without requiring laminas-modulemanager` (e.g., for use outside -a Laminas application). +Since version 3, laminas-modulemanager does not provide own autoloading mechanisms +and instead relies on [Composer dependency manager](https://getcomposer.org/) +to provide autoloading. diff --git a/docs/book/migration/to-v3-0.md b/docs/book/migration/to-v3-0.md new file mode 100644 index 00000000..da744341 --- /dev/null +++ b/docs/book/migration/to-v3-0.md @@ -0,0 +1,87 @@ +# Upgrading to 3.0 + +## Module autoloading + +laminas-modulemanager originates from before the Composer was created, where each +framework had to provide its own autoloading implementation. +Since then Composer became the de-facto standard in managing dependencies and +autoloading for the php projects. +In light of that, laminas-servicemanager removes ModuleLoader and autoload +providers support in version 3.0 in favor of +[Composer dependency manager](https://getcomposer.org/). + +### Application local modules + +Autoloading rules for application local modules should now be defined in +application's composer.json + +Before: + +```php +namespace Application; + +class Module +{ + public function getAutoloaderConfig() + { + return [ + 'Laminas\Loader\StandardAutoloader' => [ + 'namespaces' => [ + __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, + ], + ], + ]; + } +} +``` + +and after: + +```json +{ + "name": "laminas/laminas-mvc-skeleton", + "description": "Laminas MVC Skeleton Application", + "type": "project", + ... + "autoload": { + "psr-4": { + "Application\\": "module/Application/src/" + } + }, + "autoload-dev": { + "psr-4": { + "ApplicationTest\\": "module/Application/test/" + } + } +} +``` + +[laminas-composer-autoloading](https://github.com/laminas/laminas-composer-autoloading) +provides a handy tool to easily add and remove autoloading rules for local modules to +application's composer.json + +After autoloading rules were updated, composer will need to update autoloader: + +```console +$ composer dump-autoload +``` + +### Composer installed modules + +For composer installed modules, autoloading rules will be automatically picked +by composer from the module's composer.json and no extra effort is needed: +```json +{ + "name": "acme/my-module", + "description": "Module for use with laminas-mvc applications.", + "type": "library", + "require": { + "php": "^7.1" + }, + "autoload": { + "psr-4": { + "Acme\\MyModule\\": "src/" + } + } +} +``` \ No newline at end of file diff --git a/docs/book/module-autoloader.md b/docs/book/module-autoloader.md deleted file mode 100644 index 5e35ed15..00000000 --- a/docs/book/module-autoloader.md +++ /dev/null @@ -1,161 +0,0 @@ -# The Module Autoloader - -laminas-modulemanager ships with the default module autoloader -`Laminas\Loader\ModuleAutoloader`. It is a specialized autoloader responsible for -locating and on-demand loading of, the `Module` classes from a variety of -sources. - -## Module Autoloader Usage - -By default, the provided `Laminas\ModuleManager\Listener\DefaultListenerAggregate` -sets up the `ModuleAutoloader`; as a developer, you need only provide an array -of module paths, either absolute or relative to the application's root, for the -`ModuleAutoloader` to check when loading modules. The `DefaultListenerAggregate` -will take care of instantiating and registering the `ModuleAutoloader` for you. - -> ### Must be in application root -> -> In order for paths relative to your application directory to work, you must -> have the directive `chdir(dirname(__DIR__));` in your `public/index.php` file. - -### Registering module paths with the `DefaultListenerAggregate` - -The following example will search for modules in three different `module_paths`. -Two are local directories of this application and the third is a system-wide -shared directory. - -```php -// public/index.php -use Laminas\ModuleManager\Listener; -use Laminas\ModuleManager\ModuleManager; - -chdir(dirname(__DIR__)); - -// Instantiate and configure the default listener aggregate -$listenerOptions = new Listener\ListenerOptions([ - 'module_paths' => [ - './module', - './vendor', - '/usr/share/laminasmodules', - ] -]); -$defaultListeners = new Listener\DefaultListenerAggregate($listenerOptions); - -// Instantiate the module manager -$moduleManager = new ModuleManager([ - 'Application', - 'FooModule', - 'BarModule', -]); - -// Attach the default listener aggregate and load the modules -$moduleManager->getEventManager()->attachAggregate($defaultListeners); -$moduleManager->loadModules(); -``` - -> ### Module paths are FIFO -> -> Module paths behave very similar to PHP's `include_path` and are searched in -> the order they are defined. If you have modules with the same name in more -> 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 -> `ModuleAutoloader`. As such, you can disable it by passing the following -> option to the `ListenerOptions` class: -> -> ```php -> 'use_laminas_loader' => false, -> ``` -> -> If your project was begun from the [laminas-mvc-skeleton](https://github.com/laminas/laminas-mvc-skeleton), -> place the above within the `module_listener_options` configuration of your -> `config/application.config.php` file: -> -> ```php -> return [ -> /* ... */ -> 'module_listener_options' => [ -> 'use_laminas_loader' => false, -> /* ... */ -> ], -> /* ... */ -> ]; -> ``` - -## Non-Standard / Explicit Module Paths - -Sometimes you may want to specify exactly where a module is instead of having -`Laminas\Loader\ModuleAutoloader` try to find it in the registered paths. - -### Registering a Non-Standard / Explicit Module Path - -In this example, the autoloader will first check for `MyModule\Module` in -`/path/to/mymoduledir-v1.2/Module.php`. If it's not found, then it will fall -back to searching any other registered module paths. - -```php -// ./public/index.php -use Laminas\Loader\ModuleAutoloader; -use Laminas\ModuleManager\Listener; -use Laminas\ModuleManager\ModuleManager; - -chdir(dirname(__DIR__)); - -// Instantiate and configure the default listener aggregate -$listenerOptions = new Listener\ListenerOptions([ - 'module_paths' => [ - './module', - './vendor', - '/usr/share/laminasmodules', - 'MyModule' => '/path/to/mymoduledir-v1.2', - ] -]); -$defaultListeners = new Listener\DefaultListenerAggregate($listenerOptions); - -/** - * Without DefaultListenerAggregate: - * - * $moduleAutoloader = new ModuleAutoloader([ - * './module', - * './vendor', - * '/usr/share/laminasmodules', - * 'MyModule' => '/path/to/mymoduledir-v1.2', - * ]); - * $moduleAutoloader->register(); - * - */ - -// Instantiate the module manager -$moduleManager = new ModuleManager([ - 'MyModule', - 'FooModule', - 'BarModule', -]); - -// Attach the default listener aggregate and load the modules -$moduleManager->getEventManager()->attachAggregate($defaultListeners); -$moduleManager->loadModules(); -``` - -This same method works if you provide the path to a phar archive. - -## Packaging Modules with Phar - -If you prefer, you may easily package your module as a -[phar archive](http://php.net/phar). The module autoloader is able to autoload -modules in the following archive formats: .phar, .phar.gz, .phar.bz2, .phar.tar, -.phar.tar.gz, .phar.tar.bz2, .phar.zip, .tar, .tar.gz, .tar.bz2, and .zip. - -Package your module by performing a tar the module directory. You can then -replace the `MyModule/` directory with `MyModule.tar`, and it should still be -autoloaded without any additional changes! - -> ### Avoid compression -> -> If possible, avoid using any type of compression (bz2, gz, zip) on your phar -> archives, as it introduces unnecessary CPU overhead to each request. diff --git a/docs/book/module-class.md b/docs/book/module-class.md index 6c63ce7e..9e115ccf 100644 --- a/docs/book/module-class.md +++ b/docs/book/module-class.md @@ -9,9 +9,7 @@ of `{moduleName}\Module` for each enabled module. As an example, provided the module name "MyModule", `Laminas\ModuleManager\Listener\ModuleResolverListener` will expect the class -`MyModule\Module` to be available. It relies on a registered autoloader -(typically `Laminas\Loader\ModuleAutoloader`) to find and include the -`MyModule\Module` class if it isn't already available. +`MyModule\Module` to be available. > ### Module classes > @@ -26,7 +24,8 @@ something like this: ```text MyModule/ - Module.php + src/ + Module.php ``` Within `Module.php`, you define your `MyModule\Module` class: @@ -46,7 +45,7 @@ module system! This `Module` class serves as the single entry point for `ModuleManager` listeners to interact with a module. From within this class, modules can override or provide additional application configuration, perform initialization -tasks such as registering autoloader(s), services and event listeners, declaring +tasks such as registering services and event listeners, declaring dependencies, and much more. ## A Typical Module Class @@ -58,20 +57,6 @@ namespace MyModule; class Module { - public function getAutoloaderConfig() - { - return [ - 'Laminas\Loader\ClassMapAutoloader' => [ - __DIR__ . '/autoload_classmap.php', - ], - 'Laminas\Loader\StandardAutoloader' => [ - 'namespaces' => [ - __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, - ], - ], - ]; - } - public function getConfig() { return include __DIR__ . '/config/module.config.php'; @@ -79,8 +64,9 @@ class Module } ``` -For a list of the provided module manager listeners and the interfaces and methods that `Module` -classes may implement in order to interact with the module manager and application, see the +For a list of the provided module manager listeners and the interfaces and +methods that `Module` classes may implement in order to interact with the +module manager and application, see the [module manager listeners](module-manager.md#module-manager-listeners) and the [module mananger events](module-manager.md#module-manager-events) documentation. diff --git a/docs/book/module-manager.md b/docs/book/module-manager.md index d13c76cc..e93b44fc 100644 --- a/docs/book/module-manager.md +++ b/docs/book/module-manager.md @@ -69,14 +69,6 @@ you will need to attach to use the module manager, as it will take care of properly attaching the requisite listeners (those listed below) for the module system to function properly. -### AutoloaderListener - -This listener checks each module to see if it has implemented -`Laminas\ModuleManager\Feature\AutoloaderProviderInterface` or defined the -`getAutoloaderConfig()` method. If so, it calls the `getAutoloaderConfig()` -method on the module class and passes the returned array to -`Laminas\Loader\AutoloaderFactory`. - ### ModuleDependencyCheckerListener This listener checks each module to verify if all the modules it depends on were diff --git a/mkdocs.yml b/mkdocs.yml index 733601b5..6b095530 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -6,8 +6,9 @@ nav: - Reference: - "The Module Manager": module-manager.md - "The Module Class": module-class.md - - "The Module Autoloader": module-autoloader.md - "Best Practices when Creating Modules": best-practices.md + - Migration: + - "To Version 3": to-v3-0.md site_name: laminas-modulemanager site_description: "Modular application system for laminas-mvc applications" repo_url: 'https://github.com/laminas/laminas-modulemanager' diff --git a/src/Feature/AutoloaderProviderInterface.php b/src/Feature/AutoloaderProviderInterface.php deleted file mode 100644 index a752c8fc..00000000 --- a/src/Feature/AutoloaderProviderInterface.php +++ /dev/null @@ -1,22 +0,0 @@ -getModule(); - if (! $module instanceof AutoloaderProviderInterface - && ! method_exists($module, 'getAutoloaderConfig') - ) { - return; - } - $autoloaderConfig = $module->getAutoloaderConfig(); - AutoloaderFactory::factory($autoloaderConfig); - } -} diff --git a/src/Listener/DefaultListenerAggregate.php b/src/Listener/DefaultListenerAggregate.php index 3d903b58..d620ec39 100644 --- a/src/Listener/DefaultListenerAggregate.php +++ b/src/Listener/DefaultListenerAggregate.php @@ -41,27 +41,8 @@ public function attach(EventManagerInterface $events, $priority = 1) $configListener = $this->getConfigListener(); $locatorRegistrationListener = new LocatorRegistrationListener($options); - // High priority, we assume module autoloading (for FooNamespace\Module - // classes) should be available before anything else. - // Register it only if use_laminas_loader config is true, however. - if ($options->useLaminasLoader()) { - $moduleLoaderListener = new ModuleLoaderListener($options); - $moduleLoaderListener->attach($events); - $this->listeners[] = $moduleLoaderListener; - } $this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener); - if ($options->useLaminasLoader()) { - // High priority, because most other loadModule listeners will assume - // the module's classes are available via autoloading - // Register it only if use_laminas_loader config is true, however. - $this->listeners[] = $events->attach( - ModuleEvent::EVENT_LOAD_MODULE, - new AutoloaderListener($options), - 9000 - ); - } - if ($options->getCheckDependencies()) { $this->listeners[] = $events->attach( ModuleEvent::EVENT_LOAD_MODULE, diff --git a/src/Listener/ListenerOptions.php b/src/Listener/ListenerOptions.php index cc27fd62..157bbf3a 100644 --- a/src/Listener/ListenerOptions.php +++ b/src/Listener/ListenerOptions.php @@ -22,11 +22,6 @@ */ class ListenerOptions extends AbstractOptions { - /** - * @var array - */ - protected $modulePaths = []; - /** * @var array */ @@ -72,47 +67,6 @@ class ListenerOptions extends AbstractOptions */ protected $moduleMapCacheKey; - /** - * @var bool - */ - protected $useLaminasLoader = true; - - /** - * Get an array of paths where modules reside - * - * @return array - */ - public function getModulePaths() - { - return $this->modulePaths; - } - - /** - * Set an array of paths where modules reside - * - * @param array|Traversable $modulePaths - * @throws Exception\InvalidArgumentException - * @return ListenerOptions Provides fluent interface - */ - public function setModulePaths($modulePaths) - { - if (! is_array($modulePaths) && ! $modulePaths instanceof Traversable) { - throw new Exception\InvalidArgumentException( - sprintf( - 'Argument passed to %s::%s() must be an array, ' - . 'implement the Traversable interface, or be an ' - . 'instance of Laminas\Config\Config. %s given.', - __CLASS__, - __METHOD__, - gettype($modulePaths) - ) - ); - } - - $this->modulePaths = $modulePaths; - return $this; - } - /** * Get the glob patterns to load additional config files * @@ -427,20 +381,4 @@ public static function normalizePath($path) $path = rtrim($path, '\\'); return $path; } - - /** - * @deprecated Use self::useLaminasLoader instead - */ - public function useZendLoader() - { - return $this->useLaminasLoader(...func_get_args()); - } - - /** - * @deprecated Use self::setUseLaminasLoader instead - */ - public function setUseZendLoader($flag) - { - return $this->setUseLaminasLoader(...func_get_args()); - } } diff --git a/src/Listener/ModuleLoaderListener.php b/src/Listener/ModuleLoaderListener.php deleted file mode 100644 index d54c3e17..00000000 --- a/src/Listener/ModuleLoaderListener.php +++ /dev/null @@ -1,127 +0,0 @@ -generateCache = $this->options->getModuleMapCacheEnabled(); - $this->moduleLoader = new ModuleAutoloader($this->options->getModulePaths()); - - if ($this->hasCachedClassMap()) { - $this->generateCache = false; - $this->moduleLoader->setModuleClassMap($this->getCachedConfig()); - } - } - - /** - * {@inheritDoc} - */ - public function attach(EventManagerInterface $events, $priority = 1) - { - $this->callbacks[] = $events->attach( - ModuleEvent::EVENT_LOAD_MODULES, - [$this->moduleLoader, 'register'], - 9000 - ); - - if ($this->generateCache) { - $this->callbacks[] = $events->attach( - ModuleEvent::EVENT_LOAD_MODULES_POST, - [$this, 'onLoadModulesPost'] - ); - } - } - - /** - * {@inheritDoc} - */ - public function detach(EventManagerInterface $events) - { - foreach ($this->callbacks as $index => $callback) { - if ($events->detach($callback)) { - unset($this->callbacks[$index]); - } - } - } - - /** - * @return bool - */ - protected function hasCachedClassMap() - { - if ($this->options->getModuleMapCacheEnabled() - && file_exists($this->options->getModuleMapCacheFile()) - ) { - return true; - } - - return false; - } - - /** - * @return array - */ - protected function getCachedConfig() - { - return include $this->options->getModuleMapCacheFile(); - } - - /** - * loadModulesPost - * - * Unregisters the ModuleLoader and generates the module class map cache. - * - * @param ModuleEvent $event - */ - public function onLoadModulesPost(ModuleEvent $event) - { - $this->moduleLoader->unregister(); - $this->writeArrayToFile( - $this->options->getModuleMapCacheFile(), - $this->moduleLoader->getModuleClassMap() - ); - } -} diff --git a/test/Listener/AbstractListenerTestCase.php b/test/Listener/AbstractListenerTestCase.php index 276de113..da5c92e6 100644 --- a/test/Listener/AbstractListenerTestCase.php +++ b/test/Listener/AbstractListenerTestCase.php @@ -8,8 +8,6 @@ namespace LaminasTest\ModuleManager\Listener; -use Laminas\Loader\ModuleAutoloader; -use LaminasTest\ModuleManager\ResetAutoloadFunctionsTrait; use PHPUnit\Framework\TestCase; use function dirname; @@ -19,16 +17,4 @@ */ class AbstractListenerTestCase extends TestCase { - use ResetAutoloadFunctionsTrait; - - /** - * @before - */ - protected function registerTestAssetsOnModuleAutoloader() - { - $autoloader = new ModuleAutoloader([ - dirname(__DIR__) . '/TestAsset', - ]); - $autoloader->register(); - } } diff --git a/test/Listener/AutoloaderListenerTest.php b/test/Listener/AutoloaderListenerTest.php deleted file mode 100644 index 03cc46bb..00000000 --- a/test/Listener/AutoloaderListenerTest.php +++ /dev/null @@ -1,57 +0,0 @@ -moduleManager = new ModuleManager([]); - $events = $this->moduleManager->getEventManager(); - $events->attach(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener, 1000); - $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new AutoloaderListener, 2000); - } - - public function testAutoloadersRegisteredByAutoloaderListener() - { - $moduleManager = $this->moduleManager; - $moduleManager->setModules(['ListenerTestModule']); - $moduleManager->loadModules(); - $modules = $moduleManager->getLoadedModules(); - self::assertTrue($modules['ListenerTestModule']->getAutoloaderConfigCalled); - self::assertTrue(class_exists('Foo\Bar')); - } - // @codingStandardsIgnoreStart - public function testAutoloadersRegisteredIfModuleDoesNotInheritAutoloaderProviderInterfaceButDefinesGetAutoloaderConfigMethod() - { - $moduleManager = $this->moduleManager; - $moduleManager->setModules(['NotAutoloaderModule']); - $moduleManager->loadModules(); - $modules = $moduleManager->getLoadedModules(); - self::assertTrue($modules['NotAutoloaderModule']->getAutoloaderConfigCalled); - self::assertTrue(class_exists('Foo\Bar')); - } - // @codingStandardsIgnoreEnd -} diff --git a/test/Listener/DefaultListenerAggregateTest.php b/test/Listener/DefaultListenerAggregateTest.php deleted file mode 100644 index 0aa75995..00000000 --- a/test/Listener/DefaultListenerAggregateTest.php +++ /dev/null @@ -1,144 +0,0 @@ -defaultListeners = new DefaultListenerAggregate( - new ListenerOptions([ - 'module_paths' => [ - realpath(__DIR__ . '/TestAsset'), - ], - ]) - ); - } - - public function testDefaultListenerAggregateCanAttachItself() - { - $moduleManager = new ModuleManager(['ListenerTestModule']); - (new DefaultListenerAggregate)->attach($moduleManager->getEventManager()); - - $events = $this->getEventsFromEventManager($moduleManager->getEventManager()); - $expectedEvents = [ - 'loadModules' => [ - 'Laminas\Loader\ModuleAutoloader', - 'config-pre' => 'Laminas\ModuleManager\Listener\ConfigListener', - 'config-post' => 'Laminas\ModuleManager\Listener\ConfigListener', - 'Laminas\ModuleManager\Listener\LocatorRegistrationListener', - 'Laminas\ModuleManager\ModuleManager', - ], - 'loadModule.resolve' => [ - 'Laminas\ModuleManager\Listener\ModuleResolverListener', - ], - 'loadModule' => [ - 'Laminas\ModuleManager\Listener\AutoloaderListener', - 'Laminas\ModuleManager\Listener\ModuleDependencyCheckerListener', - 'Laminas\ModuleManager\Listener\InitTrigger', - 'Laminas\ModuleManager\Listener\OnBootstrapListener', - 'Laminas\ModuleManager\Listener\ConfigListener', - 'Laminas\ModuleManager\Listener\LocatorRegistrationListener', - ], - ]; - foreach ($expectedEvents as $event => $expectedListeners) { - self::assertContains($event, $events); - $count = 0; - foreach ($this->getListenersForEvent($event, $moduleManager->getEventManager()) as $listener) { - if (is_array($listener)) { - $listener = $listener[0]; - } - $listenerClass = get_class($listener); - self::assertContains($listenerClass, $expectedListeners); - $count += 1; - } - - self::assertSame(count($expectedListeners), $count); - } - } - - public function testDefaultListenerAggregateCanDetachItself() - { - $listenerAggregate = new DefaultListenerAggregate; - $moduleManager = new ModuleManager(['ListenerTestModule']); - $events = $moduleManager->getEventManager(); - - self::assertEquals(1, count($this->getEventsFromEventManager($events))); - - $listenerAggregate->attach($events); - self::assertEquals(4, count($this->getEventsFromEventManager($events))); - - $listenerAggregate->detach($events); - self::assertEquals(1, count($this->getEventsFromEventManager($events))); - } - - public function testDefaultListenerAggregateSkipsAutoloadingListenersIfLaminasLoaderIsNotUsed() - { - $moduleManager = new ModuleManager(['ListenerTestModule']); - $eventManager = $moduleManager->getEventManager(); - $listenerAggregate = new DefaultListenerAggregate(new ListenerOptions([ - 'use_laminas_loader' => false, - ])); - $listenerAggregate->attach($eventManager); - - $events = $this->getEventsFromEventManager($eventManager); - $expectedEvents = [ - 'loadModules' => [ - 'config-pre' => 'Laminas\ModuleManager\Listener\ConfigListener', - 'config-post' => 'Laminas\ModuleManager\Listener\ConfigListener', - 'Laminas\ModuleManager\Listener\LocatorRegistrationListener', - 'Laminas\ModuleManager\ModuleManager', - ], - 'loadModule.resolve' => [ - 'Laminas\ModuleManager\Listener\ModuleResolverListener', - ], - 'loadModule' => [ - 'Laminas\ModuleManager\Listener\ModuleDependencyCheckerListener', - 'Laminas\ModuleManager\Listener\InitTrigger', - 'Laminas\ModuleManager\Listener\OnBootstrapListener', - 'Laminas\ModuleManager\Listener\ConfigListener', - 'Laminas\ModuleManager\Listener\LocatorRegistrationListener', - ], - ]; - foreach ($expectedEvents as $event => $expectedListeners) { - self::assertContains($event, $events); - $count = 0; - foreach ($this->getListenersForEvent($event, $eventManager) as $listener) { - if (is_array($listener)) { - $listener = $listener[0]; - } - $listenerClass = get_class($listener); - self::assertContains($listenerClass, $expectedListeners); - $count += 1; - } - self::assertSame(count($expectedListeners), $count); - } - } -} diff --git a/test/Listener/ListenerOptionsTest.php b/test/Listener/ListenerOptionsTest.php deleted file mode 100644 index c952646a..00000000 --- a/test/Listener/ListenerOptionsTest.php +++ /dev/null @@ -1,161 +0,0 @@ - __DIR__, - 'config_cache_enabled' => true, - 'config_cache_key' => 'foo', - 'module_paths' => ['module', 'paths'], - 'config_glob_paths' => ['glob', 'paths'], - 'config_static_paths' => ['static', 'custom_paths'], - ]); - self::assertSame($options->getCacheDir(), __DIR__); - self::assertTrue($options->getConfigCacheEnabled()); - self::assertNotNull(strstr($options->getConfigCacheFile(), __DIR__)); - self::assertNotNull(strstr($options->getConfigCacheFile(), '.php')); - self::assertSame('foo', $options->getConfigCacheKey()); - self::assertSame(['module', 'paths'], $options->getModulePaths()); - self::assertSame(['glob', 'paths'], $options->getConfigGlobPaths()); - self::assertSame(['static', 'custom_paths'], $options->getConfigStaticPaths()); - } - - /** - * @group 6552 - */ - public function testConfigCacheFileWithEmptyCacheKey() - { - $options = new ListenerOptions([ - 'cache_dir' => __DIR__, - 'config_cache_enabled' => true, - 'module_paths' => ['module', 'paths'], - 'config_glob_paths' => ['glob', 'paths'], - 'config_static_paths' => ['static', 'custom_paths'], - ]); - - self::assertEquals(__DIR__ . '/module-config-cache.php', $options->getConfigCacheFile()); - $options->setConfigCacheKey('foo'); - self::assertEquals(__DIR__ . '/module-config-cache.foo.php', $options->getConfigCacheFile()); - } - - /** - * @group 6552 - */ - public function testModuleMapCacheFileWithEmptyCacheKey() - { - $options = new ListenerOptions([ - 'cache_dir' => __DIR__, - 'module_map_cache_enabled' => true, - 'module_paths' => ['module', 'paths'], - 'config_glob_paths' => ['glob', 'paths'], - 'config_static_paths' => ['static', 'custom_paths'], - ]); - - self::assertEquals(__DIR__ . '/module-classmap-cache.php', $options->getModuleMapCacheFile()); - $options->setModuleMapCacheKey('foo'); - self::assertEquals(__DIR__ . '/module-classmap-cache.foo.php', $options->getModuleMapCacheFile()); - } - - public function testCanAccessKeysAsProperties() - { - $options = new ListenerOptions([ - 'cache_dir' => __DIR__, - 'config_cache_enabled' => true, - 'config_cache_key' => 'foo', - 'module_paths' => ['module', 'paths'], - 'config_glob_paths' => ['glob', 'paths'], - 'config_static_paths' => ['static', 'custom_paths'], - ]); - self::assertSame($options->cache_dir, __DIR__); - $options->cache_dir = 'foo'; - self::assertSame($options->cache_dir, 'foo'); - self::assertTrue(isset($options->cache_dir)); - unset($options->cache_dir); - self::assertFalse(isset($options->cache_dir)); - - self::assertTrue($options->config_cache_enabled); - $options->config_cache_enabled = false; - self::assertFalse($options->config_cache_enabled); - self::assertEquals('foo', $options->config_cache_key); - self::assertSame(['module', 'paths'], $options->module_paths); - self::assertSame(['glob', 'paths'], $options->config_glob_paths); - self::assertSame(['static', 'custom_paths'], $options->config_static_paths); - } - - public function testSetModulePathsAcceptsConfigOrTraverable() - { - $config = new Config([__DIR__]); - $options = new ListenerOptions; - $options->setModulePaths($config); - self::assertSame($config, $options->getModulePaths()); - } - - public function testSetModulePathsThrowsInvalidArgumentException() - { - $this->expectException(InvalidArgumentException::class); - $options = new ListenerOptions; - $options->setModulePaths('asd'); - } - - public function testSetConfigGlobPathsAcceptsConfigOrTraverable() - { - $config = new Config([__DIR__]); - $options = new ListenerOptions; - $options->setConfigGlobPaths($config); - self::assertSame($config, $options->getConfigGlobPaths()); - } - - public function testSetConfigGlobPathsThrowsInvalidArgumentException() - { - $this->expectException(InvalidArgumentException::class); - $options = new ListenerOptions; - $options->setConfigGlobPaths('asd'); - } - - public function testSetConfigStaticPathsThrowsInvalidArgumentException() - { - $this->expectException(InvalidArgumentException::class); - $options = new ListenerOptions; - $options->setConfigStaticPaths('asd'); - } - - public function testSetExtraConfigAcceptsArrayOrTraverable() - { - $array = [__DIR__]; - $traversable = new Config($array); - $options = new ListenerOptions; - - self::assertSame($options, $options->setExtraConfig($array)); - self::assertSame($array, $options->getExtraConfig()); - - self::assertSame($options, $options->setExtraConfig($traversable)); - self::assertSame($traversable, $options->getExtraConfig()); - } - - public function testSetExtraConfigThrowsInvalidArgumentException() - { - $this->expectException(InvalidArgumentException::class); - $options = new ListenerOptions; - $options->setExtraConfig('asd'); - } -} diff --git a/test/Listener/ModuleLoaderListenerTest.php b/test/Listener/ModuleLoaderListenerTest.php deleted file mode 100644 index 19bc520d..00000000 --- a/test/Listener/ModuleLoaderListenerTest.php +++ /dev/null @@ -1,120 +0,0 @@ -moduleManager = new ModuleManager([]); - $this->moduleManager->getEventManager()->attach( - ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, - new ModuleResolverListener, - 1000 - ); - } - - public function testModuleLoaderListenerFunctionsAsAggregateListenerEnabledCache() - { - $options = new ListenerOptions([ - 'cache_dir' => $this->tmpdir, - 'module_map_cache_enabled' => true, - 'module_map_cache_key' => 'foo', - ]); - - $moduleLoaderListener = new ModuleLoaderListener($options); - - $moduleManager = $this->moduleManager; - $events = $moduleManager->getEventManager(); - - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES, $events)); - self::assertCount(1, $listeners); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events)); - self::assertCount(0, $listeners); - - $moduleLoaderListener->attach($events); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES, $events)); - self::assertCount(2, $listeners); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events)); - self::assertCount(1, $listeners); - } - - public function testModuleLoaderListenerFunctionsAsAggregateListenerDisabledCache() - { - $options = new ListenerOptions([ - 'cache_dir' => $this->tmpdir, - ]); - - $moduleLoaderListener = new ModuleLoaderListener($options); - - $moduleManager = $this->moduleManager; - $events = $moduleManager->getEventManager(); - - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES, $events)); - self::assertCount(1, $listeners); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events)); - self::assertCount(0, $listeners); - - $moduleLoaderListener->attach($events); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES, $events)); - self::assertCount(2, $listeners); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events)); - self::assertCount(0, $listeners); - } - - public function testModuleLoaderListenerFunctionsAsAggregateListenerHasCache() - { - $options = new ListenerOptions([ - 'cache_dir' => $this->tmpdir, - 'module_map_cache_key' => 'foo', - 'module_map_cache_enabled' => true, - ]); - - file_put_contents($options->getModuleMapCacheFile(), '<' . '?php return array();'); - - $moduleLoaderListener = new ModuleLoaderListener($options); - - $moduleManager = $this->moduleManager; - $events = $moduleManager->getEventManager(); - - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES, $events)); - self::assertCount(1, $listeners); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events)); - self::assertCount(0, $listeners); - - $moduleLoaderListener->attach($events); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES, $events)); - self::assertCount(2, $listeners); - $listeners = iterator_to_array($this->getListenersForEvent(ModuleEvent::EVENT_LOAD_MODULES_POST, $events)); - self::assertCount(0, $listeners); - } -} diff --git a/test/ModuleManagerTest.php b/test/ModuleManagerTest.php index c6d36038..a36f31b4 100644 --- a/test/ModuleManagerTest.php +++ b/test/ModuleManagerTest.php @@ -30,7 +30,6 @@ */ class ModuleManagerTest extends TestCase { - use ResetAutoloadFunctionsTrait; use SetUpCacheDirTrait; /** @@ -43,11 +42,7 @@ protected function setUp() : void $this->sharedEvents = new SharedEventManager; $this->events = new EventManager($this->sharedEvents); $this->defaultListeners = new DefaultListenerAggregate( - new ListenerOptions([ - 'module_paths' => [ - realpath(__DIR__ . '/TestAsset'), - ], - ]) + new ListenerOptions([]) ); } diff --git a/test/ResetAutoloadFunctionsTrait.php b/test/ResetAutoloadFunctionsTrait.php deleted file mode 100644 index 02e07542..00000000 --- a/test/ResetAutoloadFunctionsTrait.php +++ /dev/null @@ -1,76 +0,0 @@ -loaders = spl_autoload_functions(); - if (! is_array($this->loaders)) { - // spl_autoload_functions does not return an empty array when no - // autoloaders are registered... - $this->loaders = []; - } - } - - /** - * @before - */ - protected function preserveIncludePath() - { - $this->includePath = get_include_path(); - } - - /** - * @after - */ - protected function restoreAutoloadFunctions() - { - $loaders = spl_autoload_functions(); - if (is_array($loaders)) { - foreach ($loaders as $loader) { - if (! in_array($loader, $this->loaders, true)) { - spl_autoload_unregister($loader); - } - } - } - } - - /** - * @before - */ - protected function restoreIncludePath() - { - set_include_path((string) $this->includePath); - } -} diff --git a/test/TestAsset/ListenerTestModule/Module.php b/test/TestAsset/ListenerTestModule/Module.php index d581cf0b..caa4e3ff 100644 --- a/test/TestAsset/ListenerTestModule/Module.php +++ b/test/TestAsset/ListenerTestModule/Module.php @@ -9,18 +9,15 @@ namespace ListenerTestModule; use Laminas\EventManager\EventInterface; -use Laminas\ModuleManager\Feature\AutoloaderProviderInterface; use Laminas\ModuleManager\Feature\BootstrapListenerInterface; use Laminas\ModuleManager\Feature\LocatorRegisteredInterface; class Module implements - AutoloaderProviderInterface, BootstrapListenerInterface, LocatorRegisteredInterface { public $initCalled = false; public $getConfigCalled = false; - public $getAutoloaderConfigCalled = false; public $onBootstrapCalled = false; public function init($moduleManager = null) @@ -36,18 +33,6 @@ public function getConfig() ]; } - public function getAutoloaderConfig() - { - $this->getAutoloaderConfigCalled = true; - return [ - 'Laminas\Loader\StandardAutoloader' => [ - 'namespaces' => [ - 'Foo' => __DIR__ . '/src/Foo', - ], - ], - ]; - } - public function onBootstrap(EventInterface $e) { $this->onBootstrapCalled = true; diff --git a/test/TestAsset/NotAutoloaderModule/Module.php b/test/TestAsset/NotAutoloaderModule/Module.php deleted file mode 100644 index cf030ced..00000000 --- a/test/TestAsset/NotAutoloaderModule/Module.php +++ /dev/null @@ -1,26 +0,0 @@ -getAutoloaderConfigCalled = true; - return [ - 'Laminas\Loader\StandardAutoloader' => [ - 'namespaces' => [ - 'Foo' => __DIR__ . '/src/Foo', - ], - ], - ]; - } -} diff --git a/test/TestAsset/NotAutoloaderModule/src/Foo/Bar.php b/test/TestAsset/NotAutoloaderModule/src/Foo/Bar.php deleted file mode 100644 index 20caead7..00000000 --- a/test/TestAsset/NotAutoloaderModule/src/Foo/Bar.php +++ /dev/null @@ -1,15 +0,0 @@ -