Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Class Map Generator Laravel Package

composer/class-map-generator

Generate PHP class maps by scanning directories to map classes/interfaces/traits/enums to file paths. Use a simple static helper or an advanced generator to scan multiple paths, sort results, and detect ambiguous class resolutions.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Compatibility: Seamlessly integrates with Laravel’s autoloading system (PSR-4/PSR-0) while offering fine-grained control over class discovery. Ideal for:
    • Hybrid autoloading (e.g., legacy PSR-0 alongside PSR-4).
    • Custom autoloader extensions (e.g., plugin systems, dynamic modules).
    • Performance-critical paths where composer dump-autoload --optimize is insufficient.
  • Laravel-Specific Synergies:
    • Service Provider Bootstrapping: Precompute class maps during bootstrap/app.php or register() to avoid runtime scans.
    • Artisan Commands: Integrate with composer dump-autoload via a custom command (e.g., php artisan classmap:generate).
    • Cache Optimization: Store generated maps in Laravel’s cache (e.g., file, redis, database) for zero-cost autoloading in subsequent requests.
  • Non-Composer Directories: Supports scanning paths outside vendor/, critical for Laravel’s plugin/theme ecosystems or monorepos.

Integration Feasibility

  • Low Friction: Replaces or augments Composer’s autoloader with a single dependency (composer require composer/class-map-generator).
  • API Simplicity:
    • Quick Scan: ClassMapGenerator::createMap('path/to/src') for one-off use cases.
    • Advanced Control: Instantiate ClassMapGenerator to scan multiple paths, filter namespaces, and handle ambiguities.
  • Laravel-Specific Hooks:
    • Service Provider: Register a ClassMapGenerator instance as a singleton.
    • Event Listeners: Trigger map generation on Illuminate\Foundation\Bootstrap\LoadConfiguration or Illuminate\Foundation\Bootstrap\RegisterProviders.
    • Cache Events: Invalidate maps on Illuminate\Cache\Events\CacheMissed or Illuminate\Filesystem\Events\FileUpdated.

Technical Risk

Risk Mitigation Severity
Ambiguous Classes Use getAmbiguousClasses() to log warnings during build time; fail fast in CI if ambiguities exist. Medium
PSR Violations Leverage getPsrViolations() to enforce namespace compliance in CI (e.g., block PRs with violations). Low
Path Normalization Test cross-platform paths (Windows/Linux) early; use realpath() for consistency. Low
Performance Overhead Benchmark against composer dump-autoload --optimize; ensure generation time is amortized over deployments (e.g., CI precompute). Medium
Dependency Bloat Minimal footprint (no Symfony Finder required post-1.7.0); MIT license avoids vendor lock-in. Low
Laravel Cache Sync Implement a ClassMapCache class extending Laravel’s Cache to invalidate maps on file changes. Medium
Stream Wrapper Issues Test with zip://, phar://, or custom wrappers; fallback to filesystem if unsupported. Low

Key Questions

  1. Autoloading Strategy:
    • Should this replace composer dump-autoload entirely, or augment it for specific paths (e.g., plugins)?
    • How will generated maps interact with Laravel’s opcache or PSR-4 autoloader?
  2. CI/CD Integration:
    • Where should maps be generated? (e.g., GitHub Actions, local post-install-cmd, or a custom Artisan command?)
    • Should maps be versioned (e.g., hashed filenames) to avoid stale caches?
  3. Failure Modes:
    • How should ambiguous classes or PSR violations be handled? (e.g., log warnings, fail build, or auto-resolve?)
    • What’s the fallback if map generation fails at runtime?
  4. Scaling:
    • For large codebases (>10K classes), how will memory usage of ClassMapGenerator scale?
    • Should maps be split by namespace (e.g., App, Vendor) for granular caching?
  5. Laravel Ecosystem:
    • How will this interact with Laravel Packages, Lumen, or Livewire autoloading?
    • Should maps be automatically regenerated on composer install or vendor updates?

Integration Approach

Stack Fit

  • Laravel Core: Integrates with:
    • Autoloader: Replace ComposerAutoloader::getInitializer() with a custom loader using precomputed maps.
    • Cache: Store maps in Laravel’s cache backend (e.g., file, redis, database).
    • Events: Hook into Illuminate\Foundation\Bootstrap\Registered or Illuminate\Events\Dispatcher.
  • PHP Ecosystem:
    • Composer: Works alongside composer.json autoload rules; can override or supplement them.
    • PSR Standards: Enforces PSR-4/PSR-0 compliance with violation detection.
    • Stream Wrappers: Supports zip://, phar://, or custom wrappers (tested in 1.6.0+).
  • Build Tools:
    • CI/CD: Precompute maps in GitHub Actions/GitLab CI to avoid runtime scans.
    • Artisan: Expose a classmap:generate command for manual triggers.

Migration Path

Phase Action Tools
Assessment Profile autoloading bottlenecks with Blackfire or Xdebug; identify paths where static maps would help. Blackfire, Xdebug
Pilot Generate maps for a non-critical module (e.g., plugins, legacy code); compare performance against composer dump-autoload. ClassMapGenerator, Benchmark
Core Integration Replace composer dump-autoload for specific paths (e.g., app/Plugins) using a custom Artisan command. Artisan, composer.json
Full Adoption Migrate entire autoloader to use precomputed maps; invalidate caches on file changes. Laravel Cache, File Events
CI/CD Optimization Precompute maps in CI and cache them for deployments; add validation for PSR violations/ambiguities. GitHub Actions, PHPUnit
Monitoring Track autoloading time via Laravel Telescope or custom metrics; alert on map generation failures. Telescope, Prometheus

Compatibility

  • PHP Versions: Supports 7.2+ (tested up to 8.5+ with fixes for stream wrappers).
  • Laravel Versions:
    • Laravel 5.8+: Compatible with PSR-4 autoloader.
    • Laravel 8+: Leverage dependency injection to bind ClassMapGenerator as a service.
    • Lumen: Adaptable with minimal changes (e.g., manual service container binding).
  • Composer: Works with Composer 1.0+; no conflicts with autoload-dev or autoload.
  • Filesystem: Cross-platform path handling (fixed in 1.3.3); supports symlinks, stream wrappers, and non-Unix filesystems.

Sequencing

  1. Pre-requisites:
    • Ensure PHP 7.2+ and Composer 1.0+.
    • Profile autoloading bottlenecks to justify adoption.
  2. Initial Setup:
    • Install package: composer require composer/class-map-generator.
    • Create a custom Artisan command (e.g., ClassmapGenerateCommand) to generate maps.
  3. Core Integration:
    • Replace ComposerAutoloader::getInitializer() with a custom loader using precomputed maps.
    • Store maps in Laravel’s cache (e.g., cache:classmap.php).
  4. Validation:
    • Add CI checks for PSR violations and ambiguous classes.
    • Benchmark against composer dump-autoload --optimize.
  5. Optimization:
    • Split maps by namespace for granular caching.
    • Implement cache invalidation on file changes (e.g., FileUpdated event).
  6. Monitoring:
    • Track autoloading time and map generation failures in production.

Operational Impact

Maintenance

  • Pros:
    • Reduced Autoloading Debugging: Ambiguous classes and PSR violations are caught at build time.
    • **Centralized
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4