Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Auto Discoverer

Laravel Auto Discoverer Laravel Package

spatie/laravel-auto-discoverer

Quickly discover classes, interfaces, enums, and traits in your PHP app that meet certain conditions—such as classes implementing a given interface. Includes built-in caching to keep discovery fast in production.

View on GitHub
Deep Wiki
Context7

Spatie Laravel Auto Discoverer (part of the php-structure-discoverer package) lets you quickly find classes, interfaces, enums, and traits in your PHP application that meet specific conditions. Instead of manually registering or scanning files, you can fluently query your codebase—for example, discovering every class implementing a given interface—and get results in a single call. It's especially handy for building auto-registration and plugin-style architectures in Laravel with minimal boilerplate.

  • Fluent discovery API: filter structures by interface, parent class, attributes, and more
  • Supports classes, interfaces, enums, and traits, with extra metadata per structure
  • Built-in caching keeps discovery fast in production
  • Laravel-friendly: ideal for automatic service, command, or event registration
Frequently asked questions about Laravel Auto Discoverer
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections