solophp/container
最新稳定版本:v2.2.0
Composer 安装命令:
composer require solophp/container
包简介
Dependency Injection Container implementing WritableContainerInterface
关键字:
README 文档
README
A lightweight, PSR-11 compatible dependency injection container implementing Solo\Contracts\Container\WritableContainerInterface.
Installation
You can install the package via composer:
composer require solophp/container
Requirements
- PHP 8.1 or higher
- Composer 2.0 or higher
Basic Usage
use Solo\Container\Container; // Create a new container $container = new Container(); // Register a service $container->set('database', function($container) { return new Database('localhost', 'mydb', 'user', 'pass'); }); // Bind an interface to a concrete implementation $container->bind(LoggerInterface::class, FileLogger::class); // Get a service $db = $container->get('database');
Features
- Implements
WritableContainerInterfacefromsolophp/contracts(PSR-11 compatible) - Automatic dependency resolution
- Interface binding
- Singleton instances
- Constructor injection
- Service factories
Advanced Usage
Auto-Resolution
The container can automatically resolve class dependencies:
class UserRepository { public function __construct( private Database $database, private LoggerInterface $logger ) {} } // The container will automatically resolve Database and LoggerInterface $userRepo = $container->get(UserRepository::class);
Multiple Services Registration
$container = new Container([ 'config' => fn() => new Config('config.php'), 'cache' => fn($c) => new Cache($c->get('config')), ]);
Interface Binding
$container->bind(LoggerInterface::class, FileLogger::class); $container->bind(CacheInterface::class, RedisCache::class);
Development
Running Tests
composer test
Code Style
Check code style:
composer cs
Fix code style:
composer cs-fix
Error Handling
The container throws two types of exceptions:
Solo\Container\Exceptions\NotFoundException: When a requested service is not foundSolo\Container\Exceptions\ContainerException: When there's an error resolving a service
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 107
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-13