naylonkessler/con-boss

A light context manager for DI and IoC

v0.0.0 2017-02-17 09:16 UTC

README

SensioLabsInsight Build Status

ConBoss is a light context manager (container) for using with dependency injection and inversion of control concepts. It is intended to be simple and light but still powerful.

Installation

Just require the package using composer.

composer require naylonkessler/con-boss

Using the package

Import and create a container:

<?php

use ConBoss\Container;

$container = new Container();

After bind some dependencies:

// Bind a name to a class FQN
$container->bind('name', Some\Class::class);

// Bind an interface FQN to a class FQN
$container->bind(Some\Interface::class, Some\Class::class);

// Bind an interface FQN to a factory closure
$container->bind(Some\Interface::class, function ($container) {
    return new \Some\Class();
});

// Bind a variable
$container->bind('$varName', 'Any content');

// Bind an interface FQN to a shared class FQN
$container->share(Shared\Interface::class, Some\Shared\Class::class);

And finally request some bind from container:

// Get some bind name from container
$some = $container->get('name');

// Get some variable from container
$var = $container->get('$varName');

// Get multiple values from container
list($name, $var) = $container->get(['name', '$varName']);

You can also unbind something from container and check if some binding exists:

// Unbind a name from container
$container->unbind('name');

// Check if some bind exists
$exists = $container->has('name');

Except for Container::get() and Container::has() methods that returns especific values you can take advantage of chained calls if you prefer:

// Chained calls
$container->share('some-shared', Some\Shared::class)
    ->bind('some-bind', Some\Bind::class)
    ->bind('$someVar', 10);