Bucket
.=======.
/ \
/ _____ \
/.-'" "`-.\
[( )]
|`-.._____..-'|
| |
| |
| bucket |
\ /
`-.._____..-'
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
A convenient container-interop compatible DI container object., (*2)
Easy to use, easy to understand and inherently easy to extend., (*3)
Install
Via Composer, (*4)
``` bash
$ composer require codejet/bucket, (*5)
## Usage
### Creating a bucket
``` php
$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., (*6)
``` php
$bucket->add('value-id', 'The value of the value.');, (*7)
### 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.
``` php
$bucket->add(
'service-id',
function (\Interop\Container\ContainerInterface $bucket) {
return new \stdClass();
}
);
Retrieving Items
``` php
var_dump($bucket->has('value-id')); // bool(true)
var_dump($bucket->get('value-id')); // string(23) "The value of the value.", (*8)
var_dump($bucket->has('service-id')); // bool(true)
var_dump($bucket->get('service-id')); // class stdClass#4 (0) { }, (*9)
### Delegate lookup feature
The [container-interop delegate lookup standard][delegate-lookup-std-link] provides a means
through which a container may use an alternate container for dependency injection purposes.
```php
$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
bash
$ composer test
, (*10)
Security
If you discover any security related issues, please email josh@findsomehelp.com instead of using the issue tracker., (*11)
License
The MIT License (MIT). Please see License File for more information., (*12)