backspace/container 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

backspace/container

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

backspace/container 适用场景与选型建议

backspace/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dependency injection」 「container」 「inversion of control」 「backspace」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 backspace/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 backspace/container 我们能提供哪些服务?
定制开发 / 二次开发

基于 backspace/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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