定制 jonsa/pimple-ioc 二次开发

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

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

jonsa/pimple-ioc

最新稳定版本:v1.3.0

Composer 安装命令:

composer require jonsa/pimple-ioc

包简介

Resolve classes out of a Pimple container

关键字:

README 文档

README

Class resolver for the Pimple container.

This project is heavily inspired by how Laravel resolve it's classes out of their IoC container. In fact most of the code is taken directly from their Container class.

Installation

Add the IoC container to your composer.json using the command line.

composer require jonsa/pimple-ioc

Usage

The class resolver is registered in Pimple as a ServiceProvider

use Jonsa\PimpleResolver\ServiceProvider;
use Pimple\Container;

$container = new Container();
$container->register(new ServiceProvider());

This will register the make key on the Container which resolves the requested class.

$instance = $container['make']('Acme\MyClass');

and the bind key to bind a definition to a concrete implementation

$container['bind']('Acme\MyInterface', 'Acme\MyClass');

Resolved recursively

Class dependencies are resolved recursively.

interface FooContract {}

class Foo implements FooContract {};

class Bar {
    public function __construct(FooContract $foo) {}
}

class Baz {
    public function __construct(Bar $bar) {}
}

$container['bind']('FooContract', 'FooClass');
$baz = $container['make']('Baz');

Define constructor parameters

To override a class parameter use the parameter array on the resolver method.

class Log {
    public function __construct(Psr\Log\LoggerInterface $logger, $level = Psr\Log\LogLevel::WARNING)
    {
        ...
    }
}

$container['make']('Log', array(
    'level' => Psr\Log\LogLevel::DEBUG
));

Inject into the resolver workflow

To customize a resolved class before it is returned from the resolver, simply listen to the CLASS_RESOLVED event.

use Jonsa\PimpleResolver\ServiceProvider;
use Jonsa\PimpleResolver\Events;
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher;
$container[ServiceProvider::EVENT_DISPATCHER] = function () use ($dispatcher) {
    return $dispatcher;
});

$dispatcher->addListener(Events::CLASS_RESOLVED, function (ClassResolvedEvent $event) {
    $object = $event->getResolvedObject();
    ...
});

Alternatively the EVENT_DISPATCHER key can be populated with a string which in turn returns an event dispatcher from the container

$container[ServiceProvider::EVENT_DISPATCHER] = 'my dispatcher key';

Configuration

The ServiceProvider has three configuration parameters.

class ServiceProvider implements ServiceProviderInterface {
    public function __construct($bindContainerInstance = true, $makeMethod = 'make', $bindMethod = 'bind')
    {
        ...
    }
}

$bindContainerInstance tells the ServiceProvider whether to bind the container instance to the 'Pimple\Container' key. If the container is extended, that class name will also be bound to the container instance.

$makeMethod is used to define which key on the container to be used as the make method.

$bindMethod is used to define which key on the container to be used as the bind method.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-27