PHPackages                             mouf/prefixer-container - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mouf/prefixer-container

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

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.

1.0.x-dev(11y ago)313MITPHP

Since Feb 1Pushed 11y ago16 watchersCompare

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

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

Prefixer-Container
==================

[](#prefixer-container)

[![Latest Stable Version](https://camo.githubusercontent.com/8d3646349f36c38c2de850673bab83ec70aa025ccd9d1b99ee71b284563d34f7/68747470733a2f2f706f7365722e707567782e6f72672f6d6f75662f70726566697865722d636f6e7461696e65722f762f737461626c652e737667)](https://packagist.org/packages/mouf/prefixer-container)[![Latest Unstable Version](https://camo.githubusercontent.com/49d8dbeebd377234b2a37426c2361bc1507508fcc3ea5d5f61abda9e5babead6/68747470733a2f2f706f7365722e707567782e6f72672f6d6f75662f70726566697865722d636f6e7461696e65722f762f756e737461626c652e737667)](https://packagist.org/packages/mouf/prefixer-container)[![License](https://camo.githubusercontent.com/42b0e7eeadcde47e43735f929f7d11bbf040d5947ae99c530250bc83065a0e5d/68747470733a2f2f706f7365722e707567782e6f72672f6d6f75662f70726566697865722d636f6e7461696e65722f6c6963656e73652e737667)](https://packagist.org/packages/mouf/prefixer-container)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b62bc8e93de97115ad577eb0d59c319e0631b1d489da05f1978aa54029cd6e7f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f746865636f64696e676d616368696e652f70726566697865722d636f6e7461696e65722f6261646765732f7175616c6974792d73636f72652e706e673f623d312e30)](https://scrutinizer-ci.com/g/thecodingmachine/prefixer-container/?branch=1.0)[![SensioLabsInsight](https://camo.githubusercontent.com/04daffdf138fcdd4c2e491883670d7f42485b6917cddf7d7c8a2796d65921e96/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f65366366633462342d626336642d346564632d386530312d6163303533353263333638392f6d696e692e706e67)](https://insight.sensiolabs.com/projects/e6cfc4b4-bc6d-4edc-8e01-ac05352c3689)[![Build Status](https://camo.githubusercontent.com/15aee28fa009416799820410a927b7c0fb92824c5678775d841a7c8b9686a723/68747470733a2f2f7472617669732d63692e6f72672f746865636f64696e676d616368696e652f70726566697865722d636f6e7461696e65722e7376673f6272616e63683d312e30)](https://travis-ci.org/thecodingmachine/prefixer-container)[![Coverage Status](https://camo.githubusercontent.com/d396a4d0065648c49b8ac54bf630949c7f336baca5a5b1cd52ec64b52db3bc50/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f746865636f64696e676d616368696e652f70726566697865722d636f6e7461696e65722f62616467652e7376673f6272616e63683d312e30)](https://coveralls.io/r/thecodingmachine/prefixer-container?branch=1.0)

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](https://github.com/container-interop/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
------------

[](#installation)

Before using `PrefixerContainer` in your project, add it to your `composer.json` file:

```
$ ./composer.phar require mouf/prefixer-container ~1.0

```

Usage
-----

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

[![The issue](doc/the_issue.png?raw=true)](doc/the_issue.png?raw=true)

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.

[![Solution 1](doc/solution1.png?raw=true)](doc/solution1.png?raw=true)

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

[](#working-with-delegate-lookup-containers)

If the container you are wrapping is implementing the [delegate lookup feature](https://github.com/container-interop/container-interop/blob/master/docs/Delegate-lookup.md)(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`:

[![A container with a dependency](doc/container_with_dependency.png?raw=true)](doc/container_with_dependency.png?raw=true)

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.

[![Delegate lookup issue](doc/delegate_lookup_issue.png?raw=true)](doc/delegate_lookup_issue.png?raw=true)

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.

[![Delegate lookup solved](doc/delegate_lookup_solved.png?raw=true)](doc/delegate_lookup_solved.png?raw=true)

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

[](#why-the-need-for-this-package)

This package is part of a long-term effort to bring [interoperability between DI containers](https://github.com/container-interop/container-interop). 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...)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4124d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

---

Top Contributors

[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (15 commits)")

---

Tags

container-interopdependency-injectiondi

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mouf-prefixer-container/health.svg)

```
[![Health](https://phpackages.com/badges/mouf-prefixer-container/health.svg)](https://phpackages.com/packages/mouf-prefixer-container)
```

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)[level-2/dice

A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.

437730.3k17](/packages/level-2-dice)[mouf/pimple-interop

This project is a very simple extension to the Pimple microframework. It adds to Pimple compatibility with the container-interop APIs.

102.4M2](/packages/mouf-pimple-interop)[mouf/picotainer

This package contains a really minimalist dependency injection container compatible with container-interop.

16189.8k11](/packages/mouf-picotainer)[x-wp/di

The dependency injection container for WordPress

301.1k10](/packages/x-wp-di)[michaels/data-manager

Simple data manager for nested data, dot notation array access, extendability, and container interoperability.

121.9k2](/packages/michaels-data-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
