{"id":18929532,"url":"https://github.com/thecodingmachine/prefixer-container","last_synced_at":"2025-07-11T03:37:41.178Z","repository":{"id":26664080,"uuid":"30120517","full_name":"thecodingmachine/prefixer-container","owner":"thecodingmachine","description":"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.","archived":false,"fork":false,"pushed_at":"2015-02-01T14:43:50.000Z","size":428,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":17,"default_branch":"1.0","last_synced_at":"2025-07-10T11:11:29.305Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-31T17:50:37.000Z","updated_at":"2020-07-22T09:06:15.000Z","dependencies_parsed_at":"2022-07-25T16:00:10.955Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/prefixer-container","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thecodingmachine/prefixer-container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fprefixer-container","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fprefixer-container/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fprefixer-container/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fprefixer-container/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/prefixer-container/tar.gz/refs/heads/1.0","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fprefixer-container/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264722223,"owners_count":23654076,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:33:25.561Z","updated_at":"2025-07-11T03:37:41.158Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Prefixer-Container\n==================\n[![Latest Stable Version](https://poser.pugx.org/mouf/prefixer-container/v/stable.svg)](https://packagist.org/packages/mouf/prefixer-container)\n[![Latest Unstable Version](https://poser.pugx.org/mouf/prefixer-container/v/unstable.svg)](https://packagist.org/packages/mouf/prefixer-container)\n[![License](https://poser.pugx.org/mouf/prefixer-container/license.svg)](https://packagist.org/packages/mouf/prefixer-container)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/prefixer-container/badges/quality-score.png?b=1.0)](https://scrutinizer-ci.com/g/thecodingmachine/prefixer-container/?branch=1.0)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/e6cfc4b4-bc6d-4edc-8e01-ac05352c3689/mini.png)](https://insight.sensiolabs.com/projects/e6cfc4b4-bc6d-4edc-8e01-ac05352c3689)\n[![Build Status](https://travis-ci.org/thecodingmachine/prefixer-container.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/prefixer-container)\n[![Coverage Status](https://coveralls.io/repos/thecodingmachine/prefixer-container/badge.svg?branch=1.0)](https://coveralls.io/r/thecodingmachine/prefixer-container?branch=1.0)\n\nThis package contains a really minimalist dependency injection container that can be used to **prefix** all identifiers\nin a container. Prefixer-container is compatible with [container-interop](https://github.com/container-interop/container-interop)\nand is meant to be used in conjunction with other containers. By itself, Prefix-container does not store any entry. It can only be used\nto **wrap an existing container**.\n\nYou can use `PrefixerContainer` to put all identifiers of a container in a **namespace**.\n\nInstallation\n------------\n\nBefore using `PrefixerContainer` in your project, add it to your `composer.json` file:\n\n```\n$ ./composer.phar require mouf/prefixer-container ~1.0\n```\n\nUsage\n-----\n\nImagine you have 2 containers living side-by-side, and a composite container (we will call it the \"root\" container)\nis joining them. Now, both containers declare a same instance named \"dbConnection\". \n\nIf you want to keep access to both instances through the root container, you have a problem, because you have\na naming collision. Of course, you can rename one of those instances, but if the containers are provided by\nthird party libraries, that might not be possible.\n\n![The issue](doc/the_issue.png?raw=true)\n\nSo what you need to do is to rename the instances of one of the containers so that there is no more conflict.\nThis is where the `PrefixerContainer` kicks in.\n\n![Solution 1](doc/solution1.png?raw=true)\n\nBy wrapping your containers inside a `PrefixerContainer`, you can change the name of the instances to the outside\nworld.\n\nHere is a sample code demonstrating the code above:\n\n```php\nuse Mouf\\PrefixerContainer\\PrefixerContainer;\nuse Acclimate\\Container\\CompositeContainer;\nuse Interop\\Container\\ContainerInterface;\nuse Mouf\\Picotainer\\Picotainer;\n\n\n$rootContainer = new CompositeContainer();\n\n// We use Picotainer, a minimalistic container for this demo.\n$containerA = new Picotainer([\n    \"dbConnection\" =\u003e function () { return new DbConnection(...); },\n]);\n\n$containerB = new Picotainer([\n    \"dbConnection\" =\u003e function () { return new OtherDbConnection(...); },\n]);\n\n\n$rootContainer-\u003eaddContainer(new PrefixerContainer($containerA, 'A.')));\n$rootContainer-\u003eaddContainer(new PrefixerContainer($containerB, 'B.')));\n\n// Get 'dbConnection' from container A:\n$dbConnectionA = $rootContainer-\u003eget('A.dbConnection');\n\n// Get 'dbConnection' from container B:\n$dbConnectionB = $rootContainer-\u003eget('B.dbConnection');\n\n// This will throw a NotFoundException:\n$willFail = $rootContainer-\u003eget('dbConnection');\n```\n\nWorking with delegate lookup containers\n---------------------------------------\n\nIf the container you are wrapping is implementing the [delegate lookup feature](https://github.com/container-interop/container-interop/blob/master/docs/Delegate-lookup.md) \n(it should!), you will face another problem.\n\nWhen you use the delegate lookup feature, the dependencies are fetched from the root container. Now, the name of the\ndependencies has changed because of the `PrefixerContainer`!\n\nJust image a container with a service that uses the `dbConnection`:\n\n![A container with a dependency](doc/container_with_dependency.png?raw=true)\n\nWhat if we wrap this container in a `PrefixerContainer`? If we query the `A.myService` entry (1), the container will\ndelegate to the rootContainer the lookup of the `dbConnection` entry. Now, this is a problem, because it should\nquery the `A.dbConnection` entry.\n\n![Delegate lookup issue](doc/delegate_lookup_issue.png?raw=true)\n\nIn order to fix this, the prefixer-container comes with a `DelegateLookupUnprefixerContainer` class. This is a wrapper \nyou will use to wrap the delegate lookup container. When the `get` method of the wrapper is called, it will first try\nto get the instance with the prefix, and if it fails, it will try to get the instance without the prefix.\n\n![Delegate lookup solved](doc/delegate_lookup_solved.png?raw=true)\n\nIf we query the `A.myService` entry (1), , the container will delegate to the rootContainer the lookup of the `dbConnection` entry (2).\nThis goes through the `DelegateLookupUnprefixerContainer` first that will add the \"A.\" prefix (3). The lookup goes through the\nroot container again, then the prefixer container that strips the \"A.\" and finally, the dependency `dbConnection` is solved. *Job's done!*\n\nHere is a sample code demonstrating the code above:\n\n```php\nuse Mouf\\PrefixerContainer\\PrefixerContainer;\nuse Acclimate\\Container\\CompositeContainer;\nuse Interop\\Container\\ContainerInterface;\nuse Mouf\\Picotainer\\Picotainer;\n\n$prefix = \"A.\";\n\n$rootContainer = new CompositeContainer();\n\n// We use Picotainer, a minimalistic container for this demo.\n$container = new Picotainer([\n    \"dbConnection\" =\u003e function () { return new DbConnection(...); },\n    // The myService service requires the 'dbConnection'\n    \"myService\" =\u003e function (ContainerInterface $c) { return new MyService($c-\u003eget('dbConnection')); },\n], new DelegateLookupUnprefixerContainer($rootContainer, $prefix));\n\n// In the root container, we add a prefixed version of the container\n$rootContainer-\u003eaddContainer(new PrefixerContainer($container, $prefix));\n\n$service = $rootContainer-\u003eget('myService');\n```\n\nWhy the need for this package?\n------------------------------\n\nThis package is part of a long-term effort to bring [interoperability between DI containers](https://github.com/container-interop/container-interop). \nThe ultimate goal is to make sure that multiple containers can communicate together by sharing entries (one container might \nuse an entry from another container, etc...)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fprefixer-container","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fprefixer-container","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fprefixer-container/lists"}