{"id":19959698,"url":"https://github.com/reinfi/zf-dependency-injection","last_synced_at":"2025-05-03T21:31:15.879Z","repository":{"id":23738517,"uuid":"99731419","full_name":"reinfi/zf-dependency-injection","owner":"reinfi","description":"Advanced dependency injection for laminas framework","archived":false,"fork":false,"pushed_at":"2025-04-18T22:34:55.000Z","size":579,"stargazers_count":20,"open_issues_count":3,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-19T09:16:18.295Z","etag":null,"topics":["annotations","autowire","dependency-injection","laminas","yaml","zend-framework"],"latest_commit_sha":null,"homepage":"","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/reinfi.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-08T20:02:19.000Z","updated_at":"2025-03-15T12:21:04.000Z","dependencies_parsed_at":"2024-04-03T22:29:19.475Z","dependency_job_id":"3bf9fa79-2b6d-4207-ac9a-f3465c0d22ab","html_url":"https://github.com/reinfi/zf-dependency-injection","commit_stats":{"total_commits":306,"total_committers":15,"mean_commits":20.4,"dds":0.1633986928104575,"last_synced_commit":"30601d22902b60d20215885adb4e6735a87298f1"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reinfi%2Fzf-dependency-injection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reinfi%2Fzf-dependency-injection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reinfi%2Fzf-dependency-injection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reinfi%2Fzf-dependency-injection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reinfi","download_url":"https://codeload.github.com/reinfi/zf-dependency-injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252259305,"owners_count":21719659,"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","autowire","dependency-injection","laminas","yaml","zend-framework"],"created_at":"2024-11-13T01:51:03.864Z","updated_at":"2025-05-03T21:31:15.873Z","avatar_url":"https://github.com/reinfi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Configure dependency injection in Laminas or Mezzio using attributes (PHP 8.0+), yaml or autowiring.\n\nHeavily inspired by https://github.com/mikemix/mxdiModule.\n\n=======\n\n1. [Installation](#installation)\n2. [AutoWiring](#autowiring)\n3. [Attributes](#attributes)\n4. [Annotations (Deprecated)](#annotations)\n5. [YAML (Deprecated)](#yaml)\n6. [Caching](#caching)\n7. [PHPStan Extension](#phpstan-extension)\n8. [Console commands](#console-commands)\n\n### Installation\n\n1. Install with Composer: `composer require reinfi/zf-dependency-injection`.\n2. Enable the module via config in `application.config.php` under `modules` key:\n\n```php\n    return [\n        'modules' =\u003e [\n            'Reinfi\\DependencyInjection',\n            // other modules\n        ],\n    ];\n```\n### AutoWiring\nTo use autowiring for your service you need to specify the 'AutoWiringFactory' within the service manager configuration.\n```php\n'service_manager' =\u003e [\n    'factories' =\u003e [\n        YourService::class =\u003e \\Reinfi\\DependencyInjection\\Factory\\AutoWiringFactory::class,\n    ],\n]\n```\n\n#### Fallback AutoWiring\nIf you are migrating your existing project to use zend service manager and don't want to register all autowired services by hand, you can register the abstract `FallbackAutoWiringFactory` factory.\n\nPlease make sure that you don't use the fallback mechanism for everything. You should try to write and register explicit factories for your services.\n\n```php\n'service_manager' =\u003e [\n    'abstract_factories' =\u003e [\n        \\Reinfi\\DependencyInjection\\AbstractFactory\\FallbackAutoWiringFactory::class,\n    ],\n]\n```\n\n##### What can be autowired?\nEvery service registered within the service manager can be autowired.\nPlugins within the plugin manager can also be autowired. If you need to register another mapping you can simply add the following:\n```php\nPluginManagerResolver::addMapping('MyInterfaceClass', 'MyPluginManager');\n```\nIf your service needs the container as dependency this can also be autowired.\n##### Add another resolver\nIf you like to add another resolver you can simply add one through the configuration.\n```php\n'reinfi.dependencyInjection' =\u003e [\n    'autowire_resolver' =\u003e [\n        AnotherResolver::class,\n    ],\n]\n```\nIt needs to implement the ResolverInterface.\n\n### Attributes\nAttributes are activated if you are using a php version 8.0 or higher.\nTo use attributes for your dependencies you need to specify the 'InjectionFactory' within the service manager configuration.\n```php\n'service_manager' =\u003e [\n    'factories' =\u003e [\n        YourService::class =\u003e \\Reinfi\\DependencyInjection\\Factory\\InjectionFactory::class,\n    ],\n]\n```\nFollowing attributes are supported:\n* Inject (directly injects a service from the service locator)\n* InjectParent (must be used if you inject a service from a plugin manager)\n* InjectConfig (dot separated path to a config value, e.g. service_manager.factories)\n* InjectContainer (directly inject container interface)\n\nAlso in addition there a several annotations to inject from plugin managers.\n* InjectViewHelper\n* InjectFilter\n* InjectInputFilter\n* InjectValidator\n* InjectHydrator\n* InjectFormElement\n\nYou can either pass directly the required service name or if you need options you can pass them as following:\n```php\n#[InjectFormElement(name=\"Service\", options={\"field\": \"value\"}]\n```\n\nIf you need a doctrine repository there is also an attribute.\n* InjectDoctrineRepository\n\nIt is only constructor injection supported, if you need di from setters you need to use delegator factories.\n\nYou can add the attributes at properties or at the __construct method.\n\n```php\n#[Inject(\"Namespace\\MyService\")] \nprivate MyService $service;\n\npublic function __construct(MyService $service) \n{\n    $this-\u003eservice = $service;\n}\n```\nor\n```php\nprivate MyService $service;\n\n#[Inject(\"Namespace\\MyService\")] \npublic function __construct(MyService $service) \n{\n    $this-\u003eservice = $service;\n}\n```\nThe order is important and you should decide between constructor or property annotations.\n\n### Annotations (Deprecated)\n\u003e **Note**: Annotations are deprecated and will be removed in a future version. Please use Attributes instead, which provide the same functionality with a more modern syntax.\n\nTo use annotations for your dependencies you need to specify the 'InjectionFactory' within the service manager configuration.\n```php\n'service_manager' =\u003e [\n    'factories' =\u003e [\n        YourService::class =\u003e \\Reinfi\\DependencyInjection\\Factory\\InjectionFactory::class,\n    ],\n]\n```\nFollowing annotations are supported:\n* Inject (directly injects a service from the service locator)\n* InjectParent (must be used if you inject a service from a plugin manager)\n* InjectConfig (dot separated path to a config value, e.g. service_manager.factories)\n* InjectContainer (directly inject container interface)\n\nAlso in addition there a several annotations to inject from plugin managers.\n* InjectViewHelper\n* InjectFilter\n* InjectInputFilter\n* InjectValidator\n* InjectHydrator\n* InjectFormElement\n\nYou can either pass directly the required service name or if you need options you can pass them as following:\n```php\n@InjectFormElement(name=\"Service\", options={\"field\": \"value\"})\n```\n\nIf you need a doctrine repository there is also an annotation.\n* InjectDoctrineRepository\n\nIt is only constructor injection supported, if you need di from setters you need to use delegator factories.\n\nYou can add the annotations at properties or at the __construct method.\n\n```php\nuse Reinfi\\DependencyInjection\\Annotation\\Inject;\n\n/**\n * @Inject(\"Namespace\\MyService\")\n */\nprivate MyService $service;\n\n/**\n * @param MyService $service\n */\npublic function __construct(\n    MyService $service,\n) {\n    $this-\u003eservice = $service;\n}\n```\nor\n\n```php\nuse Reinfi\\DependencyInjection\\Annotation\\Inject;\n\n/**\n * @Inject(\"Namespace\\MyService\")\n */\npublic function __construct(MyService $service) \n{\n    $this-\u003eservice = $service;\n}\n```\nThe order is important and you should decide between constructor or property annotations.\n##### Adding own annotations\nIf you want to use your own annotation you just need to implement the AnnotationInterface.\n### YAML (Deprecated)\n\u003e **Note**: YAML configuration is deprecated and will be removed in a future version. Please use Attributes or Autowiring instead, which provide the same functionality with a more modern approach.\n\nYou can specify your dependencies within a yaml file.\n```yaml\nYourService:\n  - {type: Inject, value: AnotherService}\n```\nTo enable YAML usage you need to specify the following configuration\n```php\n'reinfi.dependencyInjection' =\u003e [\n    'extractor' =\u003e YamlExtractor::class,\n    'extractor_options =\u003e [\n        'file' =\u003e 'path_to_your_yaml_file.yml',\n    ],\n]\n```\n### Caching\nParsing mapping sources is very heavy. You *should* enable the cache on production servers.\nYou can set up caching easily with any custom or pre-existing PSR-16 cache adapter.\n\nYou can provide a string which will be resolved by the container. The factory must return a PSR-16 cache adapter.\n```php\n'reinfi.dependencyInjection' =\u003e [\n    'cache' =\u003e \\Laminas\\Cache\\Storage\\Adapter\\Memory::class,\n]\n```\nor you provide a factory for a cache adapter.\n```php\n'reinfi.dependencyInjection' =\u003e [\n    'cache' =\u003e function() {\n       return new \\Laminas\\Cache\\Psr\\SimpleCache\\SimpleCacheDecorator(\n           new \\Laminas\\Cache\\Storage\\Adapter\\Memory()\n       );\n    },\n]\n```\n\n### PHPStan Extension\nAs \"autowiring\" is always kind of magic this library ships with a PHPStan extension to solve that problem.\n\nIf you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!\n\n\u003cdetails\u003e\n    \u003csummary\u003eManual installation\u003c/summary\u003e\n\nIf you don't want to use `phpstan/extension-installer`, include phpstan-extension.neon in your project's PHPStan config:\n\n```\nincludes:\n    - vendor/reinfi/zf-dependency-injection/phpstan-extension.neon\n```\n\n\u003c/details\u003e\n\nThe extension requires to know your service manager to find all the classes you configured for autowiring.\n\nIf you do not provide it, the PHPStan extension will simply do nothing.\n\n```neon\nparameters:\n    reinfiLaminasDependencyInjection:\n       serviceManagerLoader: tests/service-manager.php\n```\n\nFor example, `tests/service-manager.php` would look something like this:\n\n```php\n$app = \\Laminas\\Mvc\\Application::init($config);\nreturn $app-\u003egetServiceManager();\n```\n\n\n### Console commands\n* Warmup script for Laminas: php bin/zf-dependency-injection-cache-warmup\n  Fills the cache with every injection required by a class.\n  This can either be via AutoWiringFactory or InjectionFactory.\n\n### FAQ\nFeel free to ask any questions or open own pull requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freinfi%2Fzf-dependency-injection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freinfi%2Fzf-dependency-injection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freinfi%2Fzf-dependency-injection/lists"}