- How do I install spatie/laravel-auto-discoverer for Laravel?
- Run `composer require spatie/laravel-auto-discoverer` in your project. The package auto-discovers Laravel’s service container and requires no additional setup unless you need to publish config files. For non-Laravel PHP apps, use `spatie/php-structure-discoverer` directly.
- What Laravel versions does this package support?
- The package is fully compatible with Laravel 8+ and is regularly tested up to the latest stable release. It leverages Laravel’s service container, caching, and Artisan commands, ensuring smooth integration with modern Laravel versions.
- Can I use this to dynamically register service providers or event listeners?
- Yes. Use the `Discover` facade to find classes implementing specific interfaces or using traits, then dynamically register them in your `AppServiceProvider` or via Laravel’s service container. This is ideal for plugin systems or modular applications.
- How does caching work, and when should I clear it?
- The package caches discovery results by default using Laravel’s cache drivers (Redis, database, etc.). Clear the cache manually with `php artisan structure-scouts:clear` when your codebase changes, or automate it via Laravel’s filesystem events or deployment hooks.
- Will this slow down my Laravel application in production?
- No, if configured correctly. The first scan may take time for large codebases, but subsequent queries use cached results. Enable parallel processing (`amphp/parallel`) for faster initial scans, and pre-warm caches during deployment for near-instant production performance.
- Can I exclude certain directories (e.g., vendor/, node_modules/) from discovery?
- Yes. Configure the `structure_scout_directories` array in your `config/structure-scouts.php` to limit scans to specific paths. This improves performance and avoids scanning unnecessary files like `vendor/` or generated directories.
- How do I find all classes implementing an interface, like Arrayable?
- Use the `Discover` facade: `Discover::in(__DIR__)->classes()->implementing(Arrayable::class)->get()`. This returns an array of fully qualified class names matching your condition. You can chain additional filters like `usingTrait()` or `named()` for precision.
- Is there a way to test discovery accuracy in my Laravel app?
- Yes. Write unit tests using the `Discover` facade to verify class discovery logic. For example, assert that `Discover::classes()->implementing(YourInterface::class)->count()` matches your expected number of classes. Log results for debugging if needed.
- Can I use this for security-sensitive operations, like exposing class metadata?
- Exercise caution. While the package itself is safe, exposing class metadata (e.g., via APIs) could reveal internal structures. Restrict access to discovery endpoints and validate results before exposing them to users or third parties.
- What are the alternatives to spatie/laravel-auto-discoverer?
- For basic reflection, PHP’s native `ReflectionClass` or `class_uses()`/`class_implements()` suffice, but lack caching and advanced filtering. For Laravel-specific needs, consider `laravel-discover` or `phpdocumentor/reflection-docblock`, though neither offers the same combination of performance, caching, and metadata richness.