Minimal Psr-11 container implementation handling service provider interop.
Require php >= 7.0
Installation composer require ellipse/container
Run tests ./vendor/bin/kahlan
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');