From 6f21a400a8d1a8e644a759042ef59d68ebf8469a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 16 Dec 2024 16:09:00 +0100 Subject: [PATCH] require the writer to implement getFormats() in the translation:extract --- Command/TranslationUpdateCommand.php | 4 +++ .../Compiler/TranslationUpdateCommandPass.php | 31 +++++++++++++++++++ FrameworkBundle.php | 2 ++ 3 files changed, 37 insertions(+) create mode 100644 DependencyInjection/Compiler/TranslationUpdateCommandPass.php diff --git a/Command/TranslationUpdateCommand.php b/Command/TranslationUpdateCommand.php index 91e7e3349..0ffe6a949 100644 --- a/Command/TranslationUpdateCommand.php +++ b/Command/TranslationUpdateCommand.php @@ -64,6 +64,10 @@ public function __construct(TranslationWriterInterface $writer, TranslationReade { parent::__construct(); + if (!method_exists($writer, 'getFormats')) { + throw new \InvalidArgumentException(sprintf('The writer class "%s" does not implement the "getFormats()" method.', $writer::class)); + } + $this->writer = $writer; $this->reader = $reader; $this->extractor = $extractor; diff --git a/DependencyInjection/Compiler/TranslationUpdateCommandPass.php b/DependencyInjection/Compiler/TranslationUpdateCommandPass.php new file mode 100644 index 000000000..7542191d0 --- /dev/null +++ b/DependencyInjection/Compiler/TranslationUpdateCommandPass.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +class TranslationUpdateCommandPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + if (!$container->hasDefinition('console.command.translation_extract')) { + return; + } + + $translationWriterClass = $container->getParameterBag()->resolveValue($container->findDefinition('translation.writer')->getClass()); + + if (!method_exists($translationWriterClass, 'getFormats')) { + $container->removeDefinition('console.command.translation_extract'); + } + } +} diff --git a/FrameworkBundle.php b/FrameworkBundle.php index 0da10da9d..c371d10db 100644 --- a/FrameworkBundle.php +++ b/FrameworkBundle.php @@ -20,6 +20,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationUpdateCommandPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\VirtualRequestStackPass; use Symfony\Component\Cache\Adapter\ApcuAdapter; @@ -193,6 +194,7 @@ public function build(ContainerBuilder $container) // must be registered after MonologBundle's LoggerChannelPass $container->addCompilerPass(new ErrorLoggerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32); $container->addCompilerPass(new VirtualRequestStackPass()); + $container->addCompilerPass(new TranslationUpdateCommandPass(), PassConfig::TYPE_BEFORE_REMOVING); if ($container->getParameter('kernel.debug')) { $container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);