jopic/jdi 问题修复 & 功能扩展

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

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

jopic/jdi

最新稳定版本:1.0.0

Composer 安装命令:

composer require jopic/jdi

包简介

simple dependency injection framework

README 文档

README

Build Status

This repository provides a very basic and easy to use dependency injection "framework" for PHP.

Installation

You basically only need to add the following dependency "jopic/jdi": "1.0.0" into your composer.json file.

Usage - Container Setup

At first you have to set up your dependency container:

$container = new Jopic\DI\Container();

Then you can set this container as the active container for dependency injection by:

\Jopic\DI\DependencyInjection::getInstance()->setContainer($container);

Usage - Container Object registration

To be able to inject objects to class fields the dependency injection container needs to know about injectable objects. Therefore you register them on the container by calling the register method.


$container->register("foo", function() {
    return new DummyObject();
});

Please note: it is important to register a closure function (needed for object instantiation).

Usage - Object Injection

Basically the only 3 things you need to do are:

  1. extend the abstract JDIBaseClass
  2. call parent::__construct($this); in your class constructor
  3. annotate inject fields of your class with @inject

Here is a short example of a Injectable class:

class SampleClass extends Jopic\DI\JDIBaseClass {
    /**
     * @inject
     */
    private $foo;
    
    public function __construct() {
        parent::__construct($this);
    }
}

This code assures that if a object with the name "foo" is registered in the dependency injection container it will be available in this class instances automatically.

Lazy Injection

If you define a property as protected, the class constructor of JDIBaseClass will automatically inject this property lazily. That means that the property closure gets executed just before the first property usage.

Here is a short example of a class with directly injected and lazily injected properties:

class SampleClass extends Jopic\DI\JDIBaseClass {
    /**
     * this value gets injected on constructor call
     * @inject
     */
    private $foo;
    
    /**
     * this value gets injected just before the first getFoo2() call
     * @inject
     **/
    protected $foo2;
    
    public function __construct() {
        parent::__construct($this);
    }
    
    public function getFoo() {
        return $this->foo;
    }
    
    public function getFoo2() {
        return $this->foo2;
    }
}

Request for comment

If you find any bugs or if you find something (or everything ;-) ) inconvenient please don't hesitate to contact me either directly via github or via e-mail (admin [at] jopic.at).

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2015-03-29