定制 codeia/di-senpai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

codeia/di-senpai

Composer 安装命令:

composer require codeia/di-senpai

包简介

DI framework; implements container-interop

README 文档

README

Auto resolving dependency injector. Build your object graphs programmatically.

Installation

$ composer require codeia/di-senpai

Usage

Create a container:

class Module {
    function auth() {
        return new biz\Auth();
    }

    function security(ContainerInterface $c) {
        return new biz\Acl($c->get(SecurityPolicy::class));
    }

    function acl() {
        return new stub\AllowEveryone();
    }

    function users(ContainerInterface $c) {
        return new db\UserRepository($c->get(PDO::class));
    }

    function db() {
        return new PDO('sqlite::memory:');
    }
}

$provided = (new ObjectGraphBuilder(new Module))->withScoped([
    // map the module method names to their return types
    // you need to do this for interfaces; AutoResolve can handle the concrete
    // dependencies
    'auth' => [Authenticator::class],
    'security' => [Authorization::class],
    'acl' => [SecurityPolicy::class],
    'users' => [UserRepository::class],
    'db' => [PDO::class],
])->build();
$container = new AutoResolve($provided);

You can also use any Psr\Container\ContainerInterface impl, but AutoResolve is quite useful and is what makes Senpai work its magic. You can wrap a ContainerInterface in an AutoResolver then use that.

AutoResolve and ObjectGraph both implement ContainerInterface, so you can use them in other frameworks that can take them, like zend-expressive.

Call your senpai:

class LoginController {
    // must use FQCNs in the annotations

    /** @var site\service\Authenticator */
    private $auth;

    /** @var site\service\UserRepository */
    private $users;

    /** @var site\service\Authorization */
    private $checker;

    function __construct(Senpai $pls) {
        $pls->inject($this, Senpai::NO);
    }

    function isFullyUsable() {
        return !empty($this->auth)
            && !empty($this->users)
            && !empty($this->checker);
    }
}

Build your object and their privates will be populated:

Leave out the second argument to inject() if you only want to inject public members.

$loginController = $container->get(LoginController::class);
$loginController->isFullyUsable();
  • Only the object's members are injected. The dependencies' insides are not touched.
  • No method injection is done. I don't believe setter injection should be a thing.

WARNING

This ties your classes to the DI framework! AVOID THIS IF YOU CAN! This severely limits the reusability of your classes. I myself only use this on classes that I probably won't ever reuse (like controllers) and only if they have a lot of dependencies (5 is my threshold).

Other legitimate uses are in frameworks where you don't have control over the instantiation and only provides hooks for your own code. Like activities in Android. Don't know if there are any frameworks that still work like that, it seems that lambdas are all the rage these days. Although again, I only recommend it when you have 5 or more dependencies to populate.

Even so, the AutoResolve and ObjectGraph classes are still handy can be used on their own.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2016-07-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固