定制 ericsnguyen/simple-di 二次开发

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

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

ericsnguyen/simple-di

Composer 安装命令:

composer require ericsnguyen/simple-di

包简介

a simple dependency injection library

README 文档

README

An easily setup dependency injection library that help you can set up Dependency injection in your project faster.

Documentation

Step I.

Mark with your class as @Export() to Simple DI release this class need to be keep on container. In any other class. if you want to inject by property, mark this property as @Import()

use SimpleDi\Annotations\Export;
use SimpleDi\Annotations\Import;

/**
* @Export()
 */
class TrainingPlanController extends AbstractController
{
    /**
     * @Import()
     * @var MessageFactory
     */
    protected MessageFactory $mf;
    /**
     * @Import()
     * @var CourseService
     */
    protected CourseService $courseService;
}

We have an hacked for class that ended with Controller. It doesn't need mark as @Export at all.

Step II

I have a small script that find any class that mark with @Export and create your own $register array. so you don't need to create by hand anymore. Stand at your project root. then run this command:

  • PROJECT = root project folder
  • FILE = the file that store registry map
PROJECT=application FILE=proxy/registry.php ./vendor/ericsnguyen/simple-di/bin/generate

it would print some error, but ignore it, after run, double check it.

Step III

At your index.php or any start endpoint file. put the configuration for SimpleDi

// you can put your `array` that act as your old DI container to it as param
$container = new class implements \SimpleDi\Container\IContainer(){
    // in my library we have SdContainer class that implements IContainer
    // but you can implement one if you want.
};

$register = [
    YourInterface::class=> YourImplementClass:class
];

SimpleDi::build([
       // I have a variable $GLOBALS act as an instances holder for my container
      'container'=>new SdContainer($GLOBALS),
      // an $register array.
      'registry'=> include PROJECT_ROOT."/proxy/registry.php",
       // it is optional -> get any singleton existed in your project, but you can ignore it.
      'root_class'=> Singleton::class
]);

and at anywhere you want to get this instance:

 // at my routes table reader.
$controller = App::resolver($controllerName);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固