- How do I install this package in a Laravel project?
- Run `composer require spatie/php-structure-discoverer` in your project root. Laravel-specific features like Artisan commands and cache drivers are automatically available after installation. No additional configuration is required for basic usage.
- Can I discover classes implementing a specific interface in Laravel?
- Yes. Use `Discover::in(app_path())->classes()->implementing(YourInterface::class)->get()` to fetch all classes implementing your interface. The package supports interfaces, traits, enums, and classes with rich metadata like file paths and inheritance chains.
- Does this package work with Laravel’s caching system?
- Absolutely. The package includes a `LaravelDiscoverCacheDriver` that integrates with Laravel’s cache backends (Redis, database, etc.). Run `php artisan structure-scouts:cache` to pre-warm caches for faster production scans.
- What Laravel versions does this package support?
- The package is compatible with Laravel 9.x and 10.x. Check the [GitHub repository](https://github.com/spatie/php-structure-discoverer) for the latest version’s Laravel requirements, as newer releases may add support for newer Laravel versions.
- How do I clear the cache after updating my codebase?
- Use the Artisan command `php artisan structure-scouts:clear` to invalidate caches. For CI/CD pipelines, automate this step post-deploy to ensure scans reflect the latest code. Manual cache clearing is also supported via `Discover::clearCache()`.
- Can I use this package in non-Laravel PHP projects?
- Yes, but with limitations. The core discovery functionality works in vanilla PHP, but Laravel-specific features (Artisan commands, cache drivers) are unavailable. Use `FileDiscoverCacheDriver` for caching in non-Laravel environments.
- How do I optimize performance for large codebases?
- Enable caching (`Discover::cache()`) and use parallel processing with `->parallel()` for faster scans. Pre-warm caches during deployment (`php artisan structure-scouts:cache`) to avoid runtime delays. Test memory usage with `->full()` in high-load environments.
- Are there alternatives to this package for class discovery in Laravel?
- Yes. For simple cases, Laravel’s service container (`app()->tagged()`) or ReflectionClass may suffice. For advanced use cases, consider `phpDocumentor` or `Roave/BetterReflection`, but they lack Laravel-native optimizations like Artisan commands and caching.
- How do I discover enums or traits in my Laravel project?
- Use `Discover::in(app_path())->enums()->get()` for enums or `Discover::in(app_path())->traits()->get()` for traits. The package supports all PHP 8.1+ structures, including metadata like attributes or docblocks, for dynamic tooling like code generation.
- Can I extend the package to add custom discovery conditions?
- Yes. The package allows custom conditions via closures or method chaining (e.g., `->hasAttribute(YourAttribute::class)`). Extend functionality by creating custom `Discover` subclasses or using the `->where()` method for arbitrary logic.