{"id":17291537,"url":"https://github.com/acelaya/zsm-annotated-services","last_synced_at":"2025-06-28T10:36:45.892Z","repository":{"id":56939962,"uuid":"55259947","full_name":"acelaya/zsm-annotated-services","owner":"acelaya","description":"A component to define how dependency injection has to be performed with Zend\\ServiceManager via annotations","archived":false,"fork":false,"pushed_at":"2017-07-23T17:06:33.000Z","size":30,"stargazers_count":11,"open_issues_count":0,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-14T15:50:48.997Z","etag":null,"topics":["annotations","dependency-injection","factories","zend-servicemanager","zf2","zf3"],"latest_commit_sha":null,"homepage":null,"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/acelaya.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2016-04-01T20:19:26.000Z","updated_at":"2023-11-20T08:11:01.000Z","dependencies_parsed_at":"2022-08-21T06:20:50.106Z","dependency_job_id":null,"html_url":"https://github.com/acelaya/zsm-annotated-services","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/acelaya/zsm-annotated-services","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acelaya%2Fzsm-annotated-services","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acelaya%2Fzsm-annotated-services/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acelaya%2Fzsm-annotated-services/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acelaya%2Fzsm-annotated-services/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acelaya","download_url":"https://codeload.github.com/acelaya/zsm-annotated-services/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acelaya%2Fzsm-annotated-services/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262417995,"owners_count":23307963,"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":["annotations","dependency-injection","factories","zend-servicemanager","zf2","zf3"],"created_at":"2024-10-15T10:41:15.157Z","updated_at":"2025-06-28T10:36:45.870Z","avatar_url":"https://github.com/acelaya.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Zend\\ServiceManager Annotated Services\n\n[![Build Status](https://travis-ci.org/acelaya/zsm-annotated-services.svg?branch=master)](https://travis-ci.org/acelaya/zsm-annotated-services)\n[![Code Coverage](https://scrutinizer-ci.com/g/acelaya/zsm-annotated-services/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/acelaya/zsm-annotated-services/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/acelaya/zsm-annotated-services/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/acelaya/zsm-annotated-services/?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/acelaya/zsm-annotated-services/v/stable.png)](https://packagist.org/packages/acelaya/zsm-annotated-services)\n[![Total Downloads](https://poser.pugx.org/acelaya/zsm-annotated-services/downloads.png)](https://packagist.org/packages/acelaya/zsm-annotated-services)\n[![License](https://poser.pugx.org/acelaya/zsm-annotated-services/license.png)](https://packagist.org/packages/acelaya/zsm-annotated-services)\n\nIf you are tired of defining lots of factories in your projects just to fetch some dependencies from the ServiceManager and the create a new service instance that gets those dependencies injected, try this.\n\nIt is a component that allows to define how dependency injection has to be performed with Zend\\ServiceManager via annotations.\n\n\u003e **Important!** While I will keep maintaining this project and providing bugfixes, I recommend you use the `ConfigAbstractFactory` instead, which is included in the ServiceManager package since v3.2.\n\n### Installation\n\nInstall this component with composer.\n\n    composer require acelaya/zsm-annotated-services\n\n### Basic usage\n\nThe traditional process is that you need to create a factory for each new service. Maybe sometimes you can reuse certain factories or abstract factories, but it is not the usual case.\n\n```php\nnamespace Acelaya;\n\nuse Interop\\Container\\ContainerInterface;\n\nclass MyFactory\n{\n    public funciton __invoke(ContainerInterface $container, $requestedName)\n    {\n        $foo = $container-\u003eget(Foo::class);\n        $bar = $container-\u003eget('bar');\n\n        return new MyService($foo, $bar);\n    }\n}\n```\n\nWith this component you just need to add a simple annotation to your service constructor with the services that need to be fetched from the ServiceManager and injected.\n\n```php\nnamespace Acelaya;\n\nuse Acelaya\\ZsmAnnotatedServices\\Annotation\\Inject;\n\nclass MyService\n{\n    /**\n     * @Inject({Foo::class, \"bar\"})\n     */\n    public function __construct($foo, $bar)\n    {\n        // [...]\n    }\n\n    // [...]\n}\n```\n\nAnd then, register the service with one of the provided factories (There is one factory that's used with Zend\\ServiceManager 2 and another that's used with Zend\\ServiceManager 3)\n\n```php\nuse Acelaya\\MyService;\nuse Acelaya\\ZsmAnnotatedServices\\Factory\\V3\\AnnotatedFactory;\nuse Zend\\ServiceManager\\ServiceManager;\n\n$sm = new ServiceManager([\n    'factories' =\u003e [\n        MyService::class =\u003e AnnotatedFactory::class,\n    ],\n]);\n```\n\nYou just need to replace `Acelaya\\ZsmAnnotatedServices\\Factory\\V3\\AnnotatedFactory` by `Acelaya\\ZsmAnnotatedServices\\Factory\\V2\\AnnotatedFactory` if you are using the v2 ServiceManager.\n\n### Cache\n\nThat looks cool, but processing annotations takes time. If you use this approach with several services, you will see your application's performance reduced.\n\nThat's why this library allows to use Doctrine\\Cache adapters in order to cache the result of processing annotations.\n\nFirst install the cache component.\n\n    composer require doctrine/cache\n\nThen register another service which returns a `Doctrine\\Common\\Cache\\Cache` instance with the key `Acelaya\\ZsmAnnotatedServices\\Factory\\AbstractAnnotatedFactory::CACHE_SERVICE` (or just \"Acelaya\\ZsmAnnotatedServices\\Cache\", which is the value of the constant).\n\nBy doing this, your annotations will be processed and cached, improving performance for subsequent requests.\n\n### Dot notation for array or ArrayAccess services\n\nWhen you need to inject just one part of a service which contains an array or ArrayAccess object, you can use the dot notation, where the first part is the service name and the rest of the parts are the keys to fetch from the array.\n\nFor example, imagine this services specification:\n\n```php\nuse Acelaya\\MyService;\nuse Acelaya\\ZsmAnnotatedServices\\Factory\\V3\\AnnotatedFactory;\nuse Zend\\ServiceManager\\ServiceManager;\n\n$sm = new ServiceManager([\n    'services' =\u003e [\n        'config' =\u003e [\n            'mail' =\u003e [\n                'smtp' =\u003e [\n                    // [...]\n                ],\n                'from' =\u003e 'foo@bar.com',\n                'subject' =\u003e 'Welcome!',\n            ],\n            'logger' =\u003e [\n                'file' =\u003e '/var/log/my_log.log'\n            ],\n        ],\n    ],\n    'factories' =\u003e [\n        MyService::class =\u003e AnnotatedFactory::class,\n    ],\n]);\n```\n\nAnd this service with the `@Inject` annotation:\n\n```php\nnamespace Acelaya;\n\nuse Acelaya\\ZsmAnnotatedServices\\Annotation\\Inject;\n\nclass MyService\n{\n    /**\n     * @Inject({\"config.mail.from\"})\n     */\n    public function __construct($from)\n    {\n        // The value of $from will be 'foo@bar.com'\n    }\n}\n```\n\nThe injectable service is defined by **config.mail.from**. In this case, the `AnnotatedFactory` will assume that the service name is **config**, and that it contains an associative array or ArrayAccess object. Then, it will use the rest of the dotted parts as nested keys in that array, and finally get the last value and inject it in the service.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facelaya%2Fzsm-annotated-services","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facelaya%2Fzsm-annotated-services","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facelaya%2Fzsm-annotated-services/lists"}