Skip to content

[FrameworkBundle] Translator not longer available in CompilerPass  #31745

@alexander-schranz

Description

@alexander-schranz

Symfony version(s) affected: 4.3.0

Description

I tried to upgrade to symfony 4.3.0 but after that I did got a strange error when it tries to construct to translator service. Instead it give the FrameworkBundle Translator the $container it gives it an empty string so it ends up with a Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() must implement interface Psr\Container\ContainerInterface, string given

After debugging I found out that this come from: 31d7a09#diff-687bdbb38a4dc672ca2a79f23e764892R110. So the translator service seems not longer be available in a custom compiler pass. Maybe this can not be changed but if maybe other run into this error check your compilerpasses. And you need to register it with a lower priority, basically I think it should be avoided accessing of services in compilerpasses:

How to reproduce

Register a compilerpass with default priority which uses the translator service:

    public function process(ContainerBuilder $container)
    {
         $this->container->get('translator');
    }

Possible Solution

Currently not sure if this can be changed.

For the people run into this issues quickfix change the priority of the compilerpass

$container->addCompilerPass(new YourCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -33); // Translator should be available here

Additional context

Stack Trace
PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() must implement interface Psr\Container\ContainerInterface, string given in /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/framework-bundle/Translation/Translator.php:76
Stack trace:
#0 [internal function]: Symfony\Bundle\FrameworkBundle\Translation\Translator->__construct('', Object(Symfony\Component\Translation\Formatter\MessageFormatter), 'en', Array, Array)
#1 /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/dependency-injection/ContainerBuilder.php(1145): ReflectionClass->newInstanceArgs(Array)
#2 /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/dependency-injection/ContainerBuilder.php(612): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), Array, true, 'translator.defa...')
#3 /Users/alexand in /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/framework-bundle/Translation/Translator.php on line 76

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() must implement interface Psr\Container\ContainerInterface, string given in /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/framework-bundle/Translation/Translator.php:76
Stack trace:
#0 [internal function]: Symfony\Bundle\FrameworkBundle\Translation\Translator->__construct('', Object(Symfony\Component\Translation\Formatter\MessageFormatter), 'en', Array, Array)
#1 /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/dependency-injection/ContainerBuilder.php(1145): ReflectionClass->newInstanceArgs(Array)
#2 /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/dependency-injection/ContainerBuilder.php(612): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), Array, true, 'translator.defa...')
#3 /Users/alexand in /Users/alexanderschranz/Documents/Sulu/sulu-develop.localhost/vendor/symfony/framework-bundle/Translation/Translator.php on line 76

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions