ellipse/container

This package is abandoned and no longer maintained. The author suggests using the quanta/container package instead.

Minimal Psr-11 container implementation handling service provider interop

0.4.10 2018-03-19 16:32 UTC

This package is auto-updated.

Last update: 2019-12-20 15:38:53 UTC


README

Minimal Psr-11 container implementation handling service provider interop.

Require php >= 7.0

Installation composer require ellipse/container

Run tests ./vendor/bin/kahlan

Getting started

The Ellipse\Container class constructor takes an array of Interop\Container\ServiceProviderInterface implementations. This is the only way of registering service providers and service definitions into the container.

An Ellipse\Container\Exceptions\ServiceProviderTypeException is thrown when any element of the array passed to the Container class constructor is not an implementation of ServiceProviderInterface.

<?php

namespace App;

use Interop\Container\ServiceProviderInterface;

class ServiceProviderA implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            'id' => function ($container) {

                return 'abc';

            },
        ]
    }

    public function getExtensions()
    {
        return [
            //
        ]
    }
}
<?php

namespace App;

use Interop\Container\ServiceProviderInterface;

class ServiceProviderB implements ServiceProviderInterface
{
    public function getFactories()
    {
        return [
            //
        ]
    }

    public function getExtensions()
    {
        return [
            'id' => function ($container, string $previous) {

                return $previous . 'def';

            },
        ]
    }
}
<?php

namespace App;

use Ellipse\Container;

// Get a container with a list of service providers.
$container = new Container([
    new ServiceProviderA,
    new ServiceProviderB,
]);

// Return true.
$container->has('id');

// Return 'abcdef'.
$container->get('id');