- What is the correct Composer package name for spatie/laravel-auto-discoverer?
- The repository and README install target is spatie/php-structure-discoverer, so that is the package name you should require in Composer. The spatie/laravel-auto-discoverer name used in some listings does not match the repository and should be reconciled before committing a lockfile. Verify with composer show spatie/php-structure-discoverer after installation to avoid dependency-confusion risk.
- How do I install and configure the package in a Laravel application?
- Run composer require spatie/php-structure-discoverer and Laravel will auto-discover the service provider. You can optionally publish the config file to adjust structure_scout_directories, which defaults to app_path(). No migrations, queue workers, or external services are required, so setup is limited to configuration and writing StructureScout classes.
- Which Laravel and PHP versions does this package support?
- It targets Laravel 10 or newer on PHP 8.1 or newer, where enums and readonly properties are first-class in the discovered structure model. The core library also works in non-Laravel PHP 8.1 services, but you lose the artisan cache commands and the Laravel cache driver. Check the Packagist page and composer.json constraints for the exact supported matrix before upgrading Laravel.
- How do I discover classes implementing an interface, and can it find enums and traits too?
- Use the fluent API, for example Discover::in(__DIR__)->classes()->implementing(Arrayable::class)->get(). The same query builder can target enums, interfaces, and traits, and filter by parent classes, attributes, and other conditions. Each discovered structure returns extra metadata such as file path and namespace in addition to the fully qualified class name, so narrow the scanned directory and enable caching for large codebases.
- How does the built-in cache work and how do I clear it on deploy?
- Discovered structures are serialized into a cache driver, with a dedicated Laravel cache driver plus the structure-scouts:cache and structure-scouts:clear artisan commands. Warm the cache during deployment and clear it whenever code changes. The most reliable pattern is to clear and rebuild the cache as an explicit deploy step rather than relying on TTL expiry.
- Why does discovery still return deleted or renamed classes after a deployment?
- The cache stores fully qualified class names, so removing or renaming a class leaves stale entries until the cache is cleared. Branch switches and namespace changes have the same effect. Wire structure-scouts:clear plus a rebuild into every deploy and your rollback path, and confirm the cache store is shared correctly across all application servers.
- How do I configure which directories are scanned in a modular or monorepo application?
- StructureScout classes must live in directories listed under structure_scout_directories, which defaults to app_path(). Package or module-provided scouts must be registered manually via StructureScoutManager::add, typically from each module's service provider. In symlinked monorepos, verify the resolved real paths sit inside the configured directories, otherwise discovery will silently skip them.
- How can I make discovery faster and avoid expensive inheritance chain lookups?
- Extending and implementing relationships walk inheritance chains by default, which is correct but costly. Use withoutChains deliberately when you only need direct matches, and cache results for production. For very large codebases, the optional amphp/parallel dependency can parallelize scanning, but confirm your runtime image supports it or stay sequential.
- How do I test code that relies on structure discovery?
- Discovery reads source files rather than the runtime autoloader, so tests should point at real fixture directories and assert on the returned class names and metadata. Clear the cache or use an in-memory driver in tests so previous cases do not pollute results. Avoid mocking the discoverer itself unless you are only testing consumer code, since the value is in verifying real structural matches.
- Is this package worth it compared to writing my own Finder and reflection code?
- If you currently maintain explicit arrays of listeners, policies, payment drivers, or enum-like catalogs, one discovery dependency can replace a lot of brittle registration code. For a one-off query, roughly 50 lines of Symfony Finder plus reflection may be simpler and avoids the cache lifecycle entirely. Adoption metrics for this package are low, so budget time for debugging edge cases and pin the exact version you validate.