About RootContainer
RootContainer is a facade for a composite container that aggregates all DI containers detected in the application., (*1)
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., (*2)
Compared to the classical way of thinking about a web application, this is a paradigm shift., (*3)
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., (*4)
Using this approach, each package provides its own DI container that contains instances. DI containers are added
to a global container that is queried., (*5)
About this package
Detection of other containers is done using the Container-Installer
component., (*6)
This package is using detected packages and aggregate those into a unique composite container. This container
is exposed as a facade., (*7)
So basically, using RootContainer, you can access ANY instance of ANY container of your application using:, (*8)
$instance = RootContainer::get("instance_name");
In the background, RootContainer
will load the containers.php
file generated by
Container-Installer and instanciate all the containers
of the application., (*9)
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)., (*10)
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., (*11)
That being said, it's always useful to be able to access an instance quickly for testing purposes., (*12)