PHPackages                             mouf/container-installer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mouf/container-installer

ActiveComposer-plugin[Framework](/categories/framework)

mouf/container-installer
========================

This package contains a PHP dependency injection 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(11y ago)1283MITPHPPHP &gt;=5.4

Since Feb 6Pushed 11y ago17 watchersCompare

[ Source](https://github.com/thecodingmachine/container-installer)[ Packagist](https://packagist.org/packages/mouf/container-installer)[ Docs](http://mouf-php.com)[ RSS](/packages/mouf-container-installer/feed)WikiDiscussions 1.0 Synced 1mo ago

READMEChangelogDependencies (2)Versions (1)Used By (0)

About Container-Installer
=========================

[](#about-container-installer)

This project is a test project developed as a proof of concept while working on the [ContainerInterop](https://github.com/container-interop/container-interop/) project.

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

[](#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
------------------

[](#about-this-package)

The goal of this package is simply to provide an easy way for application developers to detect DI containers that might be declared in Composer packages they use.

This project adds an additional step to Composer "install", just after the Composer dumps the autoloader. The Container-Installer will scan all packages *composer.json* files and will look for a section like:

```
{
	"extra": {
		"container-interop": {
			"container-factory": "My\\ContainerFactory::getContainer"
		}
	}
}
```

This section actually declares container factories that can be bundled in the package.

The "container-factory" parameter must point to a function or a static method that returns the container.

Here is a sample implementation:

```
class ContainerFactory {
	private static $container;

	/**
	 * This method is returning a configured container
	 *
	 * @param ContainerInterface $rootContainer
	 * @return ContainerInterface
	 */
	public static function getContainer(ContainerInterface $rootContainer) {
		if (!$this->container) {
			// Delegate dependencies fetching to the root container.
			$this->container = new Picotainer([
				"hello" => function(ContainerInterface $container) {
					return array('hello' => $container->get('world'));
				}
			], $rootContainer);
		}
		return $this->container;
	}
}
```

A quick note about this code: we are providing a [Picotainer container](http://mouf-php.com/packages/mouf/picotainer/README.md). Picotainer is a minimalistic container fully compativle with the [ContainerInterop](https://github.com/container-interop/container-interop/) project.

**Important**: the factory takes one compulsory parameter: the `$rootContainer`. If some entries in your container are containing *external dependencies* (dependencies that are not part of the container), then your container needs to be able to delegate dependencies fetching to the $rootContainer. For instance, `PimpleInterop` can delegate dependencies fetching if you pass another container as the first argument of the constructor.

Note: your package does not have to require the `mouf/container-installer` package. This is sweet because if other container aggregators follow the same convention (referencing factory code in `composer.json` extra section), there can easily be many different implementations of a root-container (maybe one per framework).

How to use a root container in your project?
============================================

[](#how-to-use-a-root-container-in-your-project)

This package will simply create a `containers.php` file at the root of your project. This `containers.php` file will contain a list of containers in this form:

```
