au9500 / laravel-auto-singleton
PHP 8 attribute to auto-register Laravel singletons.
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/au9500/laravel-auto-singleton
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.26
- laravel/sail: ^1.48
- orchestra/testbench: ^10.8
- phpunit/phpunit: ^12.4
README
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
- ๐งฉ 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
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
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
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
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
In your config:
'enabled' => false,
This disables all automatic registration.
๐ How It Works
- Laravel boots and loads this package's ServiceProvider
- Config tells the provider which directories to scan
- The Composer classmap is inspected
- Every class in the configured directories is checked via Reflection
- If it has the AutoSingleton attribute:
- It is registered as a singleton
- If the attribute specifies an abstract/interface:
- That abstract is bound to the class
No extra code. No boilerplate. Zero maintenance.
๐งช 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
- PHP 8.2+
- Laravel 10, 11, or 12
๐ค Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Open a Pull Request
๐ License
MIT License.