mouf / prefixer-container
This package contains a really minimalist dependency injection container that acts as a proxy in front of a target container. Its goal is to prefix all instances names in the target container.
Requires
Requires (Dev)
- acclimate/container: ~1.0
- mouf/picotainer: ~1.0
- phpunit/phpunit: ~3.7
- satooshi/php-coveralls: dev-master
This package is auto-updated.
Last update: 2024-11-15 05:48:05 UTC
README
This package contains a really minimalist dependency injection container that can be used to prefix all identifiers in a container. Prefixer-container is compatible with container-interop and is meant to be used in conjunction with other containers. By itself, Prefix-container does not store any entry. It can only be used to wrap an existing container.
You can use PrefixerContainer
to put all identifiers of a container in a namespace.
Installation
Before using PrefixerContainer
in your project, add it to your composer.json
file:
$ ./composer.phar require mouf/prefixer-container ~1.0
Usage
Imagine you have 2 containers living side-by-side, and a composite container (we will call it the "root" container) is joining them. Now, both containers declare a same instance named "dbConnection".
If you want to keep access to both instances through the root container, you have a problem, because you have a naming collision. Of course, you can rename one of those instances, but if the containers are provided by third party libraries, that might not be possible.
So what you need to do is to rename the instances of one of the containers so that there is no more conflict.
This is where the PrefixerContainer
kicks in.
By wrapping your containers inside a PrefixerContainer
, you can change the name of the instances to the outside
world.
Here is a sample code demonstrating the code above:
use Mouf\PrefixerContainer\PrefixerContainer; use Acclimate\Container\CompositeContainer; use Interop\Container\ContainerInterface; use Mouf\Picotainer\Picotainer; $rootContainer = new CompositeContainer(); // We use Picotainer, a minimalistic container for this demo. $containerA = new Picotainer([ "dbConnection" => function () { return new DbConnection(...); }, ]); $containerB = new Picotainer([ "dbConnection" => function () { return new OtherDbConnection(...); }, ]); $rootContainer->addContainer(new PrefixerContainer($containerA, 'A.'))); $rootContainer->addContainer(new PrefixerContainer($containerB, 'B.'))); // Get 'dbConnection' from container A: $dbConnectionA = $rootContainer->get('A.dbConnection'); // Get 'dbConnection' from container B: $dbConnectionB = $rootContainer->get('B.dbConnection'); // This will throw a NotFoundException: $willFail = $rootContainer->get('dbConnection');
Working with delegate lookup containers
If the container you are wrapping is implementing the delegate lookup feature (it should!), you will face another problem.
When you use the delegate lookup feature, the dependencies are fetched from the root container. Now, the name of the
dependencies has changed because of the PrefixerContainer
!
Just image a container with a service that uses the dbConnection
:
What if we wrap this container in a PrefixerContainer
? If we query the A.myService
entry (1), the container will
delegate to the rootContainer the lookup of the dbConnection
entry. Now, this is a problem, because it should
query the A.dbConnection
entry.
In order to fix this, the prefixer-container comes with a DelegateLookupUnprefixerContainer
class. This is a wrapper
you will use to wrap the delegate lookup container. When the get
method of the wrapper is called, it will first try
to get the instance with the prefix, and if it fails, it will try to get the instance without the prefix.
If we query the A.myService
entry (1), , the container will delegate to the rootContainer the lookup of the dbConnection
entry (2).
This goes through the DelegateLookupUnprefixerContainer
first that will add the "A." prefix (3). The lookup goes through the
root container again, then the prefixer container that strips the "A." and finally, the dependency dbConnection
is solved. Job's done!
Here is a sample code demonstrating the code above:
use Mouf\PrefixerContainer\PrefixerContainer; use Acclimate\Container\CompositeContainer; use Interop\Container\ContainerInterface; use Mouf\Picotainer\Picotainer; $prefix = "A."; $rootContainer = new CompositeContainer(); // We use Picotainer, a minimalistic container for this demo. $container = new Picotainer([ "dbConnection" => function () { return new DbConnection(...); }, // The myService service requires the 'dbConnection' "myService" => function (ContainerInterface $c) { return new MyService($c->get('dbConnection')); }, ], new DelegateLookupUnprefixerContainer($rootContainer, $prefix)); // In the root container, we add a prefixed version of the container $rootContainer->addContainer(new PrefixerContainer($container, $prefix)); $service = $rootContainer->get('myService');
Why the need for this package?
This package is part of a long-term effort to bring interoperability between DI containers. The ultimate goal is to make sure that multiple containers can communicate together by sharing entries (one container might use an entry from another container, etc...)