{"id":20719701,"url":"https://github.com/slince/di","last_synced_at":"2025-06-27T00:04:31.483Z","repository":{"id":20475673,"uuid":"23753332","full_name":"slince/di","owner":"slince","description":":sheep: A flexible  dependency injection container; It is an implementation of PSR-11","archived":false,"fork":false,"pushed_at":"2023-06-16T15:22:13.000Z","size":172,"stargazers_count":20,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-23T15:33:46.110Z","etag":null,"topics":["container","dependency-injection","di","injection","ioc-container","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slince.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-09-07T05:39:12.000Z","updated_at":"2023-01-06T04:12:07.000Z","dependencies_parsed_at":"2024-06-18T18:36:47.093Z","dependency_job_id":null,"html_url":"https://github.com/slince/di","commit_stats":{"total_commits":141,"total_committers":4,"mean_commits":35.25,"dds":"0.34751773049645385","last_synced_commit":"1cc23487f9ffb459f3eb4ca68f4c3e7005a6d9ba"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/slince/di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slince","download_url":"https://codeload.github.com/slince/di/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Fdi/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261967137,"owners_count":23237666,"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":["container","dependency-injection","di","injection","ioc-container","php","psr-11"],"created_at":"2024-11-17T03:18:04.705Z","updated_at":"2025-06-27T00:04:31.403Z","avatar_url":"https://github.com/slince.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# Dependency Injection Container\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/slince/di/test.yml?style=flat-square)](https://github.com/slince/di/actions)\n[![Coverage Status](https://img.shields.io/codecov/c/github/slince/di.svg?style=flat-square)](https://codecov.io/github/slince/di)\n[![Total Downloads](https://img.shields.io/packagist/dt/slince/di.svg?style=flat-square)](https://packagist.org/packages/slince/di)\n[![Latest Stable Version](https://img.shields.io/packagist/v/slince/di.svg?style=flat-square\u0026label=stable)](https://packagist.org/packages/slince/di)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/slince/di.svg?style=flat-square)](https://scrutinizer-ci.com/g/slince/di/?branch=master)\n\nThis package is a flexible IOC container for PHP with a focus on being lightweight and fast as well as requiring as little \nconfiguration as possible. It is an implementation of [PSR-11](https://github.com/container-interop/fig-standards/blob/master/proposed/container.md)\n\n## Installation\n\nInstall via composer.\n\n```json\n{\n    \"require\": {\n        \"slince/di\": \"^3.0\"\n    }\n}\n```\n\nAlternatively, require package use composer cli:\n\n```bash\ncomposer require slince/di ^3.0\n```\n\n## Usage\n\nContainer is dependency injection container. It allows you to implement the dependency injection design pattern meaning that you can decouple your class dependencies and have the container inject them where they are needed.\n\n```php\nnamespace Acme;\n\nclass Foo\n{\n   /**\n     * @var \\Acme\\Bar\n     */\n    public $bar;\n\n    /**\n     * Construct.\n     */\n    public function __construct(Bar $bar)\n    {\n        $this-\u003ebar = $bar;\n    }\n}\n\nclass Bar\n{\n    public $foo;\n    public $baz;\n    \n    public function __construct($foo, $baz)\n    {\n        $this-\u003efoo = $foo;\n        $this-\u003ebaz = $baz;\n    }\n}\n\n$container = new Slince\\Di\\Container();\n\n$container-\u003eregister(Acme\\Foo::class);\n$foo = $container-\u003eget(Acme\\Foo::class);\n\nvar_dump($foo instanceof Acme\\Foo);      // true\nvar_dump($foo-\u003ebar instanceof Acme\\Bar); // true\n```\n\n### Make Service References\n\n```php\n$container-\u003eregister('bar', Acme\\Bar::class);\n$container-\u003eregister('foo', Acme\\Foo::class)\n    -\u003eaddArgument(new Slince\\Di\\Reference('bar')); //refer to 'bar'\n    \nvar_dump($container-\u003eget('bar') === $container-\u003eget('foo')-\u003ebar));    // true\n```\n\n### Use a Factory to Create Services\n\nSuppose you have a factory that configures and returns a new `NewsletterManager` object \nby calling the static `createNewsletterManager()` method:\n\n```php\nclass NewsletterManagerStaticFactory\n{\n    public static function createNewsletterManager($parameter)\n    {\n        $newsletterManager = new NewsletterManager($parameter);\n\n        // ...\n\n        return $newsletterManager;\n    }\n}\n```\n\n```php\n// call the static method\n$container-\u003eregister(\n    NewsletterManager::class, \n    array(NewsletterManagerStaticFactory::class, 'createNewsletterManager')\n)-\u003eaddArgument('foo');\n\n```\nIf your factory is not using a static function to configure and create your service, but a regular method, \nyou can instantiate the factory itself as a service too. \n\n```php\n// call a method on the specified factory service\n$container-\u003eregister(NewsletterManager::class, [\n    new Reference(NewsletterManagerFactory::class),\n    'createNewsletterManager'\n]);\n```\n\n### Create Service Aliases\n\n```php\n$container-\u003eregister(Acme\\Foo::class);\n$container-\u003esetAlias('foo-alias', Acme\\Foo::class);\n$foo = $container-\u003eget('foo-alias');\n\nvar_dump($foo instanceof Acme\\Foo);      // true\n```\n\n### Configure container \n\n- Singleton\n\n```php\n$container-\u003esetDefaults([\n    'share' =\u003e false\n]);\n$container-\u003eregister('foo', Acme\\Foo::class);\nvar_dump($container-\u003eget('foo') === $container-\u003eget('foo'));      // false\n```\n\n- Autowiring\n\n```php\n$container-\u003esetDefaults([\n    'autowire' =\u003e false,\n]);\n$container-\u003eregister('foo', Acme\\Foo::class)\n    -\u003eaddArgument(new Acme\\Bar());  // You have to provide $bar\n    \nvar_dump($container-\u003eget('foo') instanceof Acme\\Foo::class);  // true\n```\n\n### Container Parameters\n\n```php\n$container-\u003esetParameters([\n    'foo' =\u003e 'hello',\n    'bar' =\u003e [\n        'baz' =\u003e 'world'\n    ]\n]);\n\n$container-\u003eregister('bar', Acme\\Bar::class)\n     -\u003esetArguments([\n        'foo' =\u003e $container-\u003egetParameter('foo'),\n        'baz' =\u003e $container-\u003egetParameter('bar.baz')\n    ]);\n\n$bar = $container-\u003eget('bar');\nvar_dump($bar-\u003efoo);  // hello\nvar_dump($bar-\u003ebar); // world\n```\n\n### Work with Service Tags\n\n```php\n$container-\u003eregister('foo')-\u003eaddTag('my.tag', array('hello' =\u003e 'world'));\n\n$serviceIds = $container-\u003efindTaggedServiceIds('my.tag');\n\nforeach ($serviceIds as $serviceId =\u003e $tags) {\n    foreach ($tags as $tag) {\n        echo $tag['hello'];\n    }\n}\n```\n## License\n \nThe MIT license. See [MIT](https://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslince%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslince%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslince%2Fdi/lists"}