{"id":19745633,"url":"https://github.com/kocuj/di","last_synced_at":"2026-02-17T22:02:55.492Z","repository":{"id":57007906,"uuid":"106261402","full_name":"kocuj/di","owner":"kocuj","description":":syringe: Container for design pattern Dependency Injection in PHP 7","archived":false,"fork":false,"pushed_at":"2025-01-04T21:09:49.000Z","size":240,"stargazers_count":2,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T10:46:13.529Z","etag":null,"topics":["container","dependency-injection","di-container","oop","php","php7","psr-1","psr-11","psr-2","psr-4"],"latest_commit_sha":null,"homepage":"http://libs.kocuj.pl/php-libraries/kocuj-di/","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/kocuj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","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":"2017-10-09T09:11:34.000Z","updated_at":"2024-05-18T19:17:57.000Z","dependencies_parsed_at":"2024-05-06T19:48:16.708Z","dependency_job_id":"f70a1cd4-301c-45fe-a8d4-07197669e8b0","html_url":"https://github.com/kocuj/di","commit_stats":{"total_commits":38,"total_committers":2,"mean_commits":19.0,"dds":0.02631578947368418,"last_synced_commit":"5d6375aee0ad6c82e56f84590d7a806d0af6b53a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/kocuj/di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kocuj%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kocuj%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kocuj%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kocuj%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kocuj","download_url":"https://codeload.github.com/kocuj/di/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kocuj%2Fdi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29559961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T21:50:49.831Z","status":"ssl_error","status_checked_at":"2026-02-17T21:46:15.313Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["container","dependency-injection","di-container","oop","php","php7","psr-1","psr-11","psr-2","psr-4"],"created_at":"2024-11-12T02:10:36.237Z","updated_at":"2026-02-17T22:02:55.463Z","avatar_url":"https://github.com/kocuj.png","language":"PHP","readme":"# Kocuj DI\nContainer for design pattern Dependency Injection in PHP 7\n\n[![Author](http://img.shields.io/badge/author-kocuj.pl-blue.svg?style=flat-square)](http://kocuj.pl)\n[![Latest Version](https://img.shields.io/github/release/kocuj/di.svg?style=flat-square)](https://github.com/kocuj/di/releases)\n[![Github Issues](http://githubbadges.herokuapp.com/kocuj/di/issues.svg)](https://github.com/kocuj/di/issues)\n[![Pending Pull-Requests](http://githubbadges.herokuapp.com/kocuj/di/pulls.svg)](https://github.com/kocuj/di/pulls)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/kocuj/di/blob/master/LICENSE.md)\n[![Build Status](https://img.shields.io/travis/kocuj/di/master.svg?style=flat-square)](https://travis-ci.org/kocuj/di)\n[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/kocuj/di.svg?style=flat-square)](https://scrutinizer-ci.com/g/kocuj/di/code-structure)\n[![Quality Score](https://img.shields.io/scrutinizer/g/kocuj/di.svg?style=flat-square)](https://scrutinizer-ci.com/g/kocuj/di)\n[![Total Downloads](https://img.shields.io/packagist/dt/kocuj/di.svg?style=flat-square)](https://packagist.org/packages/kocuj/di)\n\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/8445db3c-571b-48e3-a71b-e46de879c955/big.png)](https://insight.sensiolabs.com/projects/8445db3c-571b-48e3-a71b-e46de879c955)\n\nThis package is compliant with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md), [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) and [PSR-11](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md). If you notice compliance oversights, please send a patch via pull request.\n\n## [Download 1.4.1](https://github.com/kocuj/di/releases/tag/v1.4.1)\n\n## Install\n\nVia Composer:\n\n``` bash\n$ composer require kocuj/di\n```\n\n## Requirements\n\nThe following versions of PHP are supported by this version:\n\n* PHP 7.0\n* PHP 7.1\n* PHP 7.2\n* PHP 7.3\n\n## Documentation\n\nIn this documentation it is assumed that you will import namespaces for the Kocuj DI library by using the following code:\n\n```php\nuse Kocuj\\Di\\Di;\nuse Kocuj\\Di\\Service\\ServiceType\\ServiceType;\n```\n\nTo use the Kocuj DI library you must first initialize it:\n\n```php\n$di = new Di();\n```\n\nIt will create a Kocuj DI library object. You can create more Di objects, but it is not recommended. The better solution is to create more DI containers, which will be explained in the next part of this documentation.\n\nAfter the construction, the $di variable will have one container for Dependency Injection, which you can get by using the following code:\n\n```php\n$defaultContainer = $di-\u003egetDefault();\n```\n\nYou can also create more containers:\n\n```php\n$myContainer = $di-\u003ecreate();\n```\n\nYou can also create new container based on already existing container by using the following code:\n\n```php\n$myContainer = $di-\u003ecopy($oldContainer);\n```\n\nFrom now on you can use new container by using methods on $myContainer variable. However, the following documentation will use default container for explanations about using the Kocuj DI library.\n\nAfter creating the container (default or other), you can add services into it. There are two types of services:\n\n* shared - service which will be constructed only once, like in Singleton design pattern;\n* standard - service which will be constructed on each use.\n\nTo create service you can use the following method:\n`add(ServiceType $serviceType, string $id, string $source, array $arguments = []): ContainerInterface`\n\nThe $serviceType argument can be one of the following:\n\n* to create shared service - `new ServiceType(ServiceType::SHARED)`;\n* to create standard service - `new ServiceType(ServiceType::STANDARD)`.\n\nIf you want to ommit a $serviceType argument, you can use one of the following methods:\n\n* to create shared service - `addShared(string $id, string $source, array $arguments = []): ContainerInterface`;\n* to create standard service - `addStandard(string $id, string $source, array $arguments = []): ContainerInterface`.\n\nArgument $id is an identifier of created service. All identifiers will be automatically converted to camelCase format. Remember, that inside one container there can be only one service with the selected identifier.\n\nArgument $source is a fully qualified class name for service. It is a good practice to use \"::class\" notation in this place.\n\nFor example, to add shared service from class \\Services\\Service with \"someService\" identifier, use the following code:\n\n```php\n$myContainer-\u003eaddShared('someService', \\Services\\Service::class);\n```\n\nHowever, the best feature of the Kocuj DI library is to automatically resolving dependencies between services. To use this feature, there should be at least one argument sent to a service constructor. The place to do so is in $arguments argument.\n\nEach argument in $arguments contains an array with one element with index \"type\" and second with index \"value\" which value depends on value set in index \"type\". Element with index \"type\" contains a name of argument type.\n\nThere are two types of arguments selected by element with index \"type\":\n\n* \"service\" - to set service to get, there must be a second element in array with index \"value\" containing service identifier;\n* \"value\" - to set value, there must be a second element in array with index \"value\" containing this value.\n\nFor example, to add shared service from class \\Services\\OtherService with \"otherService\" identifier, which has constructor `__construct(\\Services\\Service $service, bool $status)` and require $status to set to true, use the following code:\n\n```php\n$myContainer-\u003eaddShared('otherService', \\Services\\OtherService::class, [\n    [\n        'type' =\u003e 'service',\n        'value' =\u003e \\Services\\Service::class\n    ],\n    [\n        'type' =\u003e 'value',\n        'value' =\u003e true\n    ]\n]);\n```\n\nThe order of adding services to container is not important, because during construction of service all dependencies will be automatically resolved.\n\nTo get service object you can use the following code:\n\n```php\n$myContainer-\u003eget('otherService');\n```\n\nor:\n\n```php\n$myContainer-\u003egetOtherService();\n```\n\nAdditionally you can check type of service by using the following method: `checkType(string $id, ServiceType $serviceType): bool`. You can also check if service exists in container by using the following method: `has($id): bool`.\n\nTo control any wrong situations, there are the following exceptions available:\n\n* `\\Kocuj\\Di\\ArgumentParser\\Exception` - for problems with argument with other services and/or values for created service;\n* `\\Kocuj\\Di\\Container\\Exception` - for problem with creating or getting service in container;\n* `\\Kocuj\\Di\\Service\\Exception` - for problems with service type; however, this exception will not be used when this library is used correctly.\n\nExample of using the library:\n\n```php\n\u003c?php\n\nuse Kocuj\\Di\\Di;\n\n// initialize DI container\n$di = new Di();\n// get DI container\n$container = $di-\u003egetDefault();\n// set DI services\n$container-\u003eaddShared('input', InputService::class);\n$container-\u003eaddShared('output', OutputService::class);\n$container-\u003eaddStandard('main', Main::class, [\n    [\n        'type' =\u003e 'service',\n        'value' =\u003e 'input'\n    ],\n    [\n        'type' =\u003e 'service',\n        'value' =\u003e 'output'\n    ]\n]);\n// execute\n$container-\u003eget('main')-\u003edisplay();\n```\n\nFor more information you can see examples included in this project or by looking on [project website](http://libs.kocuj.pl/en/kocuj-di).\n\n## Testing\n\n``` bash\n$ vendor/bin/phpunit\n```\n\n## Creating programming documentation\n\n``` bash\n$ vendor/bin/phpdoc\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/kocuj/di/blob/master/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you discover any security related issues, please contact me by using [contact form on project website](http://libs.kocuj.pl/en/contact/) instead of using the issue tracker.\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/kocuj/di/blob/master/LICENSE.md) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkocuj%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkocuj%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkocuj%2Fdi/lists"}