Weave Code
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

Lody Laravel Package

lorisleiva/lody

Lody loads files or PHP classes from one or more paths as a Laravel LazyCollection. Discover classes via PSR-4, filter by traits/interfaces/abstract status, and iterate to auto-register services, nodes, handlers, etc.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Modularity Alignment: Lody excels in modular architectures where dynamic class discovery is critical (e.g., workflow engines, plugin systems, or event-driven pipelines). Its lazy-loading approach minimizes memory overhead, making it ideal for large codebases or microservices.
  • Laravel Ecosystem Synergy: Leverages Laravel’s base_path(), app_path(), and Finder integration, reducing friction in existing projects. The LazyCollection pattern aligns with Laravel’s fluent query builder style (e.g., Eloquent, Collections).
  • PSR-4 Compliance: Resolves classnames via Composer’s autoload mappings, ensuring compatibility with standard Laravel namespace conventions. This avoids reinventing the wheel for path-to-classname resolution.
  • Extensibility: Custom resolvers (resolvePathUsing, resolveClassnameUsing) allow adaptation to non-standard projects (e.g., custom autoload paths or namespace structures).

Integration Feasibility

  • Low-Coupling Design: No database migrations, configuration files, or middleware required. Installation is a single composer require with optional service provider setup.
  • Backward Compatibility: Works with Laravel 11–13 and PHP 8.1+, covering most modern Laravel projects. Downgrade paths exist for older versions (e.g., Laravel 10 via forks).
  • Standalone Mode: Can be used outside Laravel by setting a base_path, broadening applicability to non-Laravel PHP projects (though Laravel-specific features like app()->getNamespace() are lost).
  • Testing Support: Lazy collections enable efficient unit testing by filtering classes with specific methods/traits (e.g., hasMethod('test') for test data generators).

Technical Risk

Risk Area Mitigation Strategy
Performance Overhead Lazy collections defer execution until iteration, avoiding upfront scans. Benchmark critical paths (e.g., large directories) to validate memory/CPU impact.
Classname Resolution Default PSR-4 resolver may fail for custom autoload setups. Test edge cases (e.g., namespaces with slashes, vendor classes) and override resolveClassnameUsing if needed.
Laravel Version Lock Package supports LTS Laravel versions (11–13). Monitor deprecations in Str:: helpers or Finder usage.
False Positives classExists() filtering is required for Lody::files()->getClassnames() to avoid invalid classes. Document this in usage examples.
Windows Path Handling Test cross-platform path resolution (e.g., app/Workflow/Nodes vs. app\Workflow\Nodes). Package claims Windows support, but validate in CI.
Custom Logic Complexity Overriding resolvers or chaining filters (e.g., hasTrait()->hasMethod()) can become unwieldy. Document patterns for complex queries (e.g., "filter classes with trait X and method Y").

Key Questions for the Team

  1. Use Case Clarity:
    • What specific classes/files need dynamic discovery? (e.g., workflow nodes, event listeners, CLI commands).
    • Are there performance constraints (e.g., scanning 10K+ files)? If so, test lazy collection behavior under load.
  2. Namespace/Path Standards:
    • Does the project use non-standard PSR-4 mappings? If yes, will custom resolvers be needed?
    • Are there hidden files/directories (e.g., .internal) that should be excluded/included by default?
  3. Error Handling:
    • How should invalid classes (e.g., syntax errors) be handled? (Current: classExists() filters them out.)
    • Should missing directories trigger warnings or silent failures?
  4. Testing Strategy:
    • Will Lody be used in tests (e.g., discovering test data generators)? If so, document test-specific filters (e.g., hasMethod('generateTestData')).
    • Are there mocking requirements for isolated testing of discovery logic?
  5. Maintenance:
    • Who will update the package if Laravel 14+ breaks compatibility?
    • Should a custom fork be maintained for long-term stability?
  6. Alternatives:
    • Have we evaluated manual registration (e.g., config('plugins')) or service discovery tools (e.g., Laravel’s AppServiceProvider bindings)?
    • Is there a need for runtime re-scanning (e.g., hot-reloading plugins)?

Integration Approach

Stack Fit

  • Laravel-Centric: Optimized for Laravel’s base_path(), Finder, and Str helpers. Leverages Laravel’s service container for namespace resolution (e.g., app()->getNamespace()).
  • PHP 8.1+ Features: Uses named arguments (e.g., Lody::files(path: '...', recursive: false)) and type hints (SplFileInfo, string).
  • Composer Integration: Relies on vendor/composer/autoload_psr4.php for classname resolution, ensuring consistency with Laravel’s autoloading.
  • Lazy Collections: Built on Laravel’s LazyCollection (or a compatible alternative), enabling memory-efficient iteration.

Migration Path

Step Action
1. Assessment Audit existing class discovery logic (e.g., glob(), ReflectionClass, manual arrays). Identify pain points (e.g., boilerplate, performance, maintainability).
2. Proof of Concept Replace one manual registration (e.g., workflow nodes) with Lody. Example:
```php
// Before: Manual registration in ServiceProvider
$this->register(Node::class);
$this->register(ApprovalNode::class);
// After: Dynamic discovery
Lody::classes('app/Workflow/Nodes')
->isNotAbstract()
->isInstanceOf(Node::class)
->each(fn ($class) => $this->register($class));
```
3. Configuration Add optional service provider setup if custom resolvers are needed:
```php
// app/Providers/AppServiceProvider.php
public function boot(): void
{
Lody::resolvePathUsing(fn(string $path) => Str::startsWith($path, '/') ? $path : base_path($path));
Lody::resolveClassnameUsing(fn(SplFileInfo $file) => ...);
}
```
4. Testing Write tests for discovery logic, focusing on:
- Correct classname resolution (PSR-4 compliance).
- Filtering (e.g., isInstanceOf(), hasTrait()).
- Edge cases (e.g., abstract classes, missing files, vendor classes).
5. Rollout Phase integration by module (e.g., workflows → events → CLI). Monitor memory usage and performance.
6. Documentation Update internal docs with Lody patterns (e.g., "To register all *Command classes: Lody::classes('app/Console/Commands')->hasSuffix('Command')").

Compatibility

Component Compatibility Notes
Laravel Tested on 11–13. Laravel 10 support dropped in v0.7.0.
PHP Requires 8.1+. Uses attributes (e.g., #[ReturnTypeWillChange]), so PHP 8.0 may need polyfills.
Composer Relies on autoload_psr4.php. Custom autoload setups may need resolver overrides.
Filesystems Uses SplFileInfo and Finder. Works with local filesystems; cloud storage (e.g., S3) requires custom path resolution.
Windows Officially supported (tested in v0.3.0). Validate path separators (/ vs. \) in CI.
Non-Laravel PHP Standalone mode works but loses Laravel helpers (e.g., app()->getNamespace()).

Sequencing

  1. Start with Low-Risk Use Cases:
    • Replace static class arrays (e.g., ['Node', 'ApprovalNode']) with dynamic discovery.
    • Example: Event listeners or middleware registration.
  2. Gradually Complexify:
    • Add filters (e.g., isInstanceOf(), hasTrait()) for precise targeting.
    • Example: Register only Serializable workflow nodes.
  3. Optimize Performance:
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport