承接 solophp/container 相关项目开发

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

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

solophp/container

最新稳定版本:v2.4.0

Composer 安装命令:

composer require solophp/container

包简介

Dependency Injection Container implementing WritableContainerInterface

README 文档

README

Lightweight PSR-11 dependency injection container with auto-wiring, interface binding, and singleton caching.

Latest Version on Packagist PHP Version License Coverage

Features

  • PSR-11 Compatible — Implements WritableContainerInterface from solophp/contracts
  • Auto-wiring — Automatic dependency resolution via constructor reflection
  • Interface Binding — Bind abstracts to concrete implementations
  • Singleton Caching — Each service resolved once and cached
  • Cache Invalidationset() invalidates cached instance, reset() clears all
  • Service Factories — Register services as callable factories
  • Circular Dependency Detection — Cycles in bindings or auto-wiring fail fast with a readable chain

Installation

composer require solophp/container

Quick Example

use Solo\Container\Container;

$container = new Container();

// Register a service factory
$container->set(Database::class, fn($c) => new Database('localhost', 'mydb'));

// Bind interface to implementation
$container->bind(LoggerInterface::class, FileLogger::class);

// Resolve with auto-wired dependencies
$repo = $container->get(UserRepository::class);

Usage

Constructor Registration

$container = new Container([
    'config' => fn() => new Config('config.php'),
    'cache'  => fn($c) => new Cache($c->get('config')),
]);

Auto-wiring

The container automatically resolves class dependencies via constructor reflection:

class UserRepository
{
    public function __construct(
        private Database $database,
        private LoggerInterface $logger
    ) {}
}

// Database and LoggerInterface resolved automatically
$repo = $container->get(UserRepository::class);

Interface Binding

$container->bind(LoggerInterface::class, FileLogger::class);
$container->bind(CacheInterface::class, RedisCache::class);

Re-registering Services

set() invalidates the cached instance, so the next get() uses the new factory:

$container->set(Connection::class, fn() => new Connection('db1'));
$conn1 = $container->get(Connection::class); // Connection to db1

$container->set(Connection::class, fn() => new Connection('db2'));
$conn2 = $container->get(Connection::class); // Connection to db2

Resetting All Instances

When a root dependency changes and the entire dependency tree needs rebuilding:

$container->reset(); // All cached instances cleared

Circular Dependencies

Cycles are detected and throw ContainerException with the full resolution chain:

class A { public function __construct(public B $b) {} }
class B { public function __construct(public A $a) {} }

$container->get(A::class);
// ContainerException: Circular dependency detected: A -> B -> A

The same applies to recursive bindings (bind(A, B); bind(B, A)) and factories that call $c->get() on themselves.

Error Handling

  • Solo\Container\Exceptions\NotFoundException — service not found
  • Solo\Container\Exceptions\ContainerException — service cannot be resolved (non-instantiable, unresolvable parameter, or circular dependency)

Requirements

  • PHP 8.1+

License

MIT License. See LICENSE for details.

统计信息

  • 总下载量: 125
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 2

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固