{"id":14966334,"url":"https://github.com/yiisoft/definitions","last_synced_at":"2025-04-09T13:05:35.713Z","repository":{"id":37089763,"uuid":"399962810","full_name":"yiisoft/definitions","owner":"yiisoft","description":"The package provides definition syntax. Definition is describing a way to create and configure a service or an object.","archived":false,"fork":false,"pushed_at":"2024-10-27T21:22:49.000Z","size":303,"stargazers_count":12,"open_issues_count":9,"forks_count":6,"subscribers_count":19,"default_branch":"master","last_synced_at":"2024-10-29T14:35:34.805Z","etag":null,"topics":["di-container","factory","hacktoberfest","yii3"],"latest_commit_sha":null,"homepage":"https://www.yiiframework.com/","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/yiisoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"yiisoft","github":["yiisoft"]}},"created_at":"2021-08-25T21:36:07.000Z","updated_at":"2024-06-26T09:46:00.000Z","dependencies_parsed_at":"2024-01-11T14:13:24.762Z","dependency_job_id":"c5402ef4-7bee-4227-aa2f-7e68a5f9ceba","html_url":"https://github.com/yiisoft/definitions","commit_stats":{"total_commits":133,"total_committers":13,"mean_commits":10.23076923076923,"dds":0.7593984962406015,"last_synced_commit":"674ef5b9fe35bb8f16f9cba4180272087fd82de3"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":"yiisoft/package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fdefinitions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fdefinitions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fdefinitions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fdefinitions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiisoft","download_url":"https://codeload.github.com/yiisoft/definitions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045230,"owners_count":21038553,"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":["di-container","factory","hacktoberfest","yii3"],"created_at":"2024-09-24T13:36:14.235Z","updated_at":"2025-04-09T13:05:35.692Z","avatar_url":"https://github.com/yiisoft.png","language":"PHP","funding_links":["https://opencollective.com/yiisoft","https://github.com/sponsors/yiisoft"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://yiisoft.github.io/docs/images/yii_logo.svg\" height=\"100px\" alt=\"Yii\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eYii Definitions\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\n[![Latest Stable Version](https://poser.pugx.org/yiisoft/definitions/v)](https://packagist.org/packages/yiisoft/definitions)\n[![Total Downloads](https://poser.pugx.org/yiisoft/definitions/downloads)](https://packagist.org/packages/yiisoft/definitions)\n[![Build status](https://github.com/yiisoft/definitions/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/definitions/actions/workflows/build.yml)\n[![Code Coverage](https://codecov.io/gh/yiisoft/definitions/graph/badge.svg?token=9srXPaWGlj)](https://codecov.io/gh/yiisoft/definitions)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdefinitions%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/definitions/master)\n[![Static analysis](https://github.com/yiisoft/definitions/actions/workflows/static.yml/badge.svg?branch=master)](https://github.com/yiisoft/definitions/actions/workflows/static.yml?query=branch%3Amaster)\n[![type-coverage](https://shepherd.dev/github/yiisoft/definitions/coverage.svg)](https://shepherd.dev/github/yiisoft/definitions)\n\nThe package provides syntax constructs describing a way to create and configure a service or an object.\nIt is used by [yiisoft/di](https://github.com/yiisoft/di) and [yiisoft/factory](https://github.com/yiisoft/factory)\nbut could be used in other [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible packages as well.\n\nThe following are provided:\n\n- Definitions describing services or objects to create. This includes syntax, its validation and resolving it to objects.\n- References and dynamic references to point to other definitions. These include additional utility to refer to multiple\n  definitions at once.\n\n## Requirements\n\n- PHP 8.1 or higher.\n\n## Installation\n\nThe package could be installed with [Composer](https://getcomposer.org):\n\n```shell\ncomposer require yiisoft/definitions\n```\n\n## General usage\n\n### Definitions\n\nDefinition is describing a way to create and configure a service, an object\nor return any other value. It must implement `Yiisoft\\Definitions\\Contract\\DefinitionInterface`\nthat has a single method `resolve(ContainerInterface $container)`. References are\ntypically stored in the container or a factory and are resolved into an object\nat the moment of getting a service instance or creating an object.\n\n#### `ArrayDefinition`\n\nArray definition allows describing a service or an object declaratively:\n\n```php\nuse \\Yiisoft\\Definitions\\ArrayDefinition;\n\n$definition = ArrayDefinition::fromConfig([\n    'class' =\u003e MyServiceInterface::class,\n    '__construct()' =\u003e [42], \n    '$propertyName' =\u003e 'value',\n    'setName()' =\u003e ['Alex'],\n]);\n$object = $definition-\u003eresolve($container);\n```\n\nIn the above:\n\n- `class` contains the name of the class to be instantiated.\n- `__construct()` holds an array of constructor arguments.\n- The rests of the config are property values (prefixed with `$`)\n  and method calls, postfixed with `()`. They are set/called\n  in the order they appear in the array.\n\nFor multiple method call postfix key with unique string, for example:\n\n```php\n[\n    'class' =\u003e Collector::class,\n    'add()' =\u003e ['Alex'],\n    'add()2' =\u003e ['Mike'],\n]\n```\n\n#### `CallableDefinition`\n\nCallable definition builds an object by executing a callable injecting\ndependencies based on types used in its signature:\n\n```php\nuse \\Yiisoft\\Definitions\\CallableDefinition;\n\n$definition = new CallableDefinition(\n    fn (SomeFactory $factory) =\u003e $factory-\u003ecreate('args')\n);\n$object = $definition-\u003eresolve($container);\n\n// or \n\n$definition = new CallableDefinition(\n    fn () =\u003e MyFactory::create('args')\n);\n$object = $definition-\u003eresolve($container);\n\n// or\n\n$definition = new CallableDefinition(\n    [MyFactory::class, 'create']\n);\n$object = $definition-\u003eresolve($container);\n```\n\nIn the above, we use a closure, a static call and a static\nmethod passed as array-callable. In each case, we determine\nand pass dependencies based on the types of arguments in\nthe callable signature.\n\n#### `ParameterDefinition`\n\nParameter definition resolves an object based on information from `ReflectionParameter` instance:\n\n```php\nuse \\Yiisoft\\Definitions\\ParameterDefinition;\n\n$definition = new ParameterDefinition($reflectionParameter);\n$object = $definition-\u003eresolve($container);\n```\n\nIt is mostly used internally when working with callables.\n\n#### `ValueDefinition`\n\nValue definition resolves value passed as is:\n\n```php\nuse \\Yiisoft\\Definitions\\ValueDefinition;\n\n$definition = new ValueDefinition(42, 'int');\n$value = $definition-\u003eresolve($container); // 42\n```\n\n### References\n\nReferences point to other definitions, so when defining a definition, you can use other definitions as its\ndependencies:\n\n```php\n[\n    InterfaceA::class =\u003e ConcreteA::class,\n    'alternativeForA' =\u003e ConcreteB::class,\n    MyService::class =\u003e [\n        '__construct()' =\u003e [\n            Reference::to('alternativeForA'),\n        ],\n    ],\n]\n```\n\nOptional reference returns `null` when there's no corresponding definition in container:\n\n```php\n[\n    MyService::class =\u003e [\n        '__construct()' =\u003e [\n            // If a container doesn't have definition for `EventDispatcherInterface` reference returns `null`\n            // when resolving dependencies\n            Reference::optional(EventDispatcherInterface::class), \n        ],\n    ],\n]\n```\n\nThe `DynamicReference` defines a dependency to a service not defined in the container:\n\n```php\n[\n   MyService::class =\u003e [\n       '__construct()' =\u003e [\n           DynamicReference::to([\n               'class' =\u003e SomeClass::class,\n               '$someProp' =\u003e 15\n           ])\n       ]\n   ]\n]\n```\n\nTo pass an array of IDs as references to a property or an argument, `Yiisoft\\Definitions\\ReferencesArray` or\n`Yiisoft\\Definitions\\DynamicReferencesArray` could be used:\n\n```php\n//params.php\nreturn [\n   'yiisoft/data-response' =\u003e [\n       'contentFormatters' =\u003e [\n           'text/html' =\u003e HtmlDataResponseFormatter::class,\n           'application/xml' =\u003e XmlDataResponseFormatter::class,\n           'application/json' =\u003e JsonDataResponseFormatter::class,\n       ],\n   ],\n];\n\n//web.php\n\nContentNegotiator::class =\u003e [\n    '__construct()' =\u003e [\n        'contentFormatters' =\u003e ReferencesArray::from($params['yiisoft/data-response']['contentFormatters']),\n    ],\n],\n```\n\n### Class aliases\n\nTo define another instance of a class with specific configuration, you can\nuse native PHP `class_alias()`:\n\n```php\nclass_alias(Yiisoft\\Db\\Pgsql\\Connection::class, 'MyPgSql');\n\nreturn [\n    MyPgSql::class =\u003e [ ... ]\n];\n```\n\n### Definition storage\n\nDefinition storage could be used to hold and get definitions and check if a certain definition could be instantiated.\nUsually it is used by an implementation using the definitions:\n\n```php\nuse Yiisoft\\Definitions\\DefinitionStorage;\n\n$storage = new DefinitionStorage([\n    MyInterface::class =\u003e MyClass::class,\n]);\n$storage-\u003esetDelegateContainer($fallbackContainer);\n\nif (!$storage-\u003ehas(MyInterface::class)) {\n    $buildStack = $storage-\u003egetBuildStack();\n    // ...\n}\n```\n\nIn the above `$buildStack` will contain a stack with definition IDs in the order the latest dependency obtained would be\nbuilt.\n\nBy default, if a class is checked in `has()` and it is not explicitly defined, the storage tries to autoload it first\nbefore failing. The storage may also work in a strict mode when everything in it should be defined explicitly:\n\n```php\nuse Yiisoft\\Definitions\\DefinitionStorage;\n\n$storage = new DefinitionStorage([], true);\nvar_dump($storage-\u003ehas(EngineMarkOne::class));\n```\n\n`has()` will return `false` even if `EngineMarkOne` exists.\n\n## Documentation\n\n- [Internals](docs/internals.md)\n\nIf you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for\nthat. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).\n\n## License\n\nThe Yii Definitions is free software. It is released under the terms of the BSD License.\nPlease see [`LICENSE`](./LICENSE.md) for more information.\n\nMaintained by [Yii Software](https://www.yiiframework.com/).\n\n## Support the project\n\n[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective\u0026logoColor=7eadf1\u0026labelColor=555555)](https://opencollective.com/yiisoft)\n\n## Follow updates\n\n[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)\n[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter\u0026logoColor=1DA1F2\u0026labelColor=555555?style=flat)](https://twitter.com/yiiframework)\n[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat\u0026logo=telegram)](https://t.me/yii3en)\n[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat\u0026logo=facebook\u0026logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)\n[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat\u0026logo=slack)](https://yiiframework.com/go/slack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fdefinitions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiisoft%2Fdefinitions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fdefinitions/lists"}