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 for classes/interfaces/traits/enums and mapping symbols to file paths. Supports simple one-shot map creation or incremental scans with sortable results and reporting of ambiguous class resolutions.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Leverages Laravel’s Composer Dependency: Since Laravel relies on Composer’s autoloader, this package integrates seamlessly into the existing ecosystem without requiring low-level changes to Laravel’s core. It enables fine-grained control over class mapping, which is particularly useful for:
    • Modular Laravel applications (e.g., plugins, SaaS multi-tenancy).
    • Custom autoloading logic (e.g., feature flags, dynamic class loading).
    • Performance optimizations (pre-generating class maps to reduce autoloading overhead).
  • PSR-4/PSR-0 Support: While Laravel defaults to PSR-4, this package supports both PSR-4 and PSR-0, making it versatile for legacy codebases or mixed autoloading scenarios. However, Laravel’s native tools (e.g., composer dump-autoload) may suffice for standard PSR-4 use cases.
  • Complementary to Laravel Caching: Can be used alongside Laravel’s OPcache or bootstrap caching to further optimize cold starts, especially in serverless or high-latency environments.
  • Dynamic Class Loading: Enables runtime class resolution for plugins, user-uploaded code (sandboxed), or A/B testing modules, which Laravel’s static autoloader cannot natively support.

Integration Feasibility

  • Minimal Laravel-Specific Overhead: The package is agnostic to Laravel, requiring only basic PHP integration. A TPM can:
    • Use it to pre-generate class maps during deployment (e.g., in bootstrap/cache).
    • Replace or extend Laravel’s autoloader for custom logic (e.g., via SplClassLoader or ComposerAutoloader).
    • Integrate with Laravel’s service container to dynamically load classes based on runtime conditions.
  • Compatibility with Laravel Ecosystem:
    • Works with Laravel’s Filesystem (Illuminate/Filesystem) for path scanning.
    • Can be hooked into Laravel’s bootstrapping (e.g., AppServiceProvider::boot()) to preload maps.
    • Supports Laravel Mix/Vite for build-time optimizations (e.g., generating class maps during asset compilation).
  • Tooling Synergy: Pairs well with:
    • Laravel Forge/Envoyer for deployment optimizations.
    • Laravel Telescope for debugging ambiguous class resolutions.
    • Laravel Scout for indexing dynamic classes in search.

Technical Risk

  • No Native Laravel Integration: Requires manual bridging (e.g., wrapping the generator in a Laravel service). Risk mitigated by:
    • Creating a Laravel-specific facade (e.g., ClassMapGeneratorService) to abstract the underlying logic.
    • Using Laravel’s Illuminate/Foundation/Application to resolve paths dynamically.
  • PHP Version Constraint: Requires PHP 7.2+, which may block adoption for legacy Laravel 5.x projects. Mitigation:
    • Target Laravel 8.x+ (PHP 7.4+) for new features.
    • Provide fallback logic for older versions (e.g., polyfills or conditional loading).
  • Ambiguous Class Handling: The package detects ambiguous classes (same symbol in multiple files), but Laravel’s autoloader may still fail without additional logic. Mitigation:
    • Implement custom resolution rules (e.g., prioritize certain paths).
    • Log warnings via Laravel’s logging system (Log::warning).
  • Performance vs. Flexibility Tradeoff:
    • Pre-generating class maps reduces autoloading latency but requires storage and synchronization (e.g., invalidating caches on file changes).
    • Dynamic scanning (e.g., for user-uploaded code) adds runtime overhead. Mitigation:
    • Use Laravel’s caching system (Cache::remember) to store maps.
    • Implement event listeners (e.g., filesystem.updated) to refresh maps.

