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

I use the tagged_locator in my service configuration
Everything works with a yaml configuration.
But when I do the config in php, it doesn’t work anymore.
The parameter of my service is not filled
(0 providedServices)

Configuration 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
    AppController:
        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:
        AppVoterCriterionInterface:
            tags:
                - 'app.post.voter.criterion'

    AppVoterPostVoter:
        arguments:
            - !tagged_locator 'app.post.voter.criterion'

Configuration 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('AppController', __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')]);

My service

$criteria contains list of services implementing 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());//empty array in php config

Example class used this interface

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

Thank you for your helping

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *