vendor/symfony/translation/Command/XliffLintCommand.php line 112

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Translation\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\CI\GithubActionReporter;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Completion\CompletionInput;
  15. use Symfony\Component\Console\Completion\CompletionSuggestions;
  16. use Symfony\Component\Console\Exception\RuntimeException;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Style\SymfonyStyle;
  22. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  23. use Symfony\Component\Translation\Util\XliffUtils;
  24. /**
  25.  * Validates XLIFF files syntax and outputs encountered errors.
  26.  *
  27.  * @author GrĂ©goire Pineau <lyrixx@lyrixx.info>
  28.  * @author Robin Chalas <robin.chalas@gmail.com>
  29.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  30.  */
  31. #[AsCommand(name'lint:xliff'description'Lint an XLIFF file and outputs encountered errors')]
  32. class XliffLintCommand extends Command
  33. {
  34.     private string $format;
  35.     private bool $displayCorrectFiles;
  36.     private ?\Closure $directoryIteratorProvider;
  37.     private ?\Closure $isReadableProvider;
  38.     private bool $requireStrictFileNames;
  39.     public function __construct(string $name null, callable $directoryIteratorProvider null, callable $isReadableProvider nullbool $requireStrictFileNames true)
  40.     {
  41.         parent::__construct($name);
  42.         $this->directoryIteratorProvider null === $directoryIteratorProvider null $directoryIteratorProvider(...);
  43.         $this->isReadableProvider null === $isReadableProvider null $isReadableProvider(...);
  44.         $this->requireStrictFileNames $requireStrictFileNames;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     protected function configure()
  50.     {
  51.         $this
  52.             ->addArgument('filename'InputArgument::IS_ARRAY'A file, a directory or "-" for reading from STDIN')
  53.             ->addOption('format'nullInputOption::VALUE_REQUIRED'The output format')
  54.             ->setHelp(<<<EOF
  55. The <info>%command.name%</info> command lints an XLIFF file and outputs to STDOUT
  56. the first encountered syntax error.
  57. You can validates XLIFF contents passed from STDIN:
  58.   <info>cat filename | php %command.full_name% -</info>
  59. You can also validate the syntax of a file:
  60.   <info>php %command.full_name% filename</info>
  61. Or of a whole directory:
  62.   <info>php %command.full_name% dirname</info>
  63.   <info>php %command.full_name% dirname --format=json</info>
  64. EOF
  65.             )
  66.         ;
  67.     }
  68.     protected function execute(InputInterface $inputOutputInterface $output): int
  69.     {
  70.         $io = new SymfonyStyle($input$output);
  71.         $filenames = (array) $input->getArgument('filename');
  72.         $this->format $input->getOption('format') ?? (GithubActionReporter::isGithubActionEnvironment() ? 'github' 'txt');
  73.         $this->displayCorrectFiles $output->isVerbose();
  74.         if (['-'] === $filenames) {
  75.             return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
  76.         }
  77.         if (!$filenames) {
  78.             throw new RuntimeException('Please provide a filename or pipe file content to STDIN.');
  79.         }
  80.         $filesInfo = [];
  81.         foreach ($filenames as $filename) {
  82.             if (!$this->isReadable($filename)) {
  83.                 throw new RuntimeException(sprintf('File or directory "%s" is not readable.'$filename));
  84.             }
  85.             foreach ($this->getFiles($filename) as $file) {
  86.                 $filesInfo[] = $this->validate(file_get_contents($file), $file);
  87.             }
  88.         }
  89.         return $this->display($io$filesInfo);
  90.     }
  91.     private function validate(string $contentstring $file null): array
  92.     {
  93.         $errors = [];
  94.         // Avoid: Warning DOMDocument::loadXML(): Empty string supplied as input
  95.         if ('' === trim($content)) {
  96.             return ['file' => $file'valid' => true];
  97.         }
  98.         $internal libxml_use_internal_errors(true);
  99.         $document = new \DOMDocument();
  100.         $document->loadXML($content);
  101.         if (null !== $targetLanguage $this->getTargetLanguageFromFile($document)) {
  102.             $normalizedLocalePattern sprintf('(%s|%s)'preg_quote($targetLanguage'/'), preg_quote(str_replace('-''_'$targetLanguage), '/'));
  103.             // strict file names require translation files to be named '____.locale.xlf'
  104.             // otherwise, both '____.locale.xlf' and 'locale.____.xlf' are allowed
  105.             // also, the regexp matching must be case-insensitive, as defined for 'target-language' values
  106.             // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#target-language
  107.             $expectedFilenamePattern $this->requireStrictFileNames sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/'$normalizedLocalePattern) : sprintf('/^(?:.*\.(?i:%s)|(?i:%s)\..*)\.(?:xlf|xliff)/'$normalizedLocalePattern$normalizedLocalePattern);
  108.             if (=== preg_match($expectedFilenamePatternbasename($file))) {
  109.                 $errors[] = [
  110.                     'line' => -1,
  111.                     'column' => -1,
  112.                     'message' => sprintf('There is a mismatch between the language included in the file name ("%s") and the "%s" value used in the "target-language" attribute of the file.'basename($file), $targetLanguage),
  113.                 ];
  114.             }
  115.         }
  116.         foreach (XliffUtils::validateSchema($document) as $xmlError) {
  117.             $errors[] = [
  118.                 'line' => $xmlError['line'],
  119.                 'column' => $xmlError['column'],
  120.                 'message' => $xmlError['message'],
  121.             ];
  122.         }
  123.         libxml_clear_errors();
  124.         libxml_use_internal_errors($internal);
  125.         return ['file' => $file'valid' => === \count($errors), 'messages' => $errors];
  126.     }
  127.     private function display(SymfonyStyle $io, array $files)
  128.     {
  129.         return match ($this->format) {
  130.             'txt' => $this->displayTxt($io$files),
  131.             'json' => $this->displayJson($io$files),
  132.             'github' => $this->displayTxt($io$filestrue),
  133.             default => throw new InvalidArgumentException(sprintf('The format "%s" is not supported.'$this->format)),
  134.         };
  135.     }
  136.     private function displayTxt(SymfonyStyle $io, array $filesInfobool $errorAsGithubAnnotations false)
  137.     {
  138.         $countFiles \count($filesInfo);
  139.         $erroredFiles 0;
  140.         $githubReporter $errorAsGithubAnnotations ? new GithubActionReporter($io) : null;
  141.         foreach ($filesInfo as $info) {
  142.             if ($info['valid'] && $this->displayCorrectFiles) {
  143.                 $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s'$info['file']) : ''));
  144.             } elseif (!$info['valid']) {
  145.                 ++$erroredFiles;
  146.                 $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s'$info['file']) : ''));
  147.                 $io->listing(array_map(function ($error) use ($info$githubReporter) {
  148.                     // general document errors have a '-1' line number
  149.                     $line = -=== $error['line'] ? null $error['line'];
  150.                     $githubReporter?->error($error['message'], $info['file'], $linenull !== $line $error['column'] : null);
  151.                     return null === $line $error['message'] : sprintf('Line %d, Column %d: %s'$line$error['column'], $error['message']);
  152.                 }, $info['messages']));
  153.             }
  154.         }
  155.         if (=== $erroredFiles) {
  156.             $io->success(sprintf('All %d XLIFF files contain valid syntax.'$countFiles));
  157.         } else {
  158.             $io->warning(sprintf('%d XLIFF files have valid syntax and %d contain errors.'$countFiles $erroredFiles$erroredFiles));
  159.         }
  160.         return min($erroredFiles1);
  161.     }
  162.     private function displayJson(SymfonyStyle $io, array $filesInfo)
  163.     {
  164.         $errors 0;
  165.         array_walk($filesInfo, function (&$v) use (&$errors) {
  166.             $v['file'] = (string) $v['file'];
  167.             if (!$v['valid']) {
  168.                 ++$errors;
  169.             }
  170.         });
  171.         $io->writeln(json_encode($filesInfo\JSON_PRETTY_PRINT \JSON_UNESCAPED_SLASHES));
  172.         return min($errors1);
  173.     }
  174.     private function getFiles(string $fileOrDirectory)
  175.     {
  176.         if (is_file($fileOrDirectory)) {
  177.             yield new \SplFileInfo($fileOrDirectory);
  178.             return;
  179.         }
  180.         foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
  181.             if (!\in_array($file->getExtension(), ['xlf''xliff'])) {
  182.                 continue;
  183.             }
  184.             yield $file;
  185.         }
  186.     }
  187.     private function getDirectoryIterator(string $directory)
  188.     {
  189.         $default = function ($directory) {
  190.             return new \RecursiveIteratorIterator(
  191.                 new \RecursiveDirectoryIterator($directory\FilesystemIterator::SKIP_DOTS \FilesystemIterator::FOLLOW_SYMLINKS),
  192.                 \RecursiveIteratorIterator::LEAVES_ONLY
  193.             );
  194.         };
  195.         if (null !== $this->directoryIteratorProvider) {
  196.             return ($this->directoryIteratorProvider)($directory$default);
  197.         }
  198.         return $default($directory);
  199.     }
  200.     private function isReadable(string $fileOrDirectory)
  201.     {
  202.         $default = function ($fileOrDirectory) {
  203.             return is_readable($fileOrDirectory);
  204.         };
  205.         if (null !== $this->isReadableProvider) {
  206.             return ($this->isReadableProvider)($fileOrDirectory$default);
  207.         }
  208.         return $default($fileOrDirectory);
  209.     }
  210.     private function getTargetLanguageFromFile(\DOMDocument $xliffContents): ?string
  211.     {
  212.         foreach ($xliffContents->getElementsByTagName('file')[0]->attributes ?? [] as $attribute) {
  213.             if ('target-language' === $attribute->nodeName) {
  214.                 return $attribute->nodeValue;
  215.             }
  216.         }
  217.         return null;
  218.     }
  219.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  220.     {
  221.         if ($input->mustSuggestOptionValuesFor('format')) {
  222.             $suggestions->suggestValues(['txt''json''github']);
  223.         }
  224.     }
  225. }