|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Translation\Bundle\Command; |
| 5 | + |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Input\InputArgument; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 11 | +use Symfony\Component\Finder\Finder; |
| 12 | +use Translation\Bundle\Catalogue\CatalogueCounter; |
| 13 | +use Translation\Bundle\Catalogue\CatalogueFetcher; |
| 14 | +use Translation\Bundle\Model\Configuration; |
| 15 | +use Translation\Bundle\Service\ConfigurationManager; |
| 16 | +use Translation\Bundle\Service\Importer; |
| 17 | + |
| 18 | +final class CheckCommand extends Command |
| 19 | +{ |
| 20 | + protected static $defaultName = 'translation:check'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var ConfigurationManager |
| 24 | + */ |
| 25 | + private $configurationManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var CatalogueFetcher |
| 29 | + */ |
| 30 | + private $catalogueFetcher; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Importer |
| 34 | + */ |
| 35 | + private $importer; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var CatalogueCounter |
| 39 | + */ |
| 40 | + private $catalogueCounter; |
| 41 | + |
| 42 | + public function __construct( |
| 43 | + ConfigurationManager $configurationManager, |
| 44 | + CatalogueFetcher $catalogueFetcher, |
| 45 | + Importer $importer, |
| 46 | + CatalogueCounter $catalogueCounter |
| 47 | + ) { |
| 48 | + parent::__construct(); |
| 49 | + |
| 50 | + $this->configurationManager = $configurationManager; |
| 51 | + $this->catalogueFetcher = $catalogueFetcher; |
| 52 | + $this->importer = $importer; |
| 53 | + $this->catalogueCounter = $catalogueCounter; |
| 54 | + } |
| 55 | + |
| 56 | + protected function configure(): void |
| 57 | + { |
| 58 | + $this |
| 59 | + ->setName(self::$defaultName) |
| 60 | + ->setDescription('Check that all translations for a given locale are extracted.') |
| 61 | + ->addArgument('locale', InputArgument::REQUIRED, 'The locale to check') |
| 62 | + ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') |
| 63 | + ; |
| 64 | + } |
| 65 | + |
| 66 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 67 | + { |
| 68 | + $config = $this->configurationManager->getConfiguration($input->getArgument('configuration')); |
| 69 | + |
| 70 | + $locale = $input->getArgument('locale'); |
| 71 | + |
| 72 | + $catalogues = $this->catalogueFetcher->getCatalogues($config, [$locale]); |
| 73 | + $finder = $this->getConfiguredFinder($config); |
| 74 | + |
| 75 | + $result = $this->importer->extractToCatalogues( |
| 76 | + $finder, |
| 77 | + $catalogues, |
| 78 | + [ |
| 79 | + 'blacklist_domains' => $config->getBlacklistDomains(), |
| 80 | + 'whitelist_domains' => $config->getWhitelistDomains(), |
| 81 | + 'project_root' => $config->getProjectRoot(), |
| 82 | + ] |
| 83 | + ); |
| 84 | + |
| 85 | + $definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]); |
| 86 | + $definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]); |
| 87 | + |
| 88 | + $newMessages = $definedAfter - $definedBefore; |
| 89 | + |
| 90 | + $io = new SymfonyStyle($input, $output); |
| 91 | + |
| 92 | + if ($newMessages > 0) { |
| 93 | + $io->error(sprintf('%d new message(s) have been found, run bin/console translation:extract', $newMessages)); |
| 94 | + |
| 95 | + return 1; |
| 96 | + } |
| 97 | + |
| 98 | + $io->success('No new translation messages'); |
| 99 | + |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + private function getConfiguredFinder(Configuration $config): Finder |
| 104 | + { |
| 105 | + $finder = new Finder(); |
| 106 | + $finder->in($config->getDirs()); |
| 107 | + |
| 108 | + foreach ($config->getExcludedDirs() as $exclude) { |
| 109 | + $finder->notPath($exclude); |
| 110 | + } |
| 111 | + |
| 112 | + foreach ($config->getExcludedNames() as $exclude) { |
| 113 | + $finder->notName($exclude); |
| 114 | + } |
| 115 | + |
| 116 | + return $finder; |
| 117 | + } |
| 118 | +} |
0 commit comments