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(5mo ago)077MITPHPPHP ^8.2

Since Dec 2Pushed 5mo 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 1mo ago

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

36

—

LowBetter than 82% of packages

Maintenance72

Regular maintenance activity

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

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

160d ago

Major Versions

v0.1.0 → v1.0.02025-12-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/a15a63c46d239d42b8ed4c2fe9adbc5fe99eb62f6e5bb90f08021e0c068e8b37?d=identicon)[AU9500](/maintainers/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

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

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