- How do I install Lody in a Laravel project?
- Run `composer require lorisleiva/lody` in your project directory. No additional configuration is needed for basic usage, though you can customize path resolution or classname logic if required.
- Can Lody scan files outside Laravel’s base path?
- Yes, by default paths are resolved relative to `base_path()`, but you can override this with `Lody::resolvePathUsing()` to handle absolute paths or custom logic. For example, pass a callback that returns untouched paths starting with `/`.
- Does Lody support non-PSR-4 classname resolution?
- Lody uses PSR-4 mappings from `composer.json` by default, but you can customize classname resolution with `Lody::resolveClassnameUsing()` to support legacy namespaces or custom rules.
- How does lazy loading work in Lody?
- Lody returns a `LazyCollection`, meaning classes/files are only loaded when iterated (e.g., via `each()` or `toArray()`). This avoids memory issues with large directories. Use `count()` to check how many items exist before processing.
- What Laravel versions does Lody support?
- Lody is optimized for Laravel 11+ (PHP 8.1+), but its core functionality works in any PHP environment. The package is actively maintained and supports modern Laravel features like PSR-4 autoloading.
- Can I filter classes by interface or trait?
- Yes, Lody provides fluent methods like `isInstanceOf()` for interfaces or `hasTrait()` for traits. For example, `Lody::classes('path')->isInstanceOf(Contract::class)` filters classes implementing a specific interface.
- How do I test Lody in unit tests?
- Mock the `Lody` facade or use `ClassnameLazyCollection` directly in tests. For example, inject the collection into your service and assert filtered results without hitting the filesystem.
- Will Lody work with plugins or marketplace integrations?
- Absolutely. Lody’s lazy loading and PSR-4 support make it perfect for dynamic plugin discovery. Scan a `plugins/` directory, filter by interfaces (e.g., `PluginContract`), and auto-register them at runtime.
- Are there performance concerns with large directories?
- Lazy loading mitigates overhead, but benchmark with `count()` before `each()` for directories with 10,000+ files. For critical paths, consider caching results or pre-loading during boot.
- What alternatives exist to Lody for class discovery?
- Manual `glob()` + `ReflectionClass` or Symfony’s `Finder` are alternatives, but Lody offers a Laravel-specific fluent API with PSR-4 integration and lazy evaluation. For real-time file watching, combine Lody with `fsnotify` or `spatie/laravel-activitylog`.