composer/class-map-generator
Generate PHP class maps by scanning directories for classes, interfaces, traits, and enums. Create a quick symbol-to-file map or use the generator for multi-path scans, sorting, and reporting ambiguous class definitions. MIT licensed; PHP 7.2+.
app_path(), base_path(), or storage_path() for path resolution.bootstrap/cache or vendor/composer/autoload_classmap.php.composer require composer/class-map-generator.symfony/finder (v7/8).| Use Case | Integration Strategy | Laravel Hooks |
|---|---|---|
| CLI Performance | Pre-generate maps in bootstrap/app.php or a service provider. |
register() in AppServiceProvider |
| Dynamic Plugins | Scan plugin directories at runtime (e.g., PluginManager::load()). |
Custom Facade or Manager Class |
| CI/CD Optimization | Cache maps in GitHub Actions/GitLab CI before tests. | php artisan or custom script |
| Legacy Refactoring | Generate maps for incremental modularization (e.g., app/Modules/). |
php artisan make:module extension |
| User-Uploaded Code | Scan sandboxed directories with exclusion rules. | Storage or Filesystem integration |
// In AppServiceProvider::boot()
$generator = new ClassMapGenerator();
$generator->scanPaths(app_path('Modules'));
$generator->scanPaths(storage_path('plugins'));
$classMap = $generator->getClassMap();
// Cache for 1 hour
Cache::put('class_map', $classMap->getMap(), now()->addHour());
// Custom autoloader
spl_autoload_register(function ($class) use ($classMap) {
if (isset($classMap[$class])) {
require $classMap[$class];
}
});
| Risk | Mitigation Strategy | Severity |
|---|---|---|
| Ambiguous Class Resolutions | Use $classMap->getAmbiguousClasses() to log warnings during development. |
Medium |
| PHP 7.2+ Requirement | Block adoption for PHP 8.1+ apps; use Laravel’s native autoloading. | High |
| Performance Overhead | Benchmark scan times; cache results aggressively (e.g., Redis). | Low |
| Stream Wrapper Limitations | Test with s3://, ftp://, or custom wrappers; fallback to local paths if needed. |
Medium |
| PSR Violation Handling | Integrate with php-cs-fixer or Pint for namespace compliance. |
Low |
| Dynamic Class Generation | Not supported (e.g., eval(), create_function()); use OPcache or reflection. |
High |
| Laravel Facade/Helper Collisions | Exclude vendor/ and bootstrap/cache from scans. |
Low |
migrate, queue:work) will benefit most from pre-generated maps?make:model)?get_declared_classes() suffice for our needs?Autoloader be more maintainable than this package?composer dump-autoload for custom paths.AppServiceProvider).php artisan optimize:classmap (custom command).composer.json.Filesystem or Storage components..phpstorm.meta.php or vs-code class maps dynamically.| Phase | Action | Tools/Leverage | Risk |
|---|---|---|---|
| Assessment | Benchmark current autoloading vs. this package for target use cases (e.g., migrate command). |
microtime(), Laravel Debugbar |
Low |
| Pilot | Integrate into a non-critical CLI tool (e.g., php artisan queue:work). |
Custom Artisan command | Medium |
| Core Integration | Add to AppServiceProvider for boot-time class maps. |
register() hook |
Low |
| CI/CD Optimization | Cache maps in build pipelines (e.g., GitHub Actions). | actions/cache, Laravel Envoyer |
Low |
| Dynamic Loading | Extend for plugins or user-uploaded code with exclusion rules. | Filesystem::exists(), Storage |
Medium |
| Legacy Refactor | Generate maps for modularization (e.g., app/Modules/). |
Custom ModuleServiceProvider |
High |
php_fileinfo, tokenizer (for parsing).xdebug (for debugging ambiguous classes).symfony/finder is widely used in Laravel.composer/class-map-generator pinned to ^1.7.php artisan tinker cold start).composer.json:
"require": {
"composer/class-map-generator": "^1.7"
}
app/:
vendor/bin/php class-map-generator.php app/ > bootstrap/cache/classmap.php
AppServiceProvider:
How can I help you explore Laravel packages today?