furious/container

1.0.2 2020-07-15 07:08 UTC

This package is auto-updated.

Last update: 2024-11-12 01:32:56 UTC


README

Very simple PSR-11 DI Container for PHP 7.4+

Latest Version Build Status Code Intelligence Status Quality Score Maintainability Total Downloads Monthly Downloads Software License

Install:

composer require furious/container

Use:

$container = new Container();

// get

$container->get('key');

// has

$container->has('key');

// put integers
$container->set(1, 100);
$container->put(2, 200); // use set or put

// put string
$container->set('some string', 'some value');

// put array
$container->set('config', [
    'debug' => true,
    'some key' => 'some value'
]);

// put clojure
// return SomeClass instance
$container->set(SomeClass::class, function (ContainerInterface $container) {
    return new SomeClass();
});

// put object
$container->set(SomeAnotherClass::class, new SomeAnotherClass('value', 'value'));

// autowiring

class A
{
    public function __construct(B $b)
    {
    }
}

class B
{

}

$container->get(A::class);

// Factories

class Factory
{
    public function __invoke(ContainerInterface $container)
    {
        return ExampleClass(
            $container->get('key'),
            $container->get('key'),
            $container->get('key'),
            $container->get('key')
        );
    }
}

$container->set(ExampleClass::class, function (ContainerInterface $container) {
    return (new Factory())($container);
});

// Interfaces

interface SomeInterface
{

}

class SomeClass implements SomeInterface
{
    
}

$container->get(SomeInterface::class, function (ContainerInterface $container) {
    return $container->get(SomeClass::class);
});