{"id":20733291,"url":"https://github.com/quillstack/di","last_synced_at":"2025-10-29T16:33:13.409Z","repository":{"id":45022564,"uuid":"291464853","full_name":"quillstack/di","owner":"quillstack","description":"The dependency injection container, based on PSR-11: Container interface.","archived":false,"fork":false,"pushed_at":"2022-06-02T11:34:09.000Z","size":55,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-23T22:55:38.279Z","etag":null,"topics":["class","constructor","container","dependency-injection","di","di-container","php8","properties","property","psr-11","quillstack"],"latest_commit_sha":null,"homepage":"https://quillstack.org/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/quillstack.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":"2020-08-30T12:20:04.000Z","updated_at":"2023-11-10T21:34:13.000Z","dependencies_parsed_at":"2022-09-15T17:12:07.805Z","dependency_job_id":null,"html_url":"https://github.com/quillstack/di","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quillstack%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quillstack%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quillstack%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quillstack%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quillstack","download_url":"https://codeload.github.com/quillstack/di/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250528704,"owners_count":21445512,"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":["class","constructor","container","dependency-injection","di","di-container","php8","properties","property","psr-11","quillstack"],"created_at":"2024-11-17T05:24:38.280Z","updated_at":"2025-10-29T16:33:13.350Z","avatar_url":"https://github.com/quillstack.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Quillstack DI Container\n\n[![Build Status](https://app.travis-ci.com/quillstack/di.svg?branch=main)](https://app.travis-ci.com/quillstack/di)\n[![Downloads](https://img.shields.io/packagist/dt/quillstack/di.svg)](https://packagist.org/packages/quillstack/di)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=quillstack_di\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=quillstack_di)\n[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=quillstack_di\u0026metric=ncloc)](https://sonarcloud.io/dashboard?id=quillstack_di)\n[![StyleCI](https://github.styleci.io/repos/291464853/shield?branch=main)](https://github.styleci.io/repos/291464853?branch=main)\n[![CodeFactor](https://www.codefactor.io/repository/github/quillstack/di/badge)](https://www.codefactor.io/repository/github/quillstack/di)\n![Packagist License](https://img.shields.io/packagist/l/quillstack/di)\n[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=quillstack_di\u0026metric=reliability_rating)](https://sonarcloud.io/dashboard?id=quillstack_di)\n[![Maintainability](https://api.codeclimate.com/v1/badges/d3657982e8a5bb50f4e3/maintainability)](https://codeclimate.com/github/quillstack/di/maintainability)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=quillstack_di\u0026metric=security_rating)](https://sonarcloud.io/dashboard?id=quillstack_di)\n![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/quillstack/di)\n\nQuillstack DI Container is the dependency injection container based\non _PSR-11: Container interface_, and with the main goal: to be fast.\nYou can find the full documentation on the website: \\\nhttps://quillstack.org/di\n\nThis DI container uses constructors and types of class properties.\n\n### Installation\n\nTo install this package, run the standard command using _Composer_:\n\n```\ncomposer require quillstack/di\n```\n\n### Usage\n\nYou can use Quillstack DI Container when you want:\n- To have a simple and fast DI container.\n- Define dependencies based on interfaces.\n- Define parameters e.g. credentials for a database in the `Database` class.\n- To use constructors or/and class properties.\n- To implement your own instance factories e.g. for `Request` classes.\n- To use objects as dependencies.\n\n#### Simple usage\n\nYou can easily start using a DI Container:\n\n```php\n\u003c?php\n\nuse Quillstack\\DI\\Container;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$container = new Container();\n$controller = $container-\u003eget(ExampleController::class);\n```\n\nWhere your `ExampleController` class looks like:\n\n```php\n\u003c?php\n\nclass ExampleController\n{\n    private $example = 3;\n}\n```\n\n#### Dependencies based on interfaces\n\nIf you want to define which class should be loaded based on an interface:\n\n```php\n$container = new Container([\n    LoggerInterface::class =\u003e Logger::class,\n]);\n$controller = $container-\u003eget(ExampleController::class);\n```\n\nYou can define your dependencies using interfaces:\n\n```php\n\u003c?php\n\nclass ExampleController\n{\n    public function __construct(\n        private LoggerInterface $logger\n    ) {\n    }\n}\n```\n\nWhen you create the object using the DI container, the type of `$logger` property will be set to `Logger`.\n\n#### Dependencies with parameters\n\nIf some of your classes require parameters, define them as an array\npassed on the second parameter to the container:\n\n```php\n$container = new Container([\n    Database::class =\u003e [\n        'hostname' =\u003e 'localhost',\n    ],\n]);\n$controller = $container-\u003eget(ExampleController::class);\n```\n\nEvery time you will get a database object, a container will use `localhost` as\na value for `$hostname` parameter:\n\n```phpt\n\u003c?php\n\nclass Database\n{\n    public function __construct(\n        private string $hostname\n    ) {\n    }\n}\n```\n\n#### Custom instance factories\n\nYou can implement your own instance factory. This is especially useful when you want to create many objects in a class\nfamily that are very similar in some way.\n\nIn our example we want to create different request objects:\n\n```php\n\u003c?php\n\nclass CreateUserRequest implements RequestInterface\n{\n}\n```\n\nFirst, we had to create `RequestInterface` as a common interface for all requests.\n\nNext, we have to create an instance factory class. To create it, extend a class with `CustomFactoryInterface`:\n\n```php\n\u003c?php\n\nuse Quillstack\\DI\\CustomFactoryInterface;\n\nclass RequestClassFactory implements CustomFactoryInterface\n{\n    /**\n     * {@inheritDoc}\n     */\n    public function create(string $id): object\n    {\n        $factory = $this-\u003econtainer-\u003eget(GivenRequestFromGlobalsFactory::class);\n\n        return $factory-\u003ecreateGivenServerRequest($id);\n    }\n}\n```\n\nAlso, use this configuration array when you create a DI container:\n\n```php\n$container = new Container([\n    RequestInterface::class =\u003e RequestClassFactory::class,\n]);\n$controller = $container-\u003eget(ExampleController::class);\n```\n\nCustom factories are useful for objects you want to create similarly.\n\n#### Dependencies as objects\n\nIn this example, whenever a new class of LoggerInterface will be required as a dependency, a container will use a \npreviously defined object. This object can be created once in a bootstrap file and used in the entire application:\n\n```php\n$logger = new Logger('name');\n$logger-\u003epushHandler(new StreamHandler('var/app.log'););\n\n$container = new Container([\n    LoggerInterface::class =\u003e $logger,\n]);\n```\n\nThis configuration is helpful if an object should be created once and its instance\nshould be used in other places in the application.\n\n### Unit tests\n\nRun tests using a command:\n\n```\nphpdbg -qrr ./vendor/bin/unit-tests\n```\n\n### Docker\n\n```shell\n$ docker-compose up -d\n$ docker exec -w /var/www/html -it quillstack_di sh\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquillstack%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquillstack%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquillstack%2Fdi/lists"}