jonsa/pimple-ioc

Resolve classes out of a Pimple container

v1.3.0 2015-10-07 20:54 UTC

This package is not auto-updated.

Last update: 2024-05-11 16:45:21 UTC


README

Class resolver for the Pimple container.

This project is heavily inspired by how Laravel resolve it's classes out of their IoC container. In fact most of the code is taken directly from their Container class.

Installation

Add the IoC container to your composer.json using the command line.

composer require jonsa/pimple-ioc

Usage

The class resolver is registered in Pimple as a ServiceProvider

use Jonsa\PimpleResolver\ServiceProvider;
use Pimple\Container;

$container = new Container();
$container->register(new ServiceProvider());

This will register the make key on the Container which resolves the requested class.

$instance = $container['make']('Acme\MyClass');

and the bind key to bind a definition to a concrete implementation

$container['bind']('Acme\MyInterface', 'Acme\MyClass');

Resolved recursively

Class dependencies are resolved recursively.

interface FooContract {}

class Foo implements FooContract {};

class Bar {
    public function __construct(FooContract $foo) {}
}

class Baz {
    public function __construct(Bar $bar) {}
}

$container['bind']('FooContract', 'FooClass');
$baz = $container['make']('Baz');

Define constructor parameters

To override a class parameter use the parameter array on the resolver method.

class Log {
    public function __construct(Psr\Log\LoggerInterface $logger, $level = Psr\Log\LogLevel::WARNING)
    {
        ...
    }
}

$container['make']('Log', array(
    'level' => Psr\Log\LogLevel::DEBUG
));

Inject into the resolver workflow

To customize a resolved class before it is returned from the resolver, simply listen to the CLASS_RESOLVED event.

use Jonsa\PimpleResolver\ServiceProvider;
use Jonsa\PimpleResolver\Events;
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher;
$container[ServiceProvider::EVENT_DISPATCHER] = function () use ($dispatcher) {
    return $dispatcher;
});

$dispatcher->addListener(Events::CLASS_RESOLVED, function (ClassResolvedEvent $event) {
    $object = $event->getResolvedObject();
    ...
});

Alternatively the EVENT_DISPATCHER key can be populated with a string which in turn returns an event dispatcher from the container

$container[ServiceProvider::EVENT_DISPATCHER] = 'my dispatcher key';

Configuration

The ServiceProvider has three configuration parameters.

class ServiceProvider implements ServiceProviderInterface {
    public function __construct($bindContainerInstance = true, $makeMethod = 'make', $bindMethod = 'bind')
    {
        ...
    }
}

$bindContainerInstance tells the ServiceProvider whether to bind the container instance to the 'Pimple\Container' key. If the container is extended, that class name will also be bound to the container instance.

$makeMethod is used to define which key on the container to be used as the make method.

$bindMethod is used to define which key on the container to be used as the bind method.