Lody
Lody stats
- Downloads
- 1.4M
- Stars
- 78
- Open Issues
- 0
- Forks
- 5
Load files and classes as lazy collections in Laravel.
🗄 Lody
Lody is a Laravel package designed to efficiently load files and classes as lazy collections. It offers a streamlined way to handle large numbers of files or classes within your Laravel application, utilizing lazy loading to enhance performance and resource management.
Key Features:
- Lazy Loading of Classes and Files: Load PHP classes and files as lazy collections, which helps in managing memory usage effectively.
- Customizable Path Resolutions: Automatically resolves paths based on your application's root or custom criteria.
- PSR-4 Support: Seamlessly transforms filenames into fully qualified class names following PSR-4 conventions.
- Flexible Filtering: Provides methods to filter classes by traits, methods, abstract status, and more.
- Non-Laravel Compatibility: Can be configured to work outside of Laravel projects by setting a base path manually.
Installation
Simply run the following Composer command to install:
composer require lorisleiva/lody
Usage Example
To use Lody for loading classes:
use Lorisleiva\Lody\Lody; Lody::classes('app/Workflow/Nodes') ->isNotAbstract() ->isInstanceOf(Node::class) ->each(fn (string $classname) => $this->register($classname));
For loading files:
use Lorisleiva\Lody\Lody; Lody::files('app/Workflow/Nodes') ->each(fn (SplFileInfo $file) => $this->register($file));
Configuration
Adjust path and class name resolutions easily in your service provider:
Lody::resolvePathUsing(function (string $path) { return Str::startsWith($path, DIRECTORY_SEPARATOR) ? $path : base_path($path);}); Lody::resolveClassnameUsing(function (SplFileInfo $file) { $path = str_replace(['/', '.php'], ['\\', ''], Str::after($file->getRealPath(), realpath(app_path()) . DIRECTORY_SEPARATOR)); return app()->getNamespace() . $path;});
Using Without Laravel
For non-Laravel usage, set the base path explicitly:
Lody::setBasePath(__DIR__);
Advanced Features
Lody also allows for advanced file and class manipulations such as custom finder usage, recursive or non-recursive loading, and inclusion of hidden files.
This package is invaluable for developers looking to optimize file and class loading operations within Laravel, providing powerful tools to streamline your application's performance and organization.