承接 backspace/container 相关项目开发

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

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

backspace/container

最新稳定版本:v1.0.4

Composer 安装命令:

composer require backspace/container

包简介

The package enables binding classes to specific implementations and resolving their dependencies.

README 文档

README

A lightweight dependency injection (DI) container for PHP: bind abstractions to implementations and resolve dependencies automatically via the constructor.

Installation

composer require backspace/container

The package has no extra runtime dependencies (empty require).

Quick start

<?php

use Backspace\Framework\Container;

require __DIR__ . '/vendor/autoload.php';

$container = new Container();

// Instantiate a class (if it has a constructor, types are resolved recursively)
$service = $container->make(MyService::class);

Registering bindings (bind)

Abstraction → concrete class

If the second argument is a string class name, the container will build that class when the first is requested:

interface LoggerInterface {}

class FileLogger implements LoggerInterface {}

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

$logger = $container->make(LoggerInterface::class); // FileLogger instance

Factory (Closure)

Full control over object creation; the container is passed into the closure:

$container->bind(CacheInterface::class, function (Container $c) {
    return new RedisCache($_ENV['REDIS_DSN']);
});

$cache = $container->make(CacheInterface::class);

Binding a class to itself

If the second argument is omitted (null), resolving that class calls make() for the same name—useful to register a class before it is requested as a dependency:

$container->bind(AppConfig::class);

$config = $container->make(AppConfig::class);

Constructor autowiring (make)

If there is no binding for a class, reflection is used: for each constructor parameter with a class type, make() is called:

class Database {}

class UserRepository
{
    public function __construct(
        private Database $db
    ) {}
}

$repo = $container->make(UserRepository::class);
// Database is created and injected into UserRepository

Classes without a constructor are created as new ClassName().

Invoking methods with dependency injection (call)

You can call an instance method or a static class method; method parameters with class types are resolved the same way as constructor parameters:

class ReportController
{
    public function __construct(
        private ReportService $reports
    ) {}

    public function index(Request $request): string
    {
        return $this->reports->build($request);
    }
}

$controller = $container->make(ReportController::class);

// Instance method: first argument is the object
$html = $container->call($controller, 'index');

// Static method: first argument is the class name
$html = $container->call(ReportController::class, 'someStaticAction');

Limitations

  • Constructor/method parameters without a class type hint (scalars, arrays, etc.) are not filled automatically in the current implementation—supply them via a factory in bind or adjust signatures.
  • Each make() for a closure binding creates a new instance (there is no separate “singleton” mode in the API).

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固