PHPackages                             huynt57/laravel-singletonize - 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. huynt57/laravel-singletonize

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

huynt57/laravel-singletonize
============================

Laravel package to default container resolution to singleton

1.0.5(6mo ago)26MITPHPPHP &gt;=8.1

Since Oct 22Pushed 6mo agoCompare

[ Source](https://github.com/huynt57/laravel-singletonize)[ Packagist](https://packagist.org/packages/huynt57/laravel-singletonize)[ RSS](/packages/huynt57-laravel-singletonize/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (10)Used By (0)

Laravel Singletonize
====================

[](#laravel-singletonize)

Laravel Singletonize is a lightweight package that switches the default behaviour of the Laravel service container to resolve dependencies as singletons. Once installed, every concrete that is resolved without an explicit binding will automatically be cached and reused by the container.

Compatibility
-------------

[](#compatibility)

Laravel Singletonize is tested against Laravel 10.x, 11.x, and 12.x applications.

Why this package?
-----------------

[](#why-this-package)

Laravel's automatic resolution relies heavily on reflection to discover constructor dependencies on every request. For large codebases with many services, repeatedly reflecting on types becomes expensive and increases bootstrapping time. By promoting implicitly resolved services to singletons, the package ensures the reflection work is only performed once per service lifecycle, which can noticeably improve application performance.

This package was inspired by a Laravel core PR that aimed to improve container performance, although it was never merged into the framework itself: [laravel/framework#51209](https://github.com/laravel/framework/pull/51209)

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

[](#installation)

```
composer require huynt57/laravel-singletonize

```

The service provider is auto-discovered through Laravel's package discovery. If you prefer to register it manually, add the provider to your `config/app.php` file:

```
'providers' => [
    Huynt57\LaravelSingletonize\SingletonizeServiceProvider::class,
],

```

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

[](#configuration)

Publish the configuration file to toggle the behaviour at runtime:

```
php artisan vendor:publish --tag=config --provider="Huynt57\\LaravelSingletonize\\SingletonizeServiceProvider"

```

The published file `config/laravel-singletonize.php` contains a single option:

```
return [
    'enabled' => true,
];

```

Set `enabled` to `false` to restore Laravel's default behaviour of creating a fresh instance on every resolution unless it has been explicitly registered as a singleton.

How it works
------------

[](#how-it-works)

Singletonize boots a small helper (`Huynt57\LaravelSingletonize\Singletonizer`) that hooks into Laravel's container before any concrete is resolved. The helper inspects the abstract being requested and, the first time it encounters a class name, rewrites the container's internal binding definition to:

1. Wrap the original concrete (class name, closure, or alias) in a memoized closure that stores the first resolved instance in `$container->instances`.
2. Mark the binding as `shared`, so future resolutions skip the normal reflection/build process.

Because the memoized closure still receives the container and the original parameter array, contextual bindings and parameterised `make()` calls continue to work. When parameters are provided, the memoized closure bypasses the cached instance and rebuilds the object, ensuring runtime factories keep their expected behaviour.

The helper only runs once per abstract per request, so the bookkeeping overhead is minimal while every implicitly resolved service becomes a singleton automatically.

Testing
-------

[](#testing)

A lightweight test harness is provided in `tests/run.php`. Execute the suite with:

```
composer test

```

The tests cover:

- Implicit binding reuse for classes without manual registration.
- Nested dependency reuse.
- Parameterised resolution exclusions.
- Closure bindings.
- Configuration toggling.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance66

Regular maintenance activity

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

6

Last Release

200d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/fa406ee41c5f0dbae844c00f11d265ef8d7d7d091b62fdd613f1269ed1185b11?d=identicon)[huynt57](/maintainers/huynt57)

---

Top Contributors

[![huynt57](https://avatars.githubusercontent.com/u/5053581?v=4)](https://github.com/huynt57 "huynt57 (27 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/huynt57-laravel-singletonize/health.svg)

```
[![Health](https://phpackages.com/badges/huynt57-laravel-singletonize/health.svg)](https://phpackages.com/packages/huynt57-laravel-singletonize)
```

###  Alternatives

[illuminate/events

The Illuminate Events package.

13454.3M1.7k](/packages/illuminate-events)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[illuminate/broadcasting

The Illuminate Broadcasting package.

7126.5M178](/packages/illuminate-broadcasting)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)[igorsgm/laravel-git-hooks

🪝• Efficiently manage Git hooks in Laravel projects. Enhance code quality, save time on reviews, and prevent bugs from entering your repository.

2931.7k1](/packages/igorsgm-laravel-git-hooks)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)

PHPackages © 2026

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