pmjones/caplet 问题修复 & 功能扩展

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

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

pmjones/caplet

Composer 安装命令:

composer require pmjones/caplet

包简介

A minimal PSR-11 compliant autowiring dependency injection container.

README 文档

README

Caplet is a minimal autowiring dependency injection container to handle basic constructor injection and object factories.

Getting Started

Instantiate Caplet like so:

use Caplet\Caplet;

$caplet = new Caplet();

You can then call one of these PSR-11 methods:

  • get(string $class) : object to get a shared object instance of $class.

  • has(string $class) : bool to see if an instance is available. (This means either a class definition exists; or, in the case of an interface, the interface definition exists and has a factory() entry -- see below for the factory() method.)

Caplet offers this non-PSR-11 method:

  • new(string $class) : object to get a new object instance of $class. (This method is not part of PSR-11.)

Configuration

Configure non-object constructor arguments by passing an array with the structure $config['ClassName']['parameterName'] at Caplet construction time. For example, given the following class ...

namespace Foo;

class Bar
{
    public function __construct(
        protected string $bar,
        protected string $baz
    ) {
    }
}

... you would configure the arguments for its parameters like so:

use Caplet\Caplet;
use Foo\Bar;

$caplet = new Caplet([
    Bar::class => [
        'bar' => 'bar-value',
        'baz' => 'baz-value',
    ];
]);

$bar = $caplet->get(Bar::class);

Alternatively, extend Caplet and override __construct() to accept your own environment or configuration values, then call the parent::__construct() with the $config['ClassName']['parameterName'] structure.

namespace Project;

use Caplet\Caplet;

class ProjectCaplet extends Caplet
{
    public function __construct(array $env)
    {
        parent::__construct([
            Foo::class => [
                'bar' => $env['BAR_VALUE'],
                'baz' => $env['BAZ_VALUE'],
            ],
        ]);
    }
}

Factories

Extending Caplet also allows you to call the protected factory() method inside the constructor to define the object-creation logic for a given type. This allows you to specify concrete classes for instantiation in place of abstracts or interfaces. For example:

namespace Project;

use Caplet\Caplet;
use Project\Log\Logger;
use Psr\Log\LoggerInterface;

class ProjectCaplet extends Caplet
{
    public function __construct(
        string $bar,
        string $baz,
    ) {
        parent::__construct([
            Foo::class => [
                'bar' => 'bar-value',
                'baz' => 'baz-value',
            ],
        ]);

        $this->factory(
            LoggerInterface::class,
            fn (Caplet $caplet) => $caplet->get(Logger::class)
        );
    }
}

As seen above, the callable factory logic must have the signature function (Caplet $caplet), and may specify a return type.

Constructor Parameter Resolution

Caplet will attempt to resolve constructor parameters in this order:

  • First, use an argument from $config, if one is available.
  • Next, try to get() an object of the parameter type.
  • Last, use the default parameter value, if one is defined.

If none of these work, Caplet will throw Exception\NotInstantiated, with a previous exception of Exception\NotResolved.

pmjones/caplet 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-19