codejet/bucket

一个轻量级且与 container-interop 兼容的 DI 容器对象。

1.0.0 2017-08-02 18:12 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:12 UTC


README

     .=======.
    /         \
   /   _____   \
  /.-'"     "`-.\
 [(             )]
  |`-.._____..-'|
  |             |
  |             |
  |   bucket    |
  \             /
   `-.._____..-'

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

一个方便的与container-interop兼容的 DI 容器对象。

易于使用、易于理解且易于扩展。

安装

通过 Composer

$ composer require codejet/bucket

使用方法

创建一个 bucket

$bucket = new CodeJet\Bucket\Bucket();

添加值

使用字符串作为键,传递任何非 \Closure 的值,它将被原样存储。

$bucket->add('value-id', 'The value of the value.');

添加工厂

使用字符串作为键并传递一个 \Closure 作为值将存储一个工厂。闭包可以接受 \Interop\Container\ContainerInterface 作为其唯一参数。当首次调用 $bucket->get('service-id') 时,bucket 将将自身(或指定的代理)传递给工厂,并将返回的数据作为后续请求相同 ID 的值存储。

$bucket->add(
    'service-id',
    function (\Interop\Container\ContainerInterface $bucket) {
        return new \stdClass();
    }
);

检索项

var_dump($bucket->has('value-id')); // bool(true)
var_dump($bucket->get('value-id')); // string(23) "The value of the value."

var_dump($bucket->has('service-id')); // bool(true)
var_dump($bucket->get('service-id')); // class stdClass#4 (0) { }

委托查找功能

container-interop 委托查找标准提供了一种容器可以使用替代容器进行依赖注入的方法。

$delegateLookupContainer = new \League\Container\Container();
$delegateLookupContainer->add('importantSetting', 'This value is only found in the delegate container.');

$bucket = new \CodeJet\Bucket\Bucket();
$bucket->setDelegateContainer($delegateLookupContainer);
$bucket->add(
    'service-id',
    function (\Interop\Container\ContainerInterface $container) {
        // The factory Closure is passed the delegate lookup container.
        return $container->get('importantSetting');
    }
);

var_dump($bucket->get('service-id')); // string(51) "This value is only found in the delegate container."
var_dump($bucket->has('importantSetting')); // bool(false)

测试

$ composer test

安全

如果您发现任何安全问题,请通过电子邮件 josh@findsomehelp.com 而不是使用问题跟踪器。

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件