Symfony 服务,tagged_locator 适用于 yaml 中的配置,但不适用于 php

Symfony service, tagged_locator works with the configuration in yaml but not in php

我在我的服务配置中使用 tagged_locator 一切都适用于 yaml 配置。 但是当我在 php 中进行配置时,它不再起作用了。 我服务的参数没有填写 (0 提供服务)

配置 Yaml

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    # Will tag automatically all service that implement the VoterInterface created
    _instanceof:
        App\Voter\CriterionInterface:
            tags:
                - 'app.post.voter.criterion'

    App\Voter\PostVoter:
        arguments:
            - !tagged_locator 'app.post.voter.criterion'

配置Php

return static function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();

    $services->defaults()
        ->autowire()
        ->autoconfigure();

    $services->load('App\', __DIR__.'/../src/')
        ->exclude(
            [
                __DIR__.'/../src/DependencyInjection/',
                __DIR__.'/../src/Entity/',
                __DIR__.'/../src/Kernel.php',
                __DIR__.'/../src/Tests/',
            ]
        );

    $services->load('App\Controller\', __DIR__.'/../src/Controller/')
        ->tag('controller.service_arguments');

    $services->instanceof(CriterionInterface::class)
        ->tag('app.post.voter.criterion');

    $services->set(PostVoter::class)
        ->args([tagged_locator('app.post.voter.criterion')]);

我的服务

$criteria 包含实现 Criterioninterface

的服务列表
class PostVoter extends Voter
{
    private $criteria;

    public function __construct(ServiceLocator $criteria)
    {
        $this->criteria = $criteria;
    }

    protected function supports(string $attribute, $subject)
    {
        dump($this->criteria->getProvidedServices()); //empty array in php config

        return $subject instanceof Entry && $this->criteria->has($attribute);
    }
}

dump($this->criteria->getProvidedServices());//空数组在php config

示例class使用了这个接口

class CanEdit implements CriterionInterface
{
    public function handle(Entry $post, User $user): bool
    {
        return $user === $post->getOwner();
    }
}

我的Kernelclass,这是原来class新建sf应用时的

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    protected function configureContainer(ContainerConfigurator $container): void
    {
        $container->import('../config/{packages}/*.yaml');
        $container->import('../config/{packages}/'.$this->environment.'/*.yaml');

        if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
            $container->import('../config/{services}.yaml');
            $container->import('../config/{services}_'.$this->environment.'.yaml');
        } elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
            (require $path)($container->withPath($path), $this);
        }
    }

    protected function configureRoutes(RoutingConfigurator $routes): void
    {
        $routes->import('../config/{routes}/'.$this->environment.'/*.yaml');
        $routes->import('../config/{routes}/*.yaml');

        if (is_file(\dirname(__DIR__).'/config/routes.yaml')) {
            $routes->import('../config/{routes}.yaml');
        } elseif (is_file($path = \dirname(__DIR__).'/config/routes.php')) {
            (require $path)($routes->withPath($path), $this);
        }
    }
}

感谢您的帮助

感谢 nicolas-grekas 纠正了我的错误:

配置器不可变:您需要存储和使用调用 defaults() 和 instanceof() 的 return 值:

    $services = $services->defaults()
        ->autowire()
        ->autoconfigure();

    $services = $services->instanceof(CriterionInterface::class)
        ->tag('app.post');

    $services->load('App\', __DIR__.'/../src/')
        ->exclude(
            [
                __DIR__.'/../src/DependencyInjection/',
                __DIR__.'/../src/Entity/',
                __DIR__.'/../src/Kernel.php',
                __DIR__.'/../src/Tests/',
            ]
        );

Instanceof 条件必须在 path-based 服务发现之前配置(第一次调用 load())