Symfony injects a string instead of service

I have this Symfony 5.1 app where I’m trying to inject a custom cache adapter implementation but instead it injects a string t72YSNXSq7.

Here is my configuration:

cache.yaml

framework:
    cache:
        pools:
            stories:
                name: cache.stories
                adapter: RikudouDynamoDbCacheBundleCacheDynamoDbCacheAdapter

The bundle configuration is as follows (and it works in other Symfony apps):

services:
  rikudou.dynamo_cache.cache: # arguments defined in extension
    class: RikudouDynamoDbCacheDynamoDbCache
    arguments:
      $clock: ~
      $converter: '@rikudou.dynamo_cache.converter_registry'

  rikudou.dynamo_cache.adapter:
    class: RikudouDynamoDbCacheBundleCacheDynamoDbCacheAdapter
    arguments:
      - '@rikudou.dynamo_cache.cache'

My app services.yaml (the relevant part):

services:
    _instanceof:
        AppDependencyInjectionStoriesCacheInterface:
            tags:
                - app.stories_cache

Then I have this compiler pass which uses the cache.stories service for services tagged with app.stories_cache, e.g. those implementing AppDependencyInjectionStoriesCacheInterface:

    public function process(ContainerBuilder $container): void
    {
        $services = array_keys($container->findTaggedServiceIds('app.stories_cache'));
        foreach ($services as $service) {
            $service = $container->getDefinition($service);
            $reflection = new ReflectionClass($service->getClass());
            $constructor = $reflection->getConstructor();
            if ($constructor === null) {
                continue;
            }
            $arguments = $constructor->getParameters();
            foreach ($arguments as $argument) {
                if (!($type = $argument->getType())) {
                    continue;
                }
                if ((string) $type !== AdapterInterface::class) {
                    continue;
                }
                $service->setArgument('$' . $argument->getName(), new Reference('cache.stories'));
            }
        }
    }

And finally, this is the generated container file, which injects t72YSNXSq7 instead of an instance of RikudouDynamoDbCacheDynamoDbCache (indented for better readability):

        return $container->services['AppControllerAuthorController'] = new AppControllerAuthorController(
            new AppServiceAuthorReader(
                (
                    $container->privates['cache.stories'] ?? (
                        $container->privates['cache.stories'] = new RikudouDynamoDbCacheBundleCacheDynamoDbCacheAdapter('t72YSNXSq7') // the wrong line
                    )
                ),
                ($container->privates['AppServiceStorySearcher'] ?? $container->load('getStorySearcherService')), 
                ($container->privates['AppServiceDownloader'] ?? ($container->privates['AppServiceDownloader'] = new AppServiceDownloader()))),
            $a
        );

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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