- How do I discover all classes implementing a specific interface in my Laravel app?
- Use the fluent API: `Discover::in(app_path())->classes()->implementing(YourInterface::class)->get()`. This returns an array of fully qualified class names matching your criteria. The package scans your codebase recursively by default.
- Does this package work with Laravel 8, 9, or 10?
- Yes, **spatie/laravel-auto-discoverer** is fully compatible with Laravel 8+. It leverages PHP 8.1+ features and integrates cleanly with Laravel’s service container and caching systems. No Laravel-specific versioning issues exist.
- Can I filter classes by modifiers like `abstract` or `final` in Laravel?
- Absolutely. Use `withModifier(Modifier::ABSTRACT)` or `withModifier(Modifier::FINAL)` to filter classes. For example: `Discover::classes()->withModifier(Modifier::FINAL)->get()`. This is especially useful for enforcing immutability in services or commands.
- How does caching work in production? Will it slow down my app?
- The package includes a built-in cache layer that stores discovery results. By default, it uses Laravel’s cache driver. Discovery becomes near-instant in production after the first run, with zero runtime overhead for subsequent requests.
- Can I use this to auto-wire services in Laravel’s service container?
- Yes, this is a common use case. Discover all implementations of an interface (e.g., `RepositoryInterface`) and dynamically bind them to the container. Example: `app()->bind(RepositoryInterface::class, fn() => new DiscoveredRepository())`.
- What metadata is available for each discovered structure?
- Each discovered class, interface, or trait includes metadata like file path, namespace, modifiers (abstract/final/static), and whether it implements specific interfaces. This is useful for advanced filtering or logging purposes.
- How do I clear the cache if my codebase changes?
- Use Laravel’s cache clearing commands: `php artisan cache:clear` or `php artisan config:clear`. The package automatically invalidates cached discovery when these commands run, ensuring you always get up-to-date results.
- Is there a performance impact when scanning large codebases?
- The package is optimized for speed. Discovery is parallelized by default (using `amphp/parallel` if installed) and cached. For very large projects, consider limiting the scan scope with `Discover::in('app/Modules')->...` to reduce overhead.
- Can I use this to build a plugin system for Laravel?
- Yes, this is a great fit. Discover all classes annotated with `@Component` or implementing `PluginInterface`, then dynamically load them. Example: `Discover::classes()->implementing(PluginInterface::class)->get()`.
- Are there alternatives to this package for Laravel?
- Alternatives include `illuminate/support` (Laravel’s built-in reflection tools) or `phpDocumentor/reflection-docblock`. However, **spatie/laravel-auto-discoverer** offers a more fluent API, built-in caching, and advanced filtering (e.g., by modifiers or interfaces) tailored for Laravel’s ecosystem.