An open API service indexing awesome lists of open source software.

https://github.com/thecodingmachine/root-container

This package contains a facade container that aggregates all containers that have been detected thanks to the mouf/container-installer package.
https://github.com/thecodingmachine/root-container

Last synced: 8 months ago
JSON representation

This package contains a facade container that aggregates all containers that have been detected thanks to the mouf/container-installer package.

Awesome Lists containing this project

README

          

About RootContainer
===================

RootContainer is a *facade* for a composite container that aggregates all DI containers detected in the application.

The big picture
---------------

The ultimate goal is to allow the application developer to easily create a "root container",
that can automatically detect and add containers contained in other packages into a global
composite container that can be used by the application.

Compared to the classical way of thinking about a web application, this is a paradigm shift.

**In a "classical" application**, packages added to the application may add new instances to the main and only DI container.
This is what SF2 bundles, ZF2 modules or Mouf2 packages are doing.

**Using this approach**, each package provides its own DI container that contains instances. DI containers are added
to a global container that is queried.

About this package
------------------

Detection of other containers is done using the [Container-Installer](http://mouf-php.com/packages/mouf/container-installer)
component.

This package is using detected packages and aggregate those into a unique composite container. This container
is exposed as a facade.

So basically, using **RootContainer**, you can access ANY instance of ANY container of your application using:

```php
$instance = RootContainer::get("instance_name");
```

In the background, `RootContainer` will load the `containers.php` file generated by
[Container-Installer](http://mouf-php.com/packages/mouf/container-installer) and instanciate all the containers
of the application.

A word of caution
-----------------

Having an easy to use facade for containers does not allow you to use it all over the place :)
Otherwise, you would be using the RootContainer as a service locator, which is considered by most people to
be a bad practice (tip: the author of RootContainer thinks it is a bad practice too).

So in a perfect world, the RootContainer should be used once or twice in your application, maybe by the
MVC framework you are using, to fetch the controller, and that's it.

That being said, it's always useful to be able to access an instance quickly for testing purposes.