1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Efabrica\TranslationsAutomatization\Command\CheckDictionaries; |
4
|
|
|
|
5
|
|
|
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException; |
6
|
|
|
use Efabrica\TranslationsAutomatization\Tokenizer\Token; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class CheckDictionariesCommand extends Command |
16
|
|
|
{ |
17
|
|
|
protected function configure() |
18
|
|
|
{ |
19
|
|
|
$this->setName('check:dictionaries') |
20
|
|
|
->setDescription('Compare all dictionaries for all languages if there are all translation keys in all of them') |
21
|
|
|
->addArgument('config', InputArgument::REQUIRED, 'Path to config file. Instance of ' . CheckDictionariesConfig::class . ' have to be returned') |
22
|
|
|
->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format --params="a=b&c=d"'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
26
|
|
|
{ |
27
|
|
|
if (!is_file($input->getArgument('config'))) { |
28
|
|
|
throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist'); |
29
|
|
|
} |
30
|
|
|
parse_str($input->getOption('params'), $params); |
31
|
|
|
extract($params); |
32
|
|
|
|
33
|
|
|
$checkDictionariesConfig = require_once $input->getArgument('config'); |
34
|
|
|
if ($checkDictionariesConfig instanceof InvalidConfigInstanceReturnedException) { |
35
|
|
|
throw $checkDictionariesConfig; |
36
|
|
|
} |
37
|
|
|
if (!$checkDictionariesConfig instanceof CheckDictionariesConfig) { |
38
|
|
|
throw new InvalidConfigInstanceReturnedException('"' . (is_object($checkDictionariesConfig) ? get_class($checkDictionariesConfig) : $checkDictionariesConfig) . '" is not instance of ' . CheckDictionariesConfig::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$output->writeln(''); |
42
|
|
|
$output->writeln('Loading dictionaries...'); |
43
|
|
|
$dictionaries = $checkDictionariesConfig->load(); |
44
|
|
|
|
45
|
|
|
$errors = []; |
46
|
|
|
foreach ($dictionaries as $lang1 => $dictionary1) { |
47
|
|
|
foreach ($dictionaries as $lang2 => $dictionary2) { |
48
|
|
|
if ($lang1 === $lang2) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
$missingTranslations = array_diff(array_keys($dictionary1), array_keys($dictionary2)); |
52
|
|
|
foreach ($missingTranslations as $missingTranslation) { |
53
|
|
|
$errors[] = 'Missing translation for key ' . $missingTranslation . ' for language ' . $lang2; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$output->writeln('', OutputInterface::VERBOSITY_VERY_VERBOSE); |
59
|
|
|
foreach (array_unique($errors) as $error) { |
60
|
|
|
$output->writeln($error, OutputInterface::VERBOSITY_VERY_VERBOSE); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$output->writeln(''); |
64
|
|
|
$output->writeln('<comment>' . count($errors) . ' errors found</comment>'); |
65
|
|
|
return count($errors); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|