- How do I install this package in a Laravel project?
- Run `composer require spatie/php-structure-discoverer` in your project directory. The package includes Laravel-specific features like config publishing and Artisan commands, so no additional setup is required unless you customize the default behavior.
- Can I use this package to dynamically register service providers based on discovered classes?
- Yes, the package’s fluent API lets you discover classes implementing specific interfaces or traits. You can then register them dynamically in your service provider’s `register()` method, making it ideal for auto-discovering command handlers, event listeners, or other service classes.
- Does this package support Laravel’s cache drivers for storing discovered structures?
- Yes, the package integrates with Laravel’s cache system out of the box. You can configure it to use any supported cache driver (e.g., Redis, Memcached, or file-based) via the `structure_scout_directories` configuration, ensuring fast repeated scans in production.
- How do I clear the cache when my project’s class structure changes?
- Run the Artisan command `php artisan structure-scouts:clear` to manually clear the cache. For automated invalidation, consider integrating with Laravel’s `cache:clear` command or using filesystem events to trigger cache warming when relevant files change.
- Will this package work with Laravel 10 and PHP 8.2?
- Yes, the package is fully compatible with Laravel 10 and PHP 8.2. It leverages modern PHP features like enums and attributes while maintaining backward compatibility with older Laravel versions (9.x and PHP 8.1).
- Can I use this package to discover enums or traits, not just classes?
- Absolutely. The package supports discovering enums, interfaces, traits, and classes. For example, you can find all enums in a directory with `Discover::in(__DIR__)->enums()->get()` or traits with `Discover::in(__DIR__)->traits()->get()`.
- How does the caching work, and when should I disable it?
- The package caches discovered structures to avoid repeated reflection overhead. Caching is enabled by default but can be disabled in the config. Disable it in development or CI environments where class structures change frequently, or if you need real-time discovery.
- Is there a way to parallelize class discovery for large codebases?
- Yes, the package supports parallel scanning using `amphp/parallel` for faster discovery in large projects. Enable it by setting `parallel_scanning` to `true` in the config. Note that this adds a dependency, so it’s opt-in.
- Can I extend the package to add custom discovery conditions (e.g., classes with specific annotations)?
- Yes, the package allows custom conditions via closures or classes. For example, you can filter classes by annotations using `Discover::in(__DIR__)->classes()->matching(fn($class) => class_uses($class) === ['SomeAnnotation'])` or by implementing a custom condition class.
- What are the alternatives to this package for class discovery in Laravel?
- Alternatives include `phpDocumentor/reflection-docblock` for manual reflection or `nunomaduro/collision` for detecting class collisions. However, this package stands out for its Laravel-native integration, caching, and support for enums, traits, and metadata-rich discovery out of the box.