1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Incenteev\TranslationCheckerBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Translation\Catalogue\TargetOperation; |
11
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
12
|
|
|
|
13
|
|
|
class CompareCommand extends ContainerAwareCommand |
14
|
|
|
{ |
15
|
13 |
|
protected function configure() |
16
|
|
|
{ |
17
|
13 |
|
$this->setName('incenteev:translation:compare') |
18
|
13 |
|
->setDescription('Compares two translation catalogues to ensure they are in sync') |
19
|
13 |
|
->addArgument('locale', InputArgument::REQUIRED, 'The locale being checked') |
20
|
13 |
|
->addArgument('source', InputArgument::OPTIONAL, 'The source of the comparison', 'en') |
21
|
13 |
|
->addOption('obsolete-only', null, InputOption::VALUE_NONE, 'Report only obsolete keys') |
22
|
13 |
|
->addOption('domain', 'd', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The domains being compared') |
23
|
13 |
|
->setHelp(<<<EOF |
24
|
|
|
The <info>%command.name%</info> command compares 2 translation catalogues to |
25
|
|
|
ensure they are in sync. If there is missing keys or obsolete keys in the target |
26
|
|
|
catalogue, the command will exit with an error code. |
27
|
|
|
|
28
|
|
|
When running the command in verbose mode, the translation keys will also be displayed. |
29
|
|
|
<info>php %command.full_name% fr --verbose</info> |
30
|
|
|
|
31
|
|
|
The <info>--domain</info> option allows to restrict the domains being checked. |
32
|
|
|
It can be specified several times to check several domains. If the option is not passed, |
33
|
|
|
all domains will be compared. |
34
|
|
|
|
35
|
|
|
The <info>--obsolete-only</info> option allows to check only obsolete keys, and ignore any |
36
|
13 |
|
missing keys. |
37
|
|
|
EOF |
38
|
|
|
); |
39
|
13 |
|
} |
40
|
|
|
|
41
|
13 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
42
|
|
|
{ |
43
|
13 |
|
$loader = $this->getContainer()->get('incenteev_translation_checker.exposing_translator'); |
44
|
|
|
|
45
|
13 |
|
$sourceCatalogue = $loader->getCatalogue($input->getArgument('source')); |
46
|
13 |
|
$comparedCatalogue = $loader->getCatalogue($input->getArgument('locale')); |
47
|
|
|
|
48
|
|
|
// Change the locale of the catalogue as DiffOperation requires operating on a single locale |
49
|
13 |
|
$catalogue = new MessageCatalogue($sourceCatalogue->getLocale(), $comparedCatalogue->all()); |
50
|
|
|
|
51
|
13 |
|
$operation = new TargetOperation($catalogue, $sourceCatalogue); |
52
|
|
|
|
53
|
13 |
|
$domains = $operation->getDomains(); |
54
|
13 |
|
$restrictedDomains = $input->getOption('domain'); |
55
|
13 |
|
if (!empty($restrictedDomains)) { |
56
|
4 |
|
$domains = array_intersect($domains, $restrictedDomains); |
57
|
4 |
|
$output->writeln(sprintf('<comment>Checking the domains %s</comment>', implode(', ', $domains))); |
58
|
|
|
} |
59
|
|
|
|
60
|
13 |
|
$checkMissing = !$input->getOption('obsolete-only'); |
61
|
|
|
|
62
|
13 |
|
$valid = true; |
63
|
|
|
|
64
|
13 |
|
foreach ($domains as $domain) { |
65
|
13 |
|
$missingMessages = $checkMissing ? $operation->getNewMessages($domain) : array(); |
66
|
13 |
|
$obsoleteMessages = $operation->getObsoleteMessages($domain); |
67
|
13 |
|
$written = false; |
68
|
|
|
|
69
|
13 |
View Code Duplication |
if (!empty($missingMessages)) { |
|
|
|
|
70
|
5 |
|
$valid = false; |
71
|
5 |
|
$written = true; |
72
|
5 |
|
$output->writeln(sprintf('<comment>%s</comment> messages are missing in the <info>%s</info> domain', count($missingMessages), $domain)); |
73
|
|
|
|
74
|
5 |
|
$this->displayMessages($output, $missingMessages); |
75
|
|
|
} |
76
|
|
|
|
77
|
13 |
View Code Duplication |
if (!empty($obsoleteMessages)) { |
|
|
|
|
78
|
5 |
|
$valid = false; |
79
|
5 |
|
$written = true; |
80
|
5 |
|
$output->writeln(sprintf('<comment>%s</comment> messages are obsolete in the <info>%s</info> domain', count($obsoleteMessages), $domain)); |
81
|
|
|
|
82
|
5 |
|
$this->displayMessages($output, $obsoleteMessages); |
83
|
|
|
} |
84
|
|
|
|
85
|
13 |
|
if ($written) { |
86
|
13 |
|
$output->writeln(''); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
13 |
|
if ($valid) { |
91
|
5 |
|
$output->writeln(sprintf( |
92
|
5 |
|
'<info>The <comment>%s</comment> catalogue is in sync with the <comment>%s</comment> one.</info>', |
93
|
5 |
|
$input->getArgument('locale'), |
94
|
5 |
|
$input->getArgument('source') |
95
|
|
|
)); |
96
|
|
|
|
97
|
5 |
|
return 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
8 |
|
return 1; |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
private function displayMessages(OutputInterface $output, array $messages) |
104
|
|
|
{ |
105
|
8 |
|
if ($output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { |
106
|
6 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
foreach ($messages as $key => $translation) { |
110
|
2 |
|
$output->writeln(' '.$key); |
111
|
|
|
} |
112
|
2 |
|
$output->writeln(''); |
113
|
2 |
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.