Key Questions for the TPM

  1. Use Case Prioritization:
    • Is this for performance optimization (e.g., cold starts), dynamic loading (e.g., plugins), or legacy migration?
    • Which Laravel features will it augment (e.g., CLI tools, APIs, or modular packages)?
  2. Integration Depth:
    • Should this replace Laravel’s autoloader entirely, or augment it (e.g., for specific paths)?
    • Will it require custom Laravel service providers or facades for seamless use?
  3. Maintenance and Ownership:
    • Who will own the class map generation logic (e.g., DevOps, backend team)?
    • How will cache invalidation be handled (e.g., file watchers, CI/CD hooks)?
  4. Scaling Considerations:
    • For large codebases, will parallel scanning be needed (e.g., using spatie/fork)?
    • How will user-uploaded code be scanned safely (e.g., sandboxed paths)?
  5. Fallback Strategy:
    • What happens if the package fails to generate a map (e.g., due to permission issues)?
    • Should it degrade gracefully (fall back to Composer’s autoloader)?
  6. Testing and Validation:
    • How will ambiguous class resolutions be tested (e.g., unit tests, integration tests)?
    • Will performance benchmarks be established (e.g., autoloading latency before/after)?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Composer Integration: Works natively with Laravel’s Composer-based autoloader. Can be installed via composer require composer/class-map-generator.
    • Filesystem Abstraction: Compatible with Laravel’s Illuminate/Filesystem for path scanning (e.g., storage_path(), app_path()).
    • Service Container: Can be registered as a Laravel service provider for dependency injection.
    • Caching: Integrates with Laravel’s Cache and Filesystem caches (e.g., bootstrap/cache/classmap.php).
  • Tooling Compatibility:
    • Laravel Forge/Envoyer: Pre-generate class maps during deployment to reduce server boot time.
    • Laravel Mix/Vite: Generate class maps during build processes (e.g., for asset compilation).
    • Laravel Artisan: Extend Artisan::command() to include class map generation (e.g., php artisan classmap:generate).
  • Dynamic Features:
    • Plugins/Modules: Use for runtime class loading (e.g., loading modules only when needed).
    • User-Uploaded Code: Scan sandboxed directories (e.g., storage/app/uploads) for dynamic classes.
    • Feature Flags: Load classes conditionally based on runtime logic.

Migration Path

  1. Pilot Phase (Low Risk):
    • Add as a Dependency: Install via Composer and test in a non-production environment.
    • Basic Usage: Use ClassMapGenerator::createMap() to scan a single directory (e.g., app/Modules).
    • Validate Output: Compare generated maps with Laravel’s default autoloader output.
  2. Integration Phase (Medium Risk):
    • Create a Laravel Service: Wrap the generator in a service (e.g., ClassMapGeneratorService) for dependency injection.
    • Hook into Bootstrapping: Register the service in AppServiceProvider to pre-generate maps on app start.
    • Cache Integration: Store maps in bootstrap/cache and invalidate on file changes (e.g., using File::lastModified()).
  3. Advanced Phase (High Risk):
    • Dynamic Loading: Extend Laravel’s autoloader to use custom maps for specific paths (e.g., plugins).
    • CI/CD Optimization: Generate maps in the build pipeline (e.g., GitHub Actions) and deploy with the app.
    • Ambiguous Class Handling: Implement custom resolution logic (e.g., prioritize certain paths) and log warnings.

Compatibility

  • Laravel Versions:
    • Laravel 8.x+ (PHP 7.4+): Full compatibility with modern features (e.g., enums, attributes).
    • Laravel 7.x (PHP 7.3): Works but may miss newer PHP features (e.g., typed properties).
    • Laravel 5.x (PHP 7.0–7.2): Limited support due to PHP version constraints; consider polyfills.
  • Composer Autoloader:
    • Can coexist with Laravel’s autoloader but may require custom logic to merge maps.
    • Use ComposerAutoloader::getClassMap() to integrate with existing autoloading.
  • Third-Party Packages:
    • Spatie/Fork: For parallel scanning in large codebases.
    • Nunomaduro/Collision: For detecting and resolving ambiguous classes.
    • Laravel/PackageTools: For modular package development with dynamic class loading.

Sequencing

  1. **Phase 1: Static Class Maps
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle