PHPackages                             lorisleiva/lody - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lorisleiva/lody

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lorisleiva/lody
===============

Load files and classes as lazy collections in Laravel.

v0.7.0(3mo ago)957.9M↓20.8%8[1 PRs](https://github.com/lorisleiva/lody/pulls)8MITPHPPHP ^8.2CI passing

Since Jul 4Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/lorisleiva/lody)[ Packagist](https://packagist.org/packages/lorisleiva/lody)[ Docs](https://github.com/lorisleiva/lody)[ GitHub Sponsors](https://github.com/sponsors/lorisleiva)[ RSS](/packages/lorisleiva-lody/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (7)Dependencies (8)Versions (10)Used By (8)

🗄 Lody
======

[](#-lody)

[![Latest Version on Packagist](https://camo.githubusercontent.com/69fccc235aee037b5efd897833f4e9d406821bd2b7e00600b77a3a39ca72fe0f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f7269736c656976612f6c6f64792e737667)](https://packagist.org/packages/lorisleiva/lody)[![GitHub Tests Action Status](https://camo.githubusercontent.com/a905eb07c4409ce3d2156312ec45925c7561fcd44ce431fd09b012bda600a234/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c6f7269736c656976612f6c6f64792f72756e2d74657374732e796d6c3f6272616e63683d6d61696e)](https://github.com/lorisleiva/lody/actions?query=workflow%3ATests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/75ee4796c0689f260f8c6f234ecf4901a0f53257c47a44a3287536f17bf3b7e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f7269736c656976612f6c6f64792e737667)](https://packagist.org/packages/lorisleiva/lody)

Load files and classes as lazy collections in Laravel.

Installation
------------

[](#installation)

```
composer require lorisleiva/lody
```

Usage
-----

[](#usage)

Lody enables you to fetch all existing PHP classes of a provided path (or array of paths) that are relative to your application's base path. It returns a custom `LazyCollection` with helpful methods so that you can filter classes even further based on your own requirement. For example, the code below will fetch all non-abstract instances of `Node` within the given path recursively and register each of them.

```
use Lorisleiva\Lody\Lody;

Lody::classes('app/Workflow/Nodes')
    ->isNotAbstract()
    ->isInstanceOf(Node::class)
    ->each(fn (string $classname) => $this->register($classname));
```

If you want all files instead of existing PHP classes, you may use `Lody::files` instead.

```
use Lorisleiva\Lody\Lody;

Lody::files('app/Workflow/Nodes')
    ->each(fn (SplFileInfo $file) => $this->register($file));
```

Configuration
-------------

[](#configuration)

### Resolving paths

[](#resolving-paths)

When providing paths to the `Lody::files` or `Lody::classes` methods, Lody will automatically assume these paths are within the root of your application unless they start with a slash in which case they are left untouched.

You may configure this logic by calling the `Lody::resolvePathUsing` method on one of your service providers. The example below provides the default logic.

```
Lody::resolvePathUsing(function (string $path) {
    return Str::startsWith($path, DIRECTORY_SEPARATOR) ? $path : base_path($path);
});
```

### Resolving classnames

[](#resolving-classnames)

When using the `Lody::classes` method, Lody will transform your filenames into classnames **by following PSR-4 conventions**. For example, if your filename is `app/Models/User.php` and you have mapped the `App` namespace to the `app` directory in your `composer.json` file, the it will be resolved to `App\Models\User`.

By default, the classname resolution takes into account every single PSR-4 mapping as defined in your `vendor/composer/autoload_psr4.php` file. This means, it will even resolves classes that live in your vendor directory properly.

If your PSR-4 autoload file is located elsewhere, you may configure it by calling the `Lody::setAutoloadPath` method on one of your service providers.

```
Lody::setAutoloadPath('my/custom/autoload_psr4.php');
```

Alternatively, you may override this logic entirely by calling the `Lody::resolveClassnameUsing` method. The example below provides a useful example for Laravel applications.

```
Lody::resolveClassnameUsing(function (SplFileInfo $file) {
    $classnameFromAppPath = str_replace(
        ['/', '.php'],
        ['\\', ''],
        Str::after($file->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
    );

    return app()->getNamespace() . $classnameFromAppPath;
});
```

### Using Lody without Laravel

[](#using-lody-without-laravel)

Lody works out of the box with Laravel because we can use the `base_path` method to access the root of your project.

However, if you wish to use Lody without Laravel, you may simply provide the base path of your application explicitely using the `Lody::setBasePath` method.

```
// Assuming this is executed at the root of your project.
Lody::setBasePath(__DIR__);
```

References
----------

[](#references)

### Lody

[](#lody)

```
// All return an instance of FileLazyCollection (see below).
Lody::files('app/Actions');
Lody::files(['app/Auth/Actions', 'app/Billing/Actions']);
Lody::files('app/Actions', recursive: false); // Non-recursively.
Lody::files('app/Actions', hidden: true); // Includes dot files.
Lody::filesFromFinder(Finder::create()->files()->in(app_path('Actions'))->depth(1)); // With custom finder.

// All return an instance of ClassnameLazyCollection (see below).
Lody::classes('app/Actions');
Lody::classes(['app/Auth/Actions', 'app/Billing/Actions']);
Lody::classes('app/Actions', recursive: false); // Non-recursively.
Lody::classesFromFinder(Finder::create()->files()->in(app_path('Actions'))->depth(1)); // With custom finder.

// Registering custom resolvers.
Lody::resolvePathUsing(fn(string $path) => ...);
Lody::resolveClassnameUsing(fn(SplFileInfo $file) => ...);
```

### FileLazyCollection

[](#filelazycollection)

```
// Transforms files into classnames and returns a `ClassnameLazyCollection`.
// Note that these can still be invalid classes. See `classExists` below.
Lody::files('...')->getClassnames();
```

### ClassnameLazyCollection

[](#classnamelazycollection)

```
// The `classExists` rejects all classnames that do not reference a valid PHP class.
Lody::files('...')->getClassnames()->classExists();

// Note that this is equivalent to the line above.
Lody::classes('...');

// Filter abstract classes.
Lody::classes('...')->isAbstract();
Lody::classes('...')->isNotAbstract();

// Filter classes based on inheritance.
Lody::classes('...')->isInstanceOf(SomeClassOrInterface::class);
Lody::classes('...')->isNotInstanceOf(SomeClassOrInterface::class);

// Filter classes based on traits.
Lody::classes('...')->hasTrait(SomeTrait::class);
Lody::classes('...')->hasTrait(SomeTrait::class, recursive: false); // Don't include recursive traits.
Lody::classes('...')->doesNotHaveTrait(SomeTrait::class);
Lody::classes('...')->doesNotHaveTrait(SomeTrait::class, recursive: false); // Don't include recursive traits.

// Filter classes based on method it contains or not.
Lody::classes('...')->hasMethod('someMethod');
Lody::classes('...')->hasStaticMethod('someMethod'); // Ensures the method is static.
Lody::classes('...')->hasNonStaticMethod('someMethod'); // Ensures the method is non-static.
Lody::classes('...')->doesNotHaveMethod('someMethod');
```

###  Health Score

59

—

FairBetter than 98% of packages

Maintenance79

Regular maintenance activity

Popularity60

Solid adoption and visibility

Community24

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 94.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~286 days

Recently: every ~378 days

Total

7

Last Release

108d ago

PHP version history (4 changes)v0.1.0PHP ^8.0

v0.3.0PHP ^8.0|^8.1

v0.6.0PHP ^8.1

v0.7.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3642397?v=4)[Loris Leiva](/maintainers/lorisleiva)[@lorisleiva](https://github.com/lorisleiva)

---

Top Contributors

[![lorisleiva](https://avatars.githubusercontent.com/u/3642397?v=4)](https://github.com/lorisleiva "lorisleiva (65 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![abkrim](https://avatars.githubusercontent.com/u/1238625?v=4)](https://github.com/abkrim "abkrim (1 commits)")

---

Tags

laravelfilescollectionclassesload

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/lorisleiva-lody/health.svg)

```
[![Health](https://phpackages.com/badges/lorisleiva-lody/health.svg)](https://phpackages.com/packages/lorisleiva-lody)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
