Polyscope - The agent-first dev environment for Laravel

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

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu