PHPackages                             au9500/laravel-auto-singleton - 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. au9500/laravel-auto-singleton

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

au9500/laravel-auto-singleton
=============================

PHP 8 attribute to auto-register Laravel singletons.

v1.0.0(7mo ago)077MITPHPPHP ^8.2

Since Dec 2Pushed 7mo agoCompare

[ Source](https://github.com/AU9500/laravel-auto-singleton)[ Packagist](https://packagist.org/packages/au9500/laravel-auto-singleton)[ RSS](/packages/au9500-laravel-auto-singleton/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (5)Versions (3)Used By (0)

🔗 Laravel Auto Singleton
========================

[](#-laravel-auto-singleton)

Automatically register classes as singletons in the Laravel service container using a clean PHP 8 attribute.
No more manual container bindings — just add an attribute and you're done. ⚡

---

✨ Features
----------

[](#-features)

- 🧩 Register any class as a singleton using a simple PHP 8 attribute
- 🎯 Optional: Bind an interface to its implementation
- 🔍 Scans only the directories you choose
- 🔌 Zero boilerplate — no service provider bindings required
- ⚙️ Respects Laravel config and auto-discovery
- 🚀 Works with Laravel 10, 11, 12
- 🪶 Lightweight and dependency-free

---

📦 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require au9500/laravel-auto-singleton

```

Publish the configuration file:

```
php artisan vendor:publish --tag=auto-singleton-config

```

This creates:

```
config/auto-singleton.php

```

---

⚙️ Configuration
----------------

[](#️-configuration)

Example contents of config/auto-singleton.php:

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

    'directories' => [
        base_path('app/Services'),
        base_path('app/Domain'),
    ],
];

```

`directories` determines where attribute scanning is performed.

---

🛠 Usage
-------

[](#-usage)

### 1. 🔒 Register a simple singleton

[](#1--register-a-simple-singleton)

```
use Au9500\LaravelAutoSingleton\Attributes\AutoSingleton;

#[AutoSingleton]
class PaymentService {
}

```

Inject anywhere:

```
public function __construct(PaymentService $service) {
    $this->service = $service;
}

```

Laravel now resolves this service as a singleton.

---

### 2. 🎭 Bind an interface

[](#2--bind-an-interface)

```
interface UserRepository {}

#[AutoSingleton(UserRepository::class)]
class EloquentUserRepository implements UserRepository {
}

```

Now you may type-hint the interface:

```
public function __construct(UserRepository $repo) {
    $this->repo = $repo;
}

```

Laravel automatically binds the interface → implementation as a singleton.

---

### 3. 🚫 Disable auto-registration

[](#3--disable-auto-registration)

In your config:

```
'enabled' => false,

```

This disables all automatic registration.

---

🔍 How It Works
--------------

[](#-how-it-works)

1. Laravel boots and loads this package's ServiceProvider
2. Config tells the provider which directories to scan
3. The Composer classmap is inspected
4. Every class in the configured directories is checked via Reflection
5. If it has the AutoSingleton attribute:
    - It is registered as a singleton
6. If the attribute specifies an abstract/interface:
    - That abstract is bound to the class

No extra code. No boilerplate. Zero maintenance.

---

🧪 Testing
---------

[](#-testing)

The package ships with full PHPUnit and Orchestra Testbench support.
Run the test suite with:

```
vendor/bin/phpunit

```

Tests cover:

- ✔️ Basic singleton registration
- ✔️ Interface binding
- ✔️ Ignoring non-attributed classes
- ✔️ Config disabling behavior
- ✔️ Directory filtering

---

🛡 Requirements
--------------

[](#-requirements)

- PHP 8.2+
- Laravel 10, 11, or 12

---

🤝 Contributing
--------------

[](#-contributing)

1. Fork the repository
2. Create a feature branch
3. Commit your changes
4. Open a Pull Request

---

📄 License
---------

[](#-license)

MIT License.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance65

Regular maintenance activity

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity49

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

2

Last Release

213d ago

Major Versions

v0.1.0 → v1.0.02025-12-02

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/122030400?v=4)[AU9500](/maintainers/AU9500)[@AU9500](https://github.com/AU9500)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/au9500-laravel-auto-singleton/health.svg)

```
[![Health](https://phpackages.com/badges/au9500-laravel-auto-singleton/health.svg)](https://phpackages.com/packages/au9500-laravel-auto-singleton)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M10](/packages/renatomarinho-laravel-page-speed)[illuminate/pagination

The Illuminate Pagination package.

12234.1M1.0k](/packages/illuminate-pagination)[illuminate/pipeline

The Illuminate Pipeline package.

9349.2M282](/packages/illuminate-pipeline)[illuminate/redis

The Illuminate Redis package.

8314.6M375](/packages/illuminate-redis)[illuminate/cookie

The Illuminate Cookie package.

244.6M136](/packages/illuminate-cookie)

PHPackages © 2026

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