Lody

lorisleiva/lody image

Lody stats

Downloads
1.4M
Stars
78
Open Issues
0
Forks
5

View on GitHub →

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.

lorisleiva photo

Hi there my geeky friends! 👋🦄

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Lorisleiva Lody Related Articles

Laravel Actions Package image

Laravel Actions Package

Read article
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech