{"id":18550275,"url":"https://github.com/xp-forge/inject","last_synced_at":"2026-01-24T11:04:36.987Z","repository":{"id":19991150,"uuid":"23258403","full_name":"xp-forge/inject","owner":"xp-forge","description":"Dependency injection for the XP Framework","archived":false,"fork":false,"pushed_at":"2024-07-21T08:13:12.000Z","size":266,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-09T06:09:11.944Z","etag":null,"topics":["annotations","dependency-injection","injection","php","xp-framework"],"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/xp-forge.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-08-23T14:52:20.000Z","updated_at":"2024-07-16T20:23:26.000Z","dependencies_parsed_at":"2024-02-04T10:25:22.526Z","dependency_job_id":"98b143ca-2704-4db8-bfdb-627a145948c2","html_url":"https://github.com/xp-forge/inject","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Finject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Finject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Finject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Finject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-forge","download_url":"https://codeload.github.com/xp-forge/inject/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246092525,"owners_count":20722390,"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":["annotations","dependency-injection","injection","php","xp-framework"],"created_at":"2024-11-06T21:04:04.175Z","updated_at":"2026-01-24T11:04:31.960Z","avatar_url":"https://github.com/xp-forge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Inject\n======\n\n[![Build status on GitHub](https://github.com/xp-forge/inject/workflows/Tests/badge.svg)](https://github.com/xp-forge/inject/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-forge/inject/version.svg)](https://packagist.org/packages/xp-forge/inject)\n\nThe inject package contains the XP framework's dependency injection API. Its entry point class is the \"Injector\".\n\nBinding\n-------\nValues can be bound to the injector by using its `bind()` method. It accepts the type to bind to, an optional name and these different scenarios:\n\n* **Binding a class**: The typical usecase, where we bind an interface to its concrete implementation.\n* **Binding an instance**: By binding a type to an existing instance, we can create a *singleton* model.\n* **Binding a provider**: If we need more complicated code to create an instance, we can bind to a provider.\n* **Binding a named lookup**: If we want control over the binding lookups for a type, we can bind to a `Named` instance.\n\n```php\nuse inject\\{Injector, Bindings};\nuse com\\example\\{Report, HtmlReport, Storage, InFileSystem};\n\n// Manually\n$injector= new Injector(Bindings::using()\n  -\u003etyped(Report::class, HtmlReport::class)\n  -\u003esingleton(Storage::class, new InFileSystem('.'))\n  -\u003enamed('title', 'Report title')\n);\n\n// Reusable via Bindings instances\nclass ApplicationDefaults extends Bindings {\n\n  public function configure($injector) {\n    $injector-\u003ebind(Report::class, HtmlReport::class);\n    $injector-\u003ebind(Storage::class, new InFileSystem('.'));\n    $injector-\u003ebind('string', 'Report title', 'title');\n  }\n}\n\n$injector= new Injector(new ApplicationDefaults());\n```\n\nInstance creation\n-----------------\nKeep in mind: **\"injector.get() is the new 'new'\"**. To create objects and perform injection, use the Injector's get() method instead of using the `new` keyword or factories.\n\n```php\nuse inject\\Injector;\n\n$injector-\u003ebind(Report::class, HtmlReport::class);\n\n// Explicit binding: Lookup finds binding to HtmlReport, creates instance.\n$instance= $injector-\u003eget(Report::class);\n\n// Implicit binding: No previous binding, TextReport instantiable, thus created.\n$instance= $injector-\u003eget(TextReport::class);\n```\n\nManual calls are usually not necessary though, instead you'll use injection:\n\nInjection\n---------\nInjection is performed by looking at a type's constructor. Bound values will be passed according to the given type hint.\n\n```php\nclass ReportImpl implements Report {\n\n  public function __construct(ReportWriter $writer) { ... }\n}\n```\n\nYou can supply the type by using parameter attributes in case where the PHP type system is not concise enough. If the bound value's name differs from the parameter name, you can supply a name argument.\n\n```php\nuse inject\\Inject;\n\nclass ReportImpl implements Report {\n\n  public function __construct(\n    ReportWriter $writer,\n    Format $format,\n    #[Inject(type: 'string[]', name: 'report-titles')]\n    $titles\n  ) { ... }\n}\n```\n\nWhen a required parameter is encountered and there is no bound value for this parameter, an `inject.ProvisionException` is raised.\n\n```php\nclass ReportWriter implements Writer {\n\n  public function __construct(Storage $storage) { ... }\n}\n\n$injector= new Injector();\n$report= $injector-\u003eget(ReportWriter::class);  // *** Storage not bound\n```\n\nMethod and field injection are not supported.\n\nConfiguration\n-------------\nAs seen above, bindings can be used instead of manually performing the wiring. You might want to configure some of your app's settings externally instead of harcoding them. Use the `inject.ConfiguredBindings` class for this:\n\n```php\n$injector= new Injector(\n  new ApplicationDefaults(),\n  new ConfiguredBindings(new Properties('etc/app.ini'))\n);\n```\n\nThe syntax for these INI files is simple:\n\n```ini\nweb.session.Sessions=web.session.InFileSystem(\"/tmp\")\nname=\"Application\"\n```\n\nProviders\n---------\nProviders allow implementing lazy-loading semantics. Every type bound to the injector can also be retrieved by a provider. Invoking its get() method will instantiate it.\n\n```php\n$provider= $injector-\u003eget('inject.Provider\u003ccom.example.writers.ReportWriter\u003e');\n\n// ...later on\n$instance= $provider-\u003eget();\n```\n\nNamed lookups\n-------------\nIf we need control over the lookup, we can bind instances of `Named`:\n\n```php\nuse inject\\{Injector, Named, InstanceBinding};\nuse com\\example\\Value;\n\n$inject= new Injector();\n$inject-\u003ebind(Value::class, new class() extends Named {\n  public function provides($name) { return true; }\n  public function binding($name) { return new InstanceBinding(new Value($name)); }\n});\n\n$value= $inject-\u003eget(Value::class, 'default');  // new Value(\"default\")\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Finject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-forge%2Finject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Finject/lists"}