承接 yjx/easy-di 相关项目开发

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

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

yjx/easy-di

Composer 安装命令:

composer require yjx/easy-di

包简介

An easy dependencies inject container, which implements Psr-11.

README 文档

README

EasyDI是一个具有自动依赖注入的小型容器, 遵循PSR-11.

具体详细介绍及讨论请参见blog

容器提供方法:

  • raw(string $id, mixed $value)
    适用于保存参数, $value可以是任何类型, 容器不会对其进行解析.  

  • set(string $id, \Closure|array|string $value, array $params=[], bool $shared=false)   定义服务

  • singleton(string $id, \Closure|array|string $value, array $params=[])   等同调用set($id, $value, $params, true)

  • has(string $id)   判断容器是否包含$id对应条目

  • get(string $id, array $params = [])
    从容器中获取$id对应条目, 可选参数$params可优先参与到条目实例化过程中的依赖注入

  • call(callable $function, array $params=[])   利用容器来调用callable, 由容器自动注入依赖.

  • unset(string $id)   从容器中移除$id对应条目

安装 Installation

Install EasyDi with Composer

composer require yjx/easy-di

基础使用 Basic Usage

容器的实例化

use EasyDi\Container();

$container = new Container();

EasyDI容器管理两种类型的数据: 服务 和 参数(raw)

定义服务和参数

use Psr\Container\ContainerInterface;
use EasyDI\Container();

$c = new EasyDI\Container();

// 定义参数
$c->raw('redis.host', "127.0.0.1");

// 使用闭包
$c->set('redis', function(ContainerInterface $c) {
  $redis = new Redis();
  $redis->pconnect($c->get('redis.host'));
  return $redis;
}, [], true);

$reids = $c->get('redis');  // 由于set时第4个参数$shared设置为true, 因此每次获取的都是同一个对象

闭包中的参数$c由于使用了类型指示, 因此EasyDI会自动完成依赖注入(将它自身注入).

自动依赖解决

示例1

直接拿PHP-DI官方演示代码

class Mailer
{
    public function mail($recipient, $content)
    {
        // send an email to the recipient
    }
}

class UserManager
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function register($email, $password)
    {
        // The user just registered, we create his account
        // ...

        // We send him an email to say hello!
        $this->mailer->mail($email, 'Hello and welcome!');
    }
}

$c = new EasyDI\Container();
$userManager = $c->get('UserManager');

// 等价执行
//$mailer = new Mailer();
//$userManager = new UserManager($mailer);

示例2

class ClassA
{
    protected $b;
    protected $say;

    public function __construct(ClassB $b, ClassC $c, $say="hello")
    {
        $this->b = $b;
        $this->say = $say;
    }

    public function saySth()
    {
      return "{$this->say} {$this->b->saySth()}";
    }
}

class ClassB
{
    protected $msg;

    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    public function saySth()
    {
        return $this->msg;
    }
}

class ClassC
{
}

// 容器实例化
$c = new EasyDI\Container();

// 最基础的配置方式, 未用到容器的依赖注入特性
$c->set('basic', function () {
    return new ClassA(new ClassB("easy-di"), new ClassC(), "I like");
});
$basicService = $c->get('basic');
echo $basicService->saySth().PHP_EOL;   // 输出: I like easy-di

// 利用容器自动解决依赖
$c->set(ClassB::class, ClassB::class, ['easy-di']);         // 配置ClassB的标量依赖, params 等同配置 ['msg'=>"easy-di"]
$c->set('advance', ClassA::class, [2=>"I really like"]);    // 配置advance服务, params 等同配置 ['say'=>"I really like"]
$advanceService = $c->get('advance');                       // ClassA实例化所需的第2个参数$c由容器自动生成实例
echo $advanceService->saySth().PHP_EOL; // 输出: I really like easy-di

示例3 call()

```php
class UserManager
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function register($email, $password)
    {
        // The user just registered, we create his account
        // ...

        // We send him an email to say hello!
        $this->mailer->mail($email, 'Hello and welcome!');
    }

    public function quickSend(Mailer $mailer, $email, $password)
    {
        $mailer->mail($email, 'Hello and welcome!');
    }
}

function testFunc(UserManager $manager)
{
    return "test";
}

// 实例化容器
$c = new EasyDI\Container();

// 输出: 'test'
echo $c->call('testFunc')."\n";	

// 输出: 'test'
echo $c->call(function (UserManager $tmp) {
    return 'test';
});	

// 自动实例化UserManager对象	[$className, $methodName]
$c->call([UserManager::class, 'register'], ['password'=>123, 'email'=>'1@1.1']);	

// 自动实例化UserManager对象	$methodFullName
$c->call(UserManager::class.'::'.'register', ['password'=>123, 'email'=>'1@1.1']);	

// 调用类的静态方法	[$className, $staticMethodName]
$c->call([UserManager::class, 'quickSend'], ['password'=>123, 'email'=>'1@1.1']);	

// 使用字符串调用类的静态方法 $staticMethodFullName
$c->call(UserManager::class.'::'.'quickSend', ['password'=>123, 'email'=>'1@1.1']);	

// [$obj, $methodName] 
$c->call([new UserManager(new Mailer()), 'register'], ['password'=>123, 'email'=>'1@1.1']);	

// [$obj, $staticMethodName]
$c->call([new UserManager(new Mailer()), 'quickSend'], ['password'=>123, 'email'=>'1@1.1']);	

yjx/easy-di 适用场景与选型建议

yjx/easy-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 yjx/easy-di 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-25