定制 setono/composite-compiler-pass 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

setono/composite-compiler-pass

最新稳定版本:v1.2.0

Composer 安装命令:

composer require setono/composite-compiler-pass

包简介

A composite compiler pass for Symfony to make it even easier to create composite services

README 文档

README

Latest Version Software License Build Status Code Coverage Mutation testing

When you use composite services you find yourself writing the same compiler pass over and over again. This library will give you the compiler pass you need so you don't have to think about it again.

Installation

composer require setono/composite-compiler-pass

Usage

Let's presume you have a composite service like this:

<?php
final class YourCompositeService
{
    /**
     * @var list<object>
     */
    private array $taggedServices = [];

    public function add(object $taggedService): void
    {
        $this->taggedServices[] = $taggedService;
    }
}

that has the service id your_bundle.your_service, and you want to add services that are tagged with tag automatically to this composite service.

In your bundle class you do this:

<?php

use Setono\CompositeCompilerPass\CompositeCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class YourBundle extends Bundle
{
    public function build(ContainerBuilder $container): void
    {
        $container->addCompilerPass(new CompositeCompilerPass('your_bundle.your_service', 'tag'));
    }
}

NOTICE You can even define your tagged services with a priority and they will be automatically sorted before being added to the composite service. This is thanks to the Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait used under the hood.

Composite service included

The library also comes with a small abstract class you can base your composite service on if you wish. The class is named CompositeService and you can find it here.

Here is an example of how you can use it:

<?php

use Setono\CompositeCompilerPass\CompositeService;

/**
 * @property list<TaggedServiceInterface> $services
 *
 * @extends CompositeService<TaggedServiceInterface>
 */
final class ConcreteCompositeService extends CompositeService implements TaggedServiceInterface
{
    public function process(): void
    {
        foreach ($this->services as $service) {
            // Both your IDE, Psalm, and PHPStan knows that $service is an instance of TaggedServiceInterface
        }
    }
}

interface TaggedServiceInterface
{

}

统计信息

  • 总下载量: 80.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 16
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-18