jshannon63/cobalt 问题修复 & 功能扩展

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

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

jshannon63/cobalt

Composer 安装命令:

composer require jshannon63/cobalt

包简介

A fast, autowired PSR-11 dependency injection container for PHP with cached resolution closures.

README 文档

README

CI codecov PHPStan PHP Version Latest Version on Packagist Software License

Cobalt — An Autowired Dependency Injection Container for PHP

Realized in fewer than 200 lines of source.

Well documented, perfect for building/learning.

100% line and method coverage on PHP 8.2, 8.3, and 8.4.

One of the fastest PHP dynamic autowired containers available.

Cobalt was created to push the performance limits of what a PHP-based dynamic autowired DI container can achieve. Container::class implements the PSR-11 ContainerInterface and provides many of the features found in more notable container projects. Resolution closures are cached after the first build, so subsequent lookups skip all reflection work. Cobalt and its simple, heavily commented source are perfect for learning, or for embedding inside projects and frameworks.

The Cobalt service container has the following features:

  1. Single class container implementing the PSR-11 ContainerInterface v2.
  2. ArrayAccess methods for container bindings.
  3. Constructor injection of type-hinted dependencies.
  4. Dependency injection through bind() method closures.
  5. Autowired dependency resolution using Reflection.
  6. Top-down inversion of control (IoC).
  7. Shared mode option (singleton only).
  8. Bind existing instances into the container.
  9. A self-binding global container instance.

Installation

composer require jshannon63/cobalt

Usage

Creating the container

use Jshannon63\Cobalt\Container;

// create a default (prototype) container
$app = new Container();

// or, create a singleton-only services container
$app = new Container('shared');

Binding into the container

Binding does not instantiate the class. Instantiation is deferred until the binding is requested from the container. bind() accepts three parameters: the abstract name, the concrete implementation, and a boolean for whether the binding is a singleton. The abstract name is free-form and acts as the key in the binding registry.

bind(string $abstract, mixed $concrete = null, bool $singleton = false): void

// a simple binding using only the class name
$app->bind(Foo::class);

// or, bind an interface with a desired concrete implementation —
// you can swap the concrete out in one place in your code.
$app->bind(FooInterface::class, Foo::class);

// or, bind an interface or other label to a closure to directly
// control dependency injection.
$app->bind(FooInterface::class, fn () => new Foo('123-456-7890'));

// or, use array access to bind a new instance directly.
$app['Foo'] = new Foo();

Resolving out of the container

$instance = resolve(string $id): mixed (resolve checks for an existing binding before instantiating)

$foo = $app->resolve(FooInterface::class);

// or
$foo = $app[FooInterface::class];

// or
$foo = $app->get(FooInterface::class);

Trying to resolve a missing binding throws NotFoundException per PSR-11.

Using the make() method

make() is bind() then resolve() — useful for one-shot instantiation.

$instance = make(string $abstract, mixed ...$args): mixed

$foo = $app->make(FooInterface::class, Foo::class);

Creating an alias to a binding

alias(string $alias, string $binding): void

Allows creating additional string IDs for accessing existing container bindings.

$app->alias('myfoo', FooInterface::class);

Binding an existing instance

Pass an object to bind() and Cobalt registers it as a singleton automatically (a pre-built instance is by definition shared).

$app->bind('Foo', new Foo);

Checking if a binding exists

$bool = has(string $abstract): bool

$bool = $app->has('Foo');

Getting the values of a single binding

$array = getBinding(string $abstract): array

$array = $app->getBinding($abstract);

Getting a list of bindings

$array = getBindings(): array

$array = $app->getBindings();

Development

Run the full quality bar locally:

composer install
composer check       # lint + analyse + test

Or individually:

composer test         # PHPUnit
composer lint         # Laravel Pint (PSR-12 + opinionated)
composer analyse      # PHPStan level 9

License

The MIT License (MIT). Please see License File for more information.

jshannon63/cobalt 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-15