2017 © Pedro PelĂĄez
 

library pimple-ioc

Resolve classes out of a Pimple container

image

jonsa/pimple-ioc

Resolve classes out of a Pimple container

  • Wednesday, December 9, 2015
  • by jonsa
  • Repository
  • 1 Watchers
  • 0 Stars
  • 31 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

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)

The Versions

09/12 2015

dev-master

9999999-dev

Resolve classes out of a Pimple container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonas Sandström

ioc pimple

07/10 2015

v1.3.0

1.3.0.0

Resolve classes out of a Pimple container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonas Sandström

ioc pimple

28/09 2015

v1.2.0

1.2.0.0

Resolve classes out of a Pimple container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonas Sandström

ioc pimple

28/09 2015

v1.1.0

1.1.0.0

Resolve classes out of a Pimple container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonas Sandström

ioc pimple

28/09 2015

v1.0.1

1.0.1.0

Resolve classes out of a Pimple container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonas Sandström

ioc pimple

27/09 2015

v1.0.0

1.0.0.0

Resolve classes out of a Pimple container

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonas Sandström

ioc pimple