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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
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
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing 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
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten