承接 solophp/container 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

solophp/container

最新稳定版本:v2.2.0

Composer 安装命令:

composer require solophp/container

包简介

Dependency Injection Container implementing WritableContainerInterface

README 文档

README

Latest Version on Packagist License PHP Version Coverage

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 WritableContainerInterface from solophp/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 found
  • Solo\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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-13