au9500/laravel-auto-singleton
最新稳定版本:v1.0.0
Composer 安装命令:
composer require au9500/laravel-auto-singleton
包简介
PHP 8 attribute to auto-register Laravel singletons.
README 文档
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.
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-02