Pimple IoC
Class resolver for the Pimple container., (*1)
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., (*2)
Installation
Add the IoC container to your composer.json
using the command line., (*3)
composer require jonsa/pimple-ioc
Usage
The class resolver is registered in Pimple as a ServiceProvider
, (*4)
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., (*5)
$instance = $container['make']('Acme\MyClass');
and the bind
key to bind a definition to a concrete implementation, (*6)
$container['bind']('Acme\MyInterface', 'Acme\MyClass');
Resolved recursively
Class dependencies are resolved recursively., (*7)
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., (*8)
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., (*9)
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, (*10)
$container[ServiceProvider::EVENT_DISPATCHER] = 'my dispatcher key';
Configuration
The ServiceProvider has three configuration parameters., (*11)
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., (*12)
$makeMethod
is used to define which key on the container to be used as the make method., (*13)
$bindMethod
is used to define which key on the container to be used as the bind method., (*14)