pixielity / class-autoload
Advanced class autoloader for Laravel applications with support for modules, class overrides, and PSR-4 autoloading
Requires
- php: ^8.0
- illuminate/support: *
Requires (Dev)
- laravel/pint: *
- mockery/mockery: ^1.5
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.0|^10.0
README
Advanced class autoloader for Laravel applications with support for modules, class overrides, and PSR-4 autoloading.
Features
- Module Support: Automatically register modules and their namespaces
- Class Overrides: Override classes without modifying the original code
- PSR-4 Autoloading: Automatically register PSR-4 namespaces
- Generated Code: Support for dynamically generated classes
- Composer Integration: Seamlessly integrates with Composer's autoloader
- Laravel Integration: Works with Laravel's service container and facades
Installation
You can install the package via composer:
composer require pixielity/class-autoload
Usage
Basic Usage
use Pixielity\ClassAutoload\ClassAutoloader; // Create a new instance $autoloader = ClassAutoloader::make(); // Scan for modules and classes $autoloader->scan(); // Register with Composer's autoloader $autoloader->register();
Laravel Integration
The package automatically registers a service provider that initializes the autoloader. You can publish the configuration file:
php artisan vendor:publish --tag=class-autoload-config
Then, you can use the facade:
use Pixielity\ClassAutoload\Facades\ClassAutoloader; // Add a path to scan ClassAutoloader::path('/path/to/modules'); // Register a module ClassAutoloader::module('MyModule', '/path/to/modules/my-module', 'App\\Modules\\MyModule'); // Register a class override ClassAutoloader::override('Original\\Class', 'Override\\Class');
Dependency Injection
The package provides an interface ClassAutoload\Contracts\ClassAutoloaderInterface
that you can use for dependency injection:
use Pixielity\ClassAutoload\Contracts\ClassAutoloaderInterface; class MyService { protected $autoloader; public function __construct(ClassAutoloaderInterface $autoloader) { $this->autoloader = $autoloader; } public function registerModule($name, $directory, $namespace = null) { return $this->autoloader->module($name, $directory, $namespace); } }
This makes your code more testable, as you can easily mock the autoloader in your tests:
use Pixielity\ClassAutoload\Contracts\ClassAutoloaderInterface; use Mockery; $autoloaderMock = Mockery::mock(ClassAutoloaderInterface::class); $autoloaderMock->shouldReceive('module')->once()->andReturn($autoloaderMock); $service = new MyService($autoloaderMock); $service->registerModule('TestModule', '/path/to/module');
Early Bootstrap Integration
For the earliest possible integration (before any service providers are loaded), add this to your bootstrap/app.php
file:
/* |-------------------------------------------------------------------------- | Initialize ClassAutoloader |-------------------------------------------------------------------------- | | Register the ClassAutoloader to handle modules and custom namespaces | before any service providers are loaded. | */ require_once __DIR__ . '/../vendor/pixielity/class-autoload/src/ClassAutoload/ComposerScripts.php'; \ClassAutoload\ComposerScripts::bootstrap();
Composer Scripts
The package registers several Composer scripts:
post-autoload-dump
: Initializes the autoloader after Composer generates the autoload filespost-update-cmd
: Initializes the autoloader after Composer updates packagespost-install-cmd
: Initializes the autoloader after Composer installs packagesclassmap
: Generates a classmap for all modules and registers it with Composer
You can manually run the classmap generation:
composer classmap
Configuration
The configuration file allows you to customize the autoloader:
return [ 'base_dir' => base_path(), 'generated_code_dir' => storage_path('framework/classes'), 'vendor_dir' => base_path('vendor'), 'paths' => [ base_path('app'), base_path('src'), base_path('modules'), base_path('plugins'), ], 'auto_register' => true, 'auto_scan' => true, ];
License
The MIT License (MIT). Please see License File for more information.