{"id":28610886,"url":"https://github.com/devanych/di-container","last_synced_at":"2026-01-11T16:49:38.433Z","repository":{"id":50192805,"uuid":"191013452","full_name":"devanych/di-container","owner":"devanych","description":"Simple implementation of a PSR-11 dependency injection container","archived":false,"fork":false,"pushed_at":"2023-03-27T19:08:01.000Z","size":47,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-18T19:40:59.734Z","etag":null,"topics":["autowire","autowiring","container","di","php","psr-11"],"latest_commit_sha":null,"homepage":"","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/devanych.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null}},"created_at":"2019-06-09T14:01:52.000Z","updated_at":"2024-11-30T12:15:05.000Z","dependencies_parsed_at":"2023-01-30T01:31:03.919Z","dependency_job_id":null,"html_url":"https://github.com/devanych/di-container","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/devanych/di-container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fdi-container","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fdi-container/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fdi-container/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fdi-container/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devanych","download_url":"https://codeload.github.com/devanych/di-container/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fdi-container/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259360728,"owners_count":22845817,"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":["autowire","autowiring","container","di","php","psr-11"],"created_at":"2025-06-11T23:01:48.844Z","updated_at":"2026-01-11T16:49:38.405Z","avatar_url":"https://github.com/devanych.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Dependency Injection Container\n\n[![License](https://poser.pugx.org/devanych/di-container/license)](https://packagist.org/packages/devanych/di-container)\n[![Latest Stable Version](https://poser.pugx.org/devanych/di-container/v)](https://packagist.org/packages/devanych/di-container)\n[![Total Downloads](https://poser.pugx.org/devanych/di-container/downloads)](https://packagist.org/packages/devanych/di-container)\n[![GitHub Build Status](https://github.com/devanych/di-container/workflows/build/badge.svg)](https://github.com/devanych/di-container/actions)\n[![GitHub Static Analysis Status](https://github.com/devanych/di-container/workflows/static/badge.svg)](https://github.com/devanych/di-container/actions)\n[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/devanych/di-container/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/devanych/di-container/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/devanych/di-container/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/devanych/di-container/?branch=master)\n\nA simple and lightweight container for dependency injection using autowiring that implements [PSR-11 Container](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md).\n\nSupport:\n\n* Objects or class names.\n\n* Anonymous functions (Closure instance).\n\n* Scalars (integer, float, string, boolean).\n\n* Arrays and nested arrays with all of the above data types.\n\nA guide with a detailed description in Russian language is [available here](https://devanych.ru/development/prostoj-di-kontejner-s-podderzhkoj-avtovajringa).\n\n## Installation\n\nThis package requires PHP version 7.4 or later.\n\n```\ncomposer require devanych/di-container\n```\n\n## Usage\n\nCreate a container:\n\n```php\nuse Devanych\\Di\\Container;\n\n$container = new Container();\n// or with definitions array\n$container = new Container($definitions);\n```\n\nSets in the container:\n\n```php\n/**\n * Sets definition to the container.\n *\n * @param string $id\n * @param mixed $definition\n */\n$container-\u003eset($id, $definition);\n\n/**\n * Sets multiple definitions at once; used in the constructor.\n *\n * @param array\u003cstring, mixed\u003e $definitions\n */\n$container-\u003esetMultiple($definitions);\n```\n\nExistence in the container:\n\n```php\n/**\n * Returns 'true` if the dependency with this ID was sets, otherwise `false`.\n *\n * @param string $id\n * @return bool\n */\n$container-\u003ehas($id);\n```\n\nGets from the container:\n\n```php\n/**\n * Gets instance by definition from the container by ID.\n *\n * @param string $id\n * @return mixed\n * @throws Devanych\\Di\\Exception\\NotFoundException If not found definition in the container.\n * @throws Devanych\\Di\\Exception\\ContainerException If unable to create instance.\n */\n$container-\u003eget($id);\n\n/**\n * Always gets a new instance by definition from the container by ID.\n *\n * @param string $id\n * @return mixed\n * @throws Devanych\\Di\\Exception\\NotFoundException If not found definition in the container.\n * @throws Devanych\\Di\\Exception\\ContainerException If unable to create instance.\n */\n$container-\u003egetNew($id);\n\n/**\n * Gets original definition from the container by ID.\n *\n * @param string $id\n * @return mixed\n * @throws Devanych\\Di\\Exception\\NotFoundException If not found definition in the container.\n */\n$container-\u003egetDefinition($id);\n```\nIf the definition is an anonymous function or class name, the `get()` method will execute the function and create an instance of the class only the first time, and subsequent `get()` calls will return the result already created.\n\n\u003e If you need to execute a function and create an instance of the class every time, use the `getNew ()` method.\n\nIf the definition is a class name and there are dependencies in its constructor, then when calling the `get()` and `getNew ()` methods, at the time of creating the class instance, the container will recursively bypass all dependencies and try to resolve them.\n\n\u003e If the passed parameter `$id` to the methods `get()` and `getNew()` is a class name and has not been set previously by the `set()` method, an object of these class will still be created as if it had been set.\n\u003e\n\u003e If `$id` is not a class name and has not been set previously by the `set()` method, the exception `Devanych\\Di\\Exception\\NotFoundException` will be thrown.\n\n## Examples of use\n\nSimple usage:\n\n```php\n// Set string\n$container-\u003eset('string', 'value');\n$container-\u003eget('string'); // 'value'\n\n// Set integer\n$container-\u003eset('integer', 5);\n$container-\u003eget('integer'); // 5\n\n// Set array\n$container-\u003eset('array', [1,2,3]);\n$container-\u003eget('array'); // [1,2,3]\n\n// Set nested array\n$container-\u003eset('nested', [\n    'scalar' =\u003e [\n        'integer' =\u003e 5,\n        'float' =\u003e 3.7,\n        'boolean' =\u003e false,\n        'string' =\u003e 'string',\n    ],\n    'not_scalar' =\u003e [\n        'closure' =\u003e fn() =\u003e null,\n        'object' =\u003e new User(),\n        'array' =\u003e ['array'],\n    ],\n]);\n\n// Set object\n$container-\u003eset('user', fn() =\u003e new User());\n$container-\u003eget('user'); // User instance\n// Or\n$container-\u003eset('user', User::class);\n$container-\u003eget('user');\n// Or\n$container-\u003eset(User::class, User::class);\n$container-\u003eget(User::class);\n// Or without setting via `set()`\n$container-\u003eget(User::class);\n```\n\nUsage of dependencies:\n\n```php\n/*\nfinal class UserProfile\n{\n    private $name;\n    private $age;\n\n    public function __construct(string $name = 'John', int $age = 25)\n    {\n        $this-\u003ename = $name;\n        $this-\u003eage = $age;\n    }\n}\n\nfinal class User\n{\n    private $profile;\n\n    public function __construct(UserProfile $profile)\n    {\n        $this-\u003eprofile = $profile;\n    }\n}\n*/\n\n$container-\u003eset('user_name', 'Alexander');\n$container-\u003eset('user_age', 40);\n\n$container-\u003eset('user', function (\\Psr\\Container\\ContainerInterface $container): User {\n    $name = $container-\u003eget('user_name');\n    $age = $container-\u003eget('user_age');\n    $profile = new UserProfile($name, $age);\n    return new User($profile);\n});\n\n$container-\u003eget('user');\n\n// Or\n\n$container-\u003eset(UserProfile::class, function (\\Psr\\Container\\ContainerInterface $container): UserProfile {\n    return new UserProfile($container-\u003eget('user_name'), $container-\u003eget('user_age'));\n});\n\n$container-\u003eget(User::class);\n\n// Or with default values (`John` and `25`)\n\n$container-\u003eget(User::class);\n```\n\nUsage with dependencies and factories:\n\n```php\n/*\nfinal class UserProfileFactory implements \\Devanych\\Di\\FactoryInterface\n{\n    public function create(\\Psr\\Container\\ContainerInterface $container): UserProfile\n    {\n        return new UserProfile($container-\u003eget('user_name'), $container-\u003eget('user_age'));\n    }\n}\n*/\n\n$container-\u003esetMultiple([\n    UserProfile::class =\u003e UserProfileFactory::class,\n    // Or without autowiring\n    // UserProfile::class =\u003e fn =\u003e UserProfileFactory(),\n    // UserProfile::class =\u003e new UserProfileFactory(),\n    'user_name' =\u003e 'Alexander',\n    'user_age' =\u003e 40,\n]);\n\n$container-\u003eget(User::class); // User instance\n$container-\u003eget(UserProfile::class); // UserProfile instance\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevanych%2Fdi-container","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevanych%2Fdi-container","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevanych%2Fdi-container/lists"}