此包已被弃用且不再维护。未建议替代包。

Borobudur 依赖注入组件

安装: 595

依赖者: 2

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:borobudur-component

dev-master / 0.1.x-dev 2015-12-08 18:45 UTC

This package is not auto-updated.

Last update: 2017-10-13 13:00:21 UTC


README

Build Status License Code Climate Test Coverage Scrutinizer Code Quality SensioLabsInsight

Borobudur\DependencyInjection 为 php 5.4+ 提供依赖注入容器系统

  • 可注入服务的容器参数
  • 服务可从容器中引用参数
  • 不可变参数
  • 将服务描述为闭包或对象
  • 将服务作为单例共享
  • 服务定义
  • 参数、构造函数、方法和闭包注入
  • 构造函数、方法和闭包参数类型提示的显式和隐式自动解析
  • 控制反转(IoC)容器

安装

  1. 获取 Composer
  2. 使用 composer require borobudur/di 安装 Borobudur\DependencyInjection
  3. 在主 PHP 文件中添加 composer 自动加载: require __DIR__.'/vendor/autoload.php';

示例

示例 1 - 基本用法

use Borobudur\DependencyInjection\Di;

class Foo
{
    public $message = 'this is foo';
}

class Baz
{
    public function meetFoo(Foo $foo)
    {
        return $foo->message.' meet baz';
    }
}

$di = new Di();
$service = function(Foo $foo, $prefix = '') { return $prefix.$foo->message; };

$di->call($service); // Output: this is foo
$di->call($service, array('prefix' => 'hello ')); // Output: hello this is foo

$baz = new Baz();
$di->call(array($baz, 'meetFoo')); // Output: this is foo meet baz

示例 2 - 使用实例管理器的构造函数、属性和方法注入。

use Borobudur\DependencyInjection\Di;

class Baz
{
    public $message = 'this is baz';
}

class Bar
{
    public $message = 'this is bar';
}

class Hello
{
    public $greeting = 'hello';
    
    private $baz;
    
    public function __construct(Baz $baz)
    {
        $this->baz = $baz;
    }
    
    public function greetingBaz()
    {
        return $this->greeting.' '.$this->baz->message;
    }
    
    public function greetingBar(Bar $bar)
    {
        return $this->greeting.' '.$bar->message;
    }
}

$di = new Di();
$instance = $di->createInstance('Hello');

$instance->callMethod('greetingBaz'); // Output: hello this is baz
$instance->getOriginalInstance()->greetingBaz(); // Output: hello this is baz

$instance->setProperty('greeting', 'hi');
$instance->callMethod('greetingBar'); // Output: hi this is bar

示例 3 - 服务容器

use Borobudur\DependencyInjection\Container;
use Borobudur\DependencyInjection\Reference;

class Counter
{
    private $counter = 0;
    
    public function count()
    {
        ++$this->counter;
    }

    public function getCounter()
    {
        return $this->counter;
    }
}

class Response
{
    private $counter;
    private $status;
    
    public function __construct(Counter $counter, $status)
    {
        $this->counter = $counter;
        $this->status = $status;
    }
    
    public function print($message)
    {
        $this->counter->count();
        
        return sprintf('%s: %d (%s)', $message, $this->counter->getCounter(), $this->status);
    }
}

$container = new Container();
$container->getParameterBag()->add(array(
    'status' => 'online',
    'message' => 'Hits',
    'counter' => new Reference('counter_service'),
));

// Define singleton service to make counter always increment
$container->share('counter_service', function() {
    return new Counter();
});

// Define view service
$container->set('view', function(Response $response, $message) {
    return sprintf('Statistic - %s', $response->print($message));
});

$container->get('view'); // Output: Statistic - Hits: 1 (online)
$container->get('view'); // Output: Statistic - Hits: 2 (online)
$container->get('view', array('message' => 'Today %message%')); // Output: Statistic - Today Hits: 3 (online)

示例 4 - 控制反转(IoC)

use Borobudur\DependencyInjection\Container;
use Borobudur\DependencyInjection\Definition;

interface EventInterface
{
    public function doIt();
}

class TeachEvent implements EventInterface
{
    public function doIt()
    {
        echo 'Teaching';
    }
}

class FootballEvent implements EventInterface
{
    public function doIt()
    {
        echo 'Playing football';
    }
}

class College
{
    private $event;
    
    public function __construct(EventInterface $event)
    {
        $this->event = $event;    
    }
    
    public startEvent()
    {
        $this->event->doIt();
    }
}

$container = new ContainerBuilder();
$container->setDefinition('teach', new AbstractDefinition('TeachEvent', 'EventInterface'));
$container->setDefinition('college', new Definition('College'));

$container->get('college')->startEvent(); // Output: Teaching

$container->setDefinition('football', new AbstractDefinition('FootballEvent', 'EventInterface'));

$container->get('college')->startEvent(); // Output: Playing football

许可证

MIT 许可证