VOOZH about

URL: https://deepwiki.com/MahoCommerce/maho-composer-plugin/4.1-autoloadplugin-implementation

⇱ AutoloadPlugin Implementation | MahoCommerce/maho-composer-plugin | DeepWiki


Loading...
Menu

AutoloadPlugin Implementation

Purpose and Scope

The AutoloadPlugin class hooks into Composer's autoload generation process to dynamically inject custom autoload configurations for Maho and Magento 1.x packages. This plugin executes during the PRE_AUTOLOAD_DUMP event, modifying the root package's autoload definition before Composer generates the final autoloader. By dynamically discovering installed packages and their code structures, it eliminates the need for hardcoded autoload mappings in the project's composer.json.

For details on how packages are discovered and filtered, see AutoloadRuntime Package Discovery. For information on code pool hierarchy and include path ordering, see Code Pool Structure and Priority. For PSR-0 mapping and classmap generation logic, see PSR-0 and Class Map Generation.

Sources: src/AutoloadPlugin.php14-45

Plugin Interfaces and Activation

The AutoloadPlugin class implements two Composer plugin interfaces that define its lifecycle and event handling capabilities:

InterfacePurposeRequired Methods
PluginInterfaceMarks the class as a Composer pluginactivate(), deactivate(), uninstall()
EventSubscriberInterfaceDeclares event subscriptionsgetSubscribedEvents()

Activation Process: During plugin activation src/AutoloadPlugin.php51-55 the plugin stores references to the Composer instance and creates a new Filesystem utility for path manipulation. The deactivate() and uninstall() methods are implemented as no-ops src/AutoloadPlugin.php57-63 as the plugin requires no cleanup logic.

Sources: src/AutoloadPlugin.php46-63

Event Subscription

The plugin subscribes exclusively to the PRE_AUTOLOAD_DUMP event, which fires immediately before Composer generates its autoloader:



This early positioning in the lifecycle ensures that all dynamically discovered paths and mappings are incorporated into Composer's standard autoload generation process.

Sources: src/AutoloadPlugin.php65-70

Pre-Autoload Dump Processing

The onPreAutoloadDumpCmd() method src/AutoloadPlugin.php72-113 performs the core autoload configuration modification in five distinct phases:

Phase 1: Root Package and Path Resolution


The method first retrieves the RootPackage instance and determines the project's root directory by getting the parent of the vendor-dir configuration src/AutoloadPlugin.php74-79 This root directory is used later for converting absolute paths to relative paths.

Sources: src/AutoloadPlugin.php72-79

Phase 2: Include Path Generation and Normalization


Include paths are generated by calling AutoloadRuntime::generateIncludePaths() src/AutoloadPlugin.php82 which returns absolute filesystem paths. These are then normalized to relative paths using Filesystem::findShortestPath() src/AutoloadPlugin.php83-86 making them portable across different installation locations.

Sources: src/AutoloadPlugin.php81-86

Phase 3: Files Array Population

The plugin prepends discovered files that must be explicitly loaded (such as bootstrap.php and functions.php) to the files array:


The array_unshift() operation ensures dynamically discovered files are loaded before any manually specified files src/AutoloadPlugin.php89-91

Sources: src/AutoloadPlugin.php88-91

Phase 4: Classmap Extension

Non-PSR-0/PSR-4 compliant classes (like Mage and controller classes) are added to the classmap:


This ensures classes that don't follow standard naming conventions can still be autoloaded src/AutoloadPlugin.php94-96

Sources: src/AutoloadPlugin.php93-96

Phase 5: Optimization Mode Handling

The plugin checks the optimize flag in the event to determine which autoload strategy to use:

ModeFlag ValueStrategyImplementation
OptimizedtrueFull classmap generationAdd all include paths to classmap array
Non-optimizedfalsePSR-0 prefix mappingGenerate and merge PSR-0 configuration

Optimized Mode src/AutoloadPlugin.php98-100: When optimization is enabled, all include paths are added to the classmap array. Composer will then scan these directories and create a comprehensive class-to-file mapping, resulting in faster autoload performance but a larger vendor/composer/autoload_classmap.php file.

Non-Optimized Mode src/AutoloadPlugin.php101-109: Without optimization, the plugin generates PSR-0 prefix mappings via AutoloadRuntime::generatePsr0(). Each prefix is prepended to the existing PSR-0 configuration, allowing Composer to discover classes at runtime by matching class names to file paths.

Sources: src/AutoloadPlugin.php98-109

Root Package Modification

After building the complete autoload configuration, the plugin applies two modifications to the root package:


Autoload Configuration: The setAutoload() method src/AutoloadPlugin.php111 writes the modified configuration, which Composer uses to generate vendor/composer/autoload_psr4.php, autoload_classmap.php, autoload_files.php, etc.

Include Paths: The setIncludePaths() method src/AutoloadPlugin.php112 prepends the generated paths to any existing include paths, making them available to PHP's include and require statements. This is critical for legacy code that relies on include paths rather than autoloading.

Sources: src/AutoloadPlugin.php111-112

Activation and Event Flow Summary

The complete lifecycle showing when and how the plugin executes:


This state diagram illustrates how the plugin remains dormant until the precise moment before autoloader generation, at which point it injects all necessary Maho-specific autoload configurations.

Sources: src/AutoloadPlugin.php1-114

Key Implementation Details

AspectImplementationLocation
Plugin ClassMaho\ComposerPlugin\AutoloadPluginsrc/AutoloadPlugin.php46
InterfacesPluginInterface, EventSubscriberInterfacesrc/AutoloadPlugin.php46
EventScriptEvents::PRE_AUTOLOAD_DUMPsrc/AutoloadPlugin.php68
Handler MethodonPreAutoloadDumpCmd(Event $event)src/AutoloadPlugin.php72
Path UtilityComposer\Util\Filesystemsrc/AutoloadPlugin.php49-54
Optimization Detection$event->getFlags()['optimize']src/AutoloadPlugin.php98
Package ModificationRootPackage::setAutoload(), setIncludePaths()src/AutoloadPlugin.php111-112

Sources: src/AutoloadPlugin.php1-114