diff --git a/libraries/classes/Command/FixPoTwigCommand.php b/libraries/classes/Command/FixPoTwigCommand.php new file mode 100644 index 0000000000..d0cb221beb --- /dev/null +++ b/libraries/classes/Command/FixPoTwigCommand.php @@ -0,0 +1,94 @@ +setDescription('Fixes POT file for Twig templates'); + $this->setHelp( + 'The %command.name% command fixes the Twig file name and line number in the' + . ' POT file to match the Twig template and not the compiled Twig file.' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $replaceFile = file_get_contents(self::REPLACE_FILE); + if ($replaceFile === false) { + return Command::FAILURE; + } + + $replacements = json_decode($replaceFile, true); + if (! is_array($replacements)) { + return Command::FAILURE; + } + + /* Read pot file */ + $pot = file_get_contents(self::POT_FILE); + if ($pot === false) { + return Command::FAILURE; + } + + /* Do the replacements */ + $pot = preg_replace_callback( + '@(twig-templates[0-9a-f/]*.php):([0-9]*)@', + static function (array $matches) use ($replacements): string { + $filename = $matches[1]; + $line = intval($matches[2]); + $replace = $replacements[$filename]; + foreach ($replace[1] as $cacheLine => $result) { + if ($line >= $cacheLine) { + return $replace[0] . ':' . $result; + } + } + + return $replace[0] . ':0'; + }, + $pot + ); + if ($pot === null) { + return Command::FAILURE; + } + + /* Write fixed file */ + $handle = fopen(self::POT_FILE, 'w'); + if ($handle === false) { + return Command::FAILURE; + } + + if (fwrite($handle, $pot) === false) { + return Command::FAILURE; + } + + if (! fclose($handle)) { + return Command::FAILURE; + } + + return Command::SUCCESS; + } +} diff --git a/scripts/console b/scripts/console index 1893d86eac..52b05044b0 100755 --- a/scripts/console +++ b/scripts/console @@ -2,6 +2,7 @@ add(new CacheWarmupCommand()); $application->add(new TwigLintCommand()); $application->add(new SetVersionCommand()); $application->add(new WriteGitRevisionCommand()); +$application->add(new FixPoTwigCommand()); $application->run(); diff --git a/scripts/fix-po-twig b/scripts/fix-po-twig deleted file mode 100644 index 9c3d86770d..0000000000 --- a/scripts/fix-po-twig +++ /dev/null @@ -1,36 +0,0 @@ - $result) { - if ($line >= $cacheline) { - return $replace[0] . ':' . $result; - } - } - return $replace[0] . ':0'; - }, - $pot -); - -/* Write fixed file */ -$handle = fopen($potName, 'w'); -fwrite($handle, $pot); -fclose($handle); diff --git a/scripts/update-po b/scripts/update-po index b8432eed51..4c42eeafd0 100755 --- a/scripts/update-po +++ b/scripts/update-po @@ -38,7 +38,7 @@ xgettext \ `cat ./twig-templates/filesList` # Fixup filenames for Twig templates -php scripts/fix-po-twig +php scripts/console fix-po-twig # Remote twig templates rm -rf twig-templates/