ppokatilo / magic-injection-bundle
神奇地注入服务依赖
v1.0.3
2014-11-15 04:14 UTC
Requires
- php: >=5.3.2
- doctrine/annotations: 1.*
- symfony/config: ~2.0
- symfony/dependency-injection: ~2.0
- symfony/http-kernel: ~2.0
This package is not auto-updated.
Last update: 2024-09-24 03:02:58 UTC
README
此Symfony2扩展包提供了一种将依赖注入到服务中的方式。依赖将不会在构造函数中可用。要使用它,您需要完成以下步骤
- 将标签
magic_injection.injectable_service
添加到您希望注入的服务中。该标签接受一个名为type
的可选参数,您可以使用它来分组注入服务。 - 将标签
magic_injection.injection_target
添加到应接收注入服务的服务中。 - 最后,使用带有可选的
type
参数的MagicInjection
注解注释属性,该参数引用一个可注入服务的组。
示例用法
- services.yml
services: service.that.will.be.injected: class: Service\MyServiceA tags: - { name: magic_injection.injectable_service, type: my_services } service.that.has.a.dependency: class: Service\MyServiceB tags: - { name: magic_injection.injection_target }
- MyServiceB.php
class MyServiceB { /** * @MagicInjection(type="my_services") * @var \Service\MyServiceA */ private $myServiceA; public function __construct() { assert($this->myServiceA === null); } public function foo() { $this->myServiceA->bar(); } }