{"id":28955787,"url":"https://github.com/kodedphp/container","last_synced_at":"2025-08-25T22:10:54.869Z","repository":{"id":35106542,"uuid":"207404507","full_name":"kodedphp/container","owner":"kodedphp","description":"A simple Dependency Injection Container with application modules support","archived":false,"fork":false,"pushed_at":"2022-01-25T16:41:07.000Z","size":114,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-27T23:22:02.261Z","etag":null,"topics":["container","dependency","dependency-injection","injection","psr-11"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kodedphp.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":"2019-09-09T21:05:15.000Z","updated_at":"2021-10-06T09:03:25.000Z","dependencies_parsed_at":"2022-08-08T05:01:18.211Z","dependency_job_id":null,"html_url":"https://github.com/kodedphp/container","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/kodedphp/container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fcontainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fcontainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fcontainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fcontainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kodedphp","download_url":"https://codeload.github.com/kodedphp/container/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fcontainer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268470799,"owners_count":24255391,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","dependency-injection","injection","psr-11"],"created_at":"2025-06-23T20:10:03.939Z","updated_at":"2025-08-02T23:13:22.644Z","avatar_url":"https://github.com/kodedphp.png","language":"PHP","readme":"Dependency Injection Container - Koded\n--------------------------------------\n\n[![CI](https://github.com/kodedphp/container/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/kodedphp/container/actions/workflows/unit-tests.yml)\n[![Latest Stable Version](https://img.shields.io/packagist/v/koded/container.svg)](https://packagist.org/packages/koded/container)\n[![Minimum PHP Version: 8.1](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/)\n\n\n`koded/container` is an OOP application bootstrapping and wiring library.\nIn other words, `Koded\\DIContainer` implements a **design pattern** called **Dependency Inversion**.\nThe main principle of DIP is to separate the behavior from dependency resolution.\n\n```bash\ncomposer require koded/container\n```\n\n## Example\n\nLet's look at a blog application that has\n- interfaces for the database repositories and corresponding implementations\n- a shared PDO instance\n- a service class for the blog content fetching\n- a handler class that maps the request method\n\n\n```php\nuse PDO;\n\ninterface PostRepository {\n    public function findBySlug(string $slug);\n}\n\ninterface UserRepository {\n    public function findById(int $id);\n}\n\nclass DatabasePostRepository implements PostRepository {\n    private $pdo;\n    public function __construct(PDO $pdo) {\n        $this-\u003epdo = $pdo;\n    }\n    public function findBySlug(string $slug) {\n        // $this-\u003epdo ...\n    }\n}\n\nclass DatabaseUserRepository implements UserRepository {\n    private $pdo;\n    public function __construct(PDO $pdo) {\n        $this-\u003epdo = $pdo;\n    }\n    public function findById(int $id) {\n        // $this-\u003epdo ...\n    }\n}\n```\n\nSomewhere we may have a service class that uses the dependent repositories:\n```php\nclass PostService {\n    private $post, $user;\n    public function __construct(PostRepository $post, UserRepository $user) {\n        $this-\u003epost = $post;\n        $this-\u003euser = $user;\n    }\n\n    // a service method that uses the post and user repository instances\n    public function findBlogPostBySlug(string $slug) {\n        $post = $this-\u003epost-\u003efindBySlug($slug);\n        $user = $this-\u003euser-\u003efindById($post-\u003euserId());\n        // ... do something with the results, create a result data structure...\n    }\n}\n```\n\nThen somewhere we might have a handler/controller that asks for its own dependencies:\n```php\nclass BlogHandler {\n    public function get(ServerRequestInterface $request, PostService $service): ResponseInterface {\n        $slug = slugify($request-\u003egetUri()-\u003egetPath());\n        $post = $service-\u003efindBlogPostBySlug($slug);\n\n        // some PSR-7 compatible response object\n        return new ServerResponse($post);\n    }\n}\n```\n\n### Wire All The Things\n\nThis is the bootstrapping / wiring application module \n(or container's \"configuration\" class) where all known dependencies\nare binded and shared\n\n```php\nclass BlogModule implements DIModule {\n    public function configure(DIContainer $container): void {\n        // bind interfaces to concrete class implementations\n        $container-\u003ebind(PostRepository::class, DatabasePostRepository::class);\n        $container-\u003ebind(UserRepository::class, DatabaseUserRepository::class);\n        $container-\u003ebind(ServerRequestInterface::class, /*some PSR-7 server request class name*/);\n        \n        // share one PDO instance\n        $container-\u003esingleton(PDO::class, ['sqlite:database.db']);\n    }\n}\n```\n\nAnd finally in the dispatcher file, we process the request\n```php\n// index.php\n\n// (resolved through an HTTP router or other means)\n$handler = BlogHandler::class;\n$method = 'get';\n\n// by invoking the container\n$response = (new DIContainer(new BlogModule))([$handler, $method]);\n\n// we have a `$response` object to output the content\n// ex. `echo $response-\u003egetBody()-\u003egetContents();`\n```\n\nThe container implements the [__invoke()][invoke] method, so the instance can be used as a function:\n```php\n$container('method', ['arg1', 'arg2', ...]);\n```\n\n\u003e To be continued...\n\n\nCode quality\n------------\n\n[![Code Coverage](https://scrutinizer-ci.com/g/kodedphp/container/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/container/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/kodedphp/container/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/container/?branch=master)\n[![Infection MSI](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fkodedphp%2Fcontainer%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/kodedphp/container/master)\n\n```shell script\nvendor/bin/infection --threads=4\nvendor/bin/phpbench run --report=default\nvendor/bin/phpunit\n```\n\n\nLicense\n-------\n[![Software license](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](LICENSE)\n\nThe code is distributed under the terms of [The 3-Clause BSD license](LICENSE).\n\n\n[invoke]: https://php.net/manual/en/language.oop5.magic.php#object.invoke","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodedphp%2Fcontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkodedphp%2Fcontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodedphp%2Fcontainer/lists"}