| 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 |  |  |                 $missingTranslations = array_diff(array_keys($dictionary1), array_keys($dictionary2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 foreach ($missingTranslations as $missingTranslation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     $errors[] = 'Missing translation for key ' . $missingTranslation . ' for language ' . $lang2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $output->writeln("\n", OutputInterface::VERBOSITY_VERY_VERBOSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($errors as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $output->writeln($error, OutputInterface::VERBOSITY_VERY_VERBOSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $output->writeln("\n"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $output->writeln('<comment>' . count($errors) . ' errors found</comment>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return count($errors); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  |