2017 © Pedro Peláez
 

library container

Thruster Container Component

image

thruster/container

Thruster Container Component

  • Tuesday, May 31, 2016
  • by gcds
  • Repository
  • 1 Watchers
  • 0 Stars
  • 236 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 16 % Grown

The README.md

Container Component

[Latest Version] (https://github.com/ThrusterIO/container/releases) [Software License] (LICENSE) [Build Status] (https://travis-ci.org/ThrusterIO/container) [Code Coverage] (https://scrutinizer-ci.com/g/ThrusterIO/container) [Quality Score] (https://scrutinizer-ci.com/g/ThrusterIO/container) [Total Downloads] (https://packagist.org/packages/thruster/container), (*1)

[Email] (mailto:team@thruster.io), (*2)

The Thruster Container Component. Container is small self contained dependency injection container implementing ContainerInterface interface., (*3)

Install

Via Composer, (*4)

$ composer require thruster/container

Usage

Just create instance of Container:, (*5)

<?php

use Thruster\Component\Container;

$container = new Container();

Also you can pass preset array of values to constructor., (*6)

<?php

use Thruster\Component\Container;

$values = [
    'render.engine' => function ($container) {
        return new RenderEngine($container->get('translation.engine'));
    },
    'translation.engine' => function () {
        return new TranslationEngine();
    }
];

$container = new Container($values);

Container has a bunch of simple named functions to use container: has, set, get, remove:, (*7)

<?php

use Thruster\Component\Container;

$container = new Container();

$container->has('render.engine'); // = false
$container->set('render.engine', function ($container) {
        return new RenderEngine($container->get('translation.engine'));
});

$container->has('render.engine'); // = true

$renderEngine = $container->get('render.engine');

$container->remove('render.engine');
$container->has('render.engine'); // = false

Container also support \ArrayAccess, (*8)

<?php

use Thruster\Component\Container;

$container = new Container();

isset($container['render.engine']); // = false
$container['render.engine'] = function ($container) {
        return new RenderEngine($container->get('translation.engine'));
};

isset($container['render.engine']); // = true

$renderEngine = $container['render.engine'];

unset($container['render.engine']);
isset($container['render.engine']); // = false

By default Container always returns the same instance of identifier, but you can create a factory definition which will return always new instance of identifier., (*9)

<?php

use Thruster\Component\Container;

$container = new Container();

$i = 1;

$value = function () use ($i) {
    return $i++;
}

$factoryValue = function () use (&$i) {
    return $i++;
}

$container->set('normal', $value);

$container->get('normal'); // = 1
$container->get('normal'); // = 1

$container->set('factory', $factoryValue);

$container->get('factory'); // = 1
$container->get('factory'); // = 2

Container provides a way to extend container with ContainerProviderInterface, (*10)

<?php

use Thruster\Component\Container;
use Thruster\Component\ContainerProviderInterface;

$container = new Container();

$provider = new class implements ContainerProviderInterface {
    public function register(Container $container)
    {
        $container->set(....);
    }
};

$container->addProvider($provider);

Errors

  • Container throws NotFoundException if identifier is not found in container.
  • Container throws IdentifierFrozenException when trying to set definition for already frozen identifier. (Frozen means when value was already used once)

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*11)

License

Please see License File for more information., (*12)

The Versions

31/05 2016

dev-master

9999999-dev https://thruster.io

Thruster Container Component

  Sources   Download

MIT

The Requires

 

The Development Requires

container thruster

31/05 2016

1.1.0

1.1.0.0 https://thruster.io

Thruster Container Component

  Sources   Download

MIT

The Requires

 

The Development Requires

container thruster

09/05 2016

1.0.0

1.0.0.0 https://thruster.io

Thruster Container Component

  Sources   Download

MIT

The Requires

 

The Development Requires

container thruster