hendricha/tagpass

为添加标记服务到其他服务提供简单的symfony编译器通过

dev-master 2015-09-12 16:40 UTC

This package is not auto-updated.

Last update: 2024-09-18 08:19:42 UTC


README

通过方法调用,在容器编译时将Symfony服务引用添加到其他服务定义的简单解决方案。

基本上,这个库包含了两个可配置的Symfony "使用标记服务"示例代码版本。

TagPass

这允许您收集具有特定标记的所有服务,并将它们添加到特定服务或服务的调用中。请参见下面的示例

namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use HendrichA\TagPassLibrary\TagPass;

class AppBundle extends Bundle
{
  public function build(ContainerBuilder $container)
  {
    parent::build($container);

    //This compiler pass will add all services tagged with foo to the
    //repository_of_all_things_foo service, by the addFoo method call.
    $fooPass = new TagPass('foo');
    $container->addCompilerPass(
      $fooPass->addServiceIdsTo('repository_of_all_things_foo', 'addFoo')
    );

    //You can add the tagged services to multiple service definitions too.
    $barPass = new TagPass('bar');
    $container->addCompilerPass(
      $barPass
        ->addServiceIdsTo('repository_of_all_things_bar', 'addBar')
        ->addServiceIdsTo('repository_of_all_the_things', 'addSomething')
      );
   }
}

TargetedTagPass

此编译器可以通过标记本身定义的其他服务定义添加标记服务。

//SomeBundle.php
//...
$fooPass = new TargetedTagPass('form_extension', addExtension);
$container->addCompilerPass(fooPass);
#services.yml
services:
  login_form:
    class: LoginForm

  #This service will be added to login_form, because the tag specifies it
  login_extension:
    class: LoginExtension
    tags:
      - { name: form_extension, service: login_form }


  foo_form:
    class: Form
    tags:
      - { name: very_extensible_form }

  bar_form:
    class: Form
    tags:
      - { name: very_extensible_form }

  #This service will be added to both of the above service, since they are both
  #have the "very_extensible_form" tag
  foobar_extension:
    class: VeryExtensibleFormExtension
    tags:
      - { name: form_extension, tag: very_extensible_form }

##测试此存储库包含编译器的功能测试,可以使用phpunit执行。因为它们需要某些Symfony类存在,所以应该从您的symfony应用程序根目录中调用phpunit

$ phpunit -c app/phpunit.xml vendor/hendricha/tagpass/HendrichA/TagPassLibrary/Tests