spatie/laravel-auto-discoverer
Fast, cached discovery of PHP classes, interfaces, traits, and enums based on conditions. Scan directories to find structures implementing an interface, using attributes, extending classes, and more—ideal for auto-registration and production-ready performance.
app()->bind() calls for interfaces with auto-discovered implementations.ClassLoader (limited to PSR-4).ReflectionClass loops (verbose, no caching).laravel-discoverable-policies (niche use case).Discover facade provides a fluent API, reducing boilerplate.Discover::cache()) mitigates performance overhead in large codebases, critical for runtime discovery (e.g., plugin systems).hasMethod(), usesTrait()), enabling advanced filtering (e.g., "find all classes extending BaseController with a handle() method").implementing()) could return incorrect results. Mitigate via unit tests with edge cases (e.g., abstract classes, anonymous classes).Discover::clearCache()) may be needed post-deployment. Automate via Laravel’s cache:clear or config:clear hooks.enum support).bootstrap/app.php or use Discover::cacheForMinutes(1440).app/ dir) or scoped (e.g., app/Modules/*)? Scoping reduces noise but requires explicit path configuration.Discover or use a golden file of expected outputs?bind() calls with auto-discovered implementations (e.g., Discover::in(app_path())->classes()->implementing(LoggerInterface::class)->bindTo(LoggerService::class)).Discover::facades()).ShouldHandle (e.g., Discover::listenersFor(Event::class)).app()->bind('handler', Handler::class) with:
Discover::in(app_path('Handlers'))->classes()->implementing(HandlerInterface::class)->bindTo('handler');
AppServiceProvider.Reflection-based logic (e.g., in Console commands).Discover for dynamic cases, fall back to manual binding for critical paths.spatie/laravel-package-tools to polyfill features (e.g., enums).composer.json:
"require": {
"spatie/laravel-auto-discoverer": "^1.0"
}
php artisan vendor:publish --provider="Spatie\StructureDiscoverer\StructureDiscovererServiceProvider".DISCOVERER_CACHE_DRIVER=file in .env).AppServiceProvider@boot().Discover in unit tests or use a test double for deterministic results.Discover::clearCache() tests.post-deploy hook).bind()/singleton() calls.config/discoverer.php).implementing(Interface::class)) act as runtime documentation.storage/logs/discovery.log for auditing.Discover::time() for performance tuning.app/, modules/*).DISCOVERY.md file detailing:
app/Handlers, plugins/*).spatie/async).Discover::scanInBackground()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Cache corruption | Stale discovery results | Use Redis for cache; implement checksum validation. |
| Missing classes in scan paths | Broken bindings/listeners | Validate paths in CI; use Discover::paths() to log scanned dirs. |
| PHP Reflection errors (e.g., missing files) |
How can I help you explore Laravel packages today?