welbertymartins/php-singleton
最新稳定版本:v8.2.0
Composer 安装命令:
composer require welbertymartins/php-singleton
包简介
Minimal and testable singleton container for object sharing and retrieval
README 文档
README
A minimal and testable singleton container for managing and retrieving shared instances in PHP.
📦 Installation
composer require welbertymartins/php-singleton
🚀 Quick Start
use WelbertyMartins\Singleton\Singleton; // Store a shared object in the global singleton container Singleton::root()->remember(fn() => new MyService()); // Retrieve it later $service = Singleton::root()->make(MyService::class);
Or use an isolated container:
$container = Singleton::local(); $container->remember(fn() => new MyService(), 'custom_service'); $service = $container->make('custom_service');
🧪 Running Tests
composer install
composer test
Includes PHPUnit tests, PSR-12 code style checking, and static analysis via Psalm.
📁 Project Structure
src/ → Main singleton implementation
test/ → PHPUnit test suite
✅ Features
- 🧩 Global and isolated singleton instances
- 🛡️ Strict type safety
- ⚡ Lightweight, zero-dependency
- ✅ PSR-4 autoloading
- 🧪 Fully tested
📘 API Overview
Singleton::root(): Singleton
Returns the globally shared singleton instance.
Singleton::local(): Singleton
Returns a new isolated singleton instance.
remember(callable $factory, string $name = '', bool $force = false): self
Stores an object returned by a factory under a given name (or class name by default).
make(string $name = ''): ?object
Retrieves a stored instance by name, or the last remembered instance.
📄 License
This project is open-sourced under the MIT license.
🤝 Contributing
Contributions, issues, and feature requests are welcome!
- Fork this repository
- Create a feature branch (
git checkout -b feature/your-feature) - Submit a pull request 🚀
🔗 Useful Links
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-29