codejet/bucket

A light container-interop compatible DI Container object.

1.0.0 2017-08-02 18:12 UTC

This package is auto-updated.

Last update: 2024-11-29 04:57:45 UTC


README

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

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

A convenient container-interop compatible DI container object.

Easy to use, easy to understand and inherently easy to extend.

Install

Via Composer

$ composer require codejet/bucket

Usage

Creating a bucket

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

Adding Values

Using a string as the key, pass any value that is not a \Closure and it will be stored as-is.

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

Adding Factories

Using a string as the key and passing a \Closure as the value will store a factory. The Closure may accept \Interop\Container\ContainerInterface as it's only argument. The bucket will pass itself (or the assigned delegate) in to the factory when $bucket->get('service-id') is called the first time and it will store the returned data as the value for subsequent requests for the same id.

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

Retrieving Items

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) { }

Delegate lookup feature

The container-interop delegate lookup standard provides a means through which a container may use an alternate container for dependency injection purposes.

$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)

Testing

$ composer test

Security

If you discover any security related issues, please email josh@findsomehelp.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.