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(8mo ago)26MITPHPPHP &gt;=8.1

Since Oct 22Pushed 8mo 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 today

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

33

—

LowBetter than 72% of packages

Maintenance60

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

254d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5053581?v=4)[Nguyễn Thế Huy](/maintainers/huynt57)[@huynt57](https://github.com/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

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[illuminate/events

The Illuminate Events package.

13557.0M2.1k](/packages/illuminate-events)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[illuminate/broadcasting

The Illuminate Broadcasting package.

7127.2M208](/packages/illuminate-broadcasting)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M151](/packages/laravel-mcp)

PHPackages © 2026

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