mouf/root-container

There is no license information available for the latest version (1.0.x-dev) of this package.

This package contains a PHP DI container detector and aggregator. The idea is to have potentially one DI container per composer package and to aggregate all those containers into a 'root' container.

1.0.x-dev 2015-01-16 15:54 UTC

This package is auto-updated.

Last update: 2024-12-15 05:01:22 UTC


README

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 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:

$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.

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.