ppokatilo/magic-injection-bundle

神奇地注入服务依赖

v1.0.3 2014-11-15 04:14 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:02:58 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

此Symfony2扩展包提供了一种将依赖注入到服务中的方式。依赖将不会在构造函数中可用。要使用它,您需要完成以下步骤

  1. 将标签magic_injection.injectable_service添加到您希望注入的服务中。该标签接受一个名为type的可选参数,您可以使用它来分组注入服务。
  2. 将标签magic_injection.injection_target添加到应接收注入服务的服务中。
  3. 最后,使用带有可选的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();
      }
    }