{"id":18929754,"url":"https://github.com/thecodingmachine/common-container-definitions","last_synced_at":"2026-03-16T17:30:17.153Z","repository":{"id":35885507,"uuid":"40171383","full_name":"thecodingmachine/common-container-definitions","owner":"thecodingmachine","description":"This package contains common classes that implement the interfaces defined in container-interop/compiler-interop","archived":false,"fork":false,"pushed_at":"2015-09-04T09:09:17.000Z","size":188,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":9,"default_branch":"1.0","last_synced_at":"2025-02-16T12:30:25.838Z","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":"mit","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":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-04T08:02:00.000Z","updated_at":"2015-08-04T08:03:09.000Z","dependencies_parsed_at":"2022-08-30T13:11:07.237Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/common-container-definitions","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcommon-container-definitions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcommon-container-definitions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcommon-container-definitions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fcommon-container-definitions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/common-container-definitions/tar.gz/refs/heads/1.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239927825,"owners_count":19719835,"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:34:52.924Z","updated_at":"2026-03-16T17:30:17.108Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/common-container-definitions/badges/quality-score.png?b=1.0)](https://scrutinizer-ci.com/g/thecodingmachine/common-container-definitions/?branch=1.0)\n[![Build Status](https://travis-ci.org/thecodingmachine/common-container-definitions.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/common-container-definitions)\n[![Coverage Status](https://coveralls.io/repos/thecodingmachine/common-container-definitions/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/thecodingmachine/common-container-definitions?branch=1.0)\n\n# Common container definitions for compiler-interop\n\nThis package contains common classes that implement the interfaces defined in [*compiler-interop*](https://github.com/container-interop/compiler-interop/)\n\n## Installation\n\nYou can install this package through Composer:\n\n```json\n{\n    \"require\": {\n        \"mouf/common-container-definitions\": \"dev-master\"\n    }\n}\n```\n\nThe packages adheres to the [SemVer](http://semver.org/) specification, and there will be full backward compatibility\nbetween minor versions.\n\n## Usage\n\nClasses in this package represent **definitions** of entries that can be put in a container.\nThose definitions needs to be passed to a **compiler** that will generate a **container** PHP class.\n\nAll the definitions in this package are implementing the `Interop\\Container\\Compiler\\DefinitionInterface`.\nThis means they can be fed to any compiler compatible with [*compiler-interop*](https://github.com/container-interop/compiler-interop/).\n\n### Creating a typical container entry\n\nThe typical container entry is an instance of a class that is passed some constructor arguments, with a few\nmethod calls (typically setters).\n\n```php\nuse Mouf\\Container\\Definition\\InstanceDefinition;\n\n$instanceDefinition = new InstanceDefinition(\"instanceName\", \"My\\\\Class\");\n$instanceDefinition-\u003eaddConstructorArgument(\"foo\");\n$instanceDefinition-\u003eaddConstructorArgument([\"bar\"]);\n\nreturn $instanceDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **expression**: `new My\\Class(\"foo\", [\"bar\"])`\n- **statements**: *empty*\n- **usedVariables**: *empty*\n\nYou can pass references to other entries in the container by passing another object implementing the `DefinitionInterface`:\n \n```php\nuse Mouf\\Container\\Definition\\InstanceDefinition;\n\n$dependencyDefinition = new InstanceDefinition(\"dependency\", \"My\\\\Dependency\");\n\n$instanceDefinition = new InstanceDefinition(\"instanceName\", \"My\\\\Class\");\n$instanceDefinition-\u003eaddConstructorArgument($dependencyDefinition);\n\nreturn $instanceDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **expression**: `new My\\Class($container-\u003eget(\"dependency\"))`\n- **statements**: *empty*\n- **usedVariables**: *empty*\n\n### Method calls\n\nYou can add method calls on your entry using the \"addMethodCall\" method:\n\n```php\nuse Mouf\\Container\\Definition\\InstanceDefinition;\n\n$instanceDefinition = new InstanceDefinition(\"instanceName\", \"My\\\\Class\");\n$methodCall = $instanceDefinition-\u003eaddMethodCall(\"setFoo\");\n$methodCall-\u003eaddArgument(42);\n\nreturn $instanceDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **statements**: \n  ```php\n  $instanceName = new My\\Class();\n  $instanceName-\u003esetFoo(42);\n  ```\n- **expression**: `$instanceName`\n- **usedVariables**: `[ '$instanceName' ]`\n\n### Setting public properties\n\nYou can add method calls on your entry using the \"setProperty\" method:\n\n```php\nuse Mouf\\Container\\Definition\\InstanceDefinition;\n\n$instanceDefinition = new InstanceDefinition(\"instanceName\", \"My\\\\Class\");\n$instanceDefinition-\u003esetProperty(\"foo\", 42);\n\nreturn $instanceDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **statements**: \n  ```php\n  $instanceName = new My\\Class();\n  $instanceName-\u003efoo = 42;\n  ```\n- **expression**: `$instanceName`\n- **usedVariables**: `[ '$instanceName' ]`\n\n### Inlining dependencies\n\nIf you perfectly know that a dependency will be used only by a given entry, you can **inline** the code of the\ndependency into the code generating the main entry. To do this, you just need to put **null** as the identifier\nfor your instance.\n\n```php\nuse Mouf\\Container\\Definition\\InstanceDefinition;\n\n// null is passed as the identifier\n$dependencyDefinition = new InstanceDefinition(null, \"My\\\\Dependency\");\n\n$instanceDefinition = new InstanceDefinition(\"instanceName\", \"My\\\\Class\");\n$instanceDefinition-\u003eaddConstructorArgument($dependencyDefinition);\n\nreturn $instanceDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **statements**: \n  ```php\n  $myDependency = new My\\Dependency();\n  $instanceName = new My\\Class($myDependency);\n  ```\n- **expression**: `$instanceName`\n- **usedVariables**: `[ '$instanceName', '$myDependency' ]`\n\n### Creating a parameter entry\n\nA container does not store only objects. It can also store raw values. These values typically do not need to be\nstored in callbacks as resolving the callback would be an unnecessary burden. If you want to store a raw value,\nyou can use the `ParameterDefinition` and directly pass the value of the parameter to this class.\n\n```php\nuse Mouf\\Container\\Definition\\ParameterDefinition;\n\n$parameterDefinition = new ParameterDefinition(\"parameterName\", \"value\");\n\nreturn $parameterDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **statements**: *empty*\n- **expression**: `\"value\"`\n- **usedVariables**: *empty*\n- **lazilyEvaluated**: *false*\n\nThis code will generate an entry \"parameterName\" in your container whose value is \"value\".\nYou can pass any kind of scalar or array values to `ParameterDefinition`.\n\n### Creating a parameter entry that references a constant\n\nIf you want your parameter entry to actually point to a constant (declared with `define`) or a class constant\n(declared with `const`), you can use the `ConstParameterDefinition`.\n\n```php\nuse Mouf\\Container\\Definition\\ConstParameterDefinition;\n\n$parameterDefinition = new ConstParameterDefinition(\"parameterName\", \"My\\\\Class::CONSTANT\");\nreturn $parameterDefinition-\u003etoPhpCode('$container', []);\n```\n\nwill return an `InlineEntry` object containing:\n\n- **statements**: *empty*\n- **expression**: `My\\\\Class::CONSTANT`\n- **usedVariables**: *empty*\n- **lazilyEvaluated**: *false*\n\nThis code will generate an entry \"parameterName\" in your container that directly points to `My\\\\Class::CONSTANT`.\n\n### Creating an alias\n\nA container can store aliases to other container's entries. You can create an alias to another entry using the \n`AliasDefinition` class.\n\n```php\nuse Mouf\\Container\\Definition\\ParameterDefinition;\n\n$aliasDefinition = new AliasDefinition(\"alias\", \"aliased_entry\");\n```\n\nWhen calling `$container-\u003eget('alias')`, you will be given the entry stored in `aliased_entry`.\nGenerated code is:\n\n- **expression**: `$container-\u003eget('aliased_entry')`\n- **statements**: *empty*\n- **usedVariables**: *empty*\n\n### Creating a definition from a closure\n\nYou can define container entries using **closures**. When the entry is retrieved, the closure will be evaluated\nand the entry will be the return value of the closure.\n\n```php\nuse Mouf\\Container\\Definition\\ClosureDefinition;\nuse Interop\\Container\\ContainerInterface;\n\n$closureDefinition = new ClosureDefinition(\"closureDef\", function(ContainerInterface $container) {\n    return new My\\Service();\n});\n```\n\nPlease note:\n\n - The closure should accept one parameter: the container on which dependencies will be fetched\n - The closure cannot use the `$this` keyword\n - The closure cannot use context (the `use` keyword in the closure declaration)\n - The code of the closure will actually be **copied**, not referenced\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fcommon-container-definitions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fcommon-container-definitions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fcommon-container-definitions/lists"}