示例#1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $yaml = new Parser();
     $yaml_left = $input->getArgument('yaml-left');
     $yaml_right = $input->getArgument('yaml-right');
     $stats = $input->getOption('stats');
     $negate = $input->getOption('negate');
     $limit = $input->getOption('limit');
     $offset = $input->getOption('offset');
     if ($negate == 1 || $negate == 'TRUE') {
         $negate = true;
     } else {
         $negate = false;
     }
     try {
         $yamlLeftParsed = $yaml->parse(file_get_contents($yaml_left));
         if (empty($yamlLeftParsed)) {
             $io->error(sprintf($this->trans('commands.yaml.merge.messages.wrong-parse'), $yaml_left));
         }
         $yamlRightParsed = $yaml->parse(file_get_contents($yaml_right));
         if (empty($yamlRightParsed)) {
             $io->error(sprintf($this->trans('commands.yaml.merge.messages.wrong-parse'), $yaml_right));
         }
     } catch (\Exception $e) {
         $io->error($this->trans('commands.yaml.merge.messages.error-parsing') . ': ' . $e->getMessage());
         return;
     }
     $statistics = ['total' => 0, 'equal' => 0, 'diff' => 0];
     /*        print_r($yamlLeftParsed);
             print_r($yamlRightParsed);*/
     $diff = $this->nestedArray->arrayDiff($yamlLeftParsed, $yamlRightParsed, $negate, $statistics);
     print_r($diff);
     if ($stats) {
         $io->info(sprintf($this->trans('commands.yaml.diff.messages.total'), $statistics['total']));
         $io->info(sprintf($this->trans('commands.yaml.diff.messages.diff'), $statistics['diff']));
         $io->info(sprintf($this->trans('commands.yaml.diff.messages.equal'), $statistics['equal']));
         return;
     }
     // FLAT YAML file to display full yaml to be used with command yaml:update:key or yaml:update:value
     $diffFlatten = array();
     $keyFlatten = '';
     $this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);
     if ($limit !== null) {
         if (!$offset) {
             $offset = 0;
         }
         $diffFlatten = array_slice($diffFlatten, $offset, $limit);
     }
     $tableHeader = [$this->trans('commands.yaml.diff.messages.key'), $this->trans('commands.yaml.diff.messages.value')];
     $tableRows = [];
     foreach ($diffFlatten as $yamlKey => $yamlValue) {
         $tableRows[] = [$yamlKey, $yamlValue];
         print $yamlKey . "\n";
         print $yamlValue . "\n";
     }
     $io->table($tableHeader, $tableRows, 'compact');
 }
 protected function determinePendingTranslation($io, $language = null, $languages, $fileFilter)
 {
     $englishFilesFinder = new Finder();
     $yaml = new Parser();
     $statistics = [];
     $englishDirectory = $this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, 'en');
     $englishFiles = $englishFilesFinder->files()->name('*.yml')->in($englishDirectory);
     $pendingTranslations = 0;
     foreach ($englishFiles as $file) {
         $resource = $englishDirectory . '/' . $file->getBasename();
         $filename = $file->getBasename('.yml');
         if ($fileFilter && $fileFilter != $file->getBasename()) {
             continue;
         }
         try {
             $englishFileParsed = $yaml->parse(file_get_contents($resource));
         } catch (ParseException $e) {
             $io->error($filename . '.yml: ' . $e->getMessage());
             continue;
         }
         foreach ($languages as $langCode => $languageName) {
             $languageDir = $this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, $langCode);
             if (isset($language) && $langCode != $language) {
                 continue;
             }
             $resourceTranslated = $languageDir . '/' . $file->getBasename();
             if (!file_exists($resourceTranslated)) {
                 $io->info(sprintf($this->trans('commands.translation.pending.messages.missing-file'), $languageName, $file->getBasename()));
                 continue;
             }
             try {
                 $resourceTranslatedParsed = $yaml->parse(file_get_contents($resourceTranslated));
             } catch (ParseException $e) {
                 $io->error($resourceTranslated . ':' . $e->getMessage());
             }
             $diffStatistics = ['total' => 0, 'equal' => 0, 'diff' => 0];
             $diff = $this->nestedArray->arrayDiff($englishFileParsed, $resourceTranslatedParsed, true, $diffStatistics);
             if (!empty($diff)) {
                 $diffFlatten = array();
                 $keyFlatten = '';
                 $this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);
                 $tableHeader = [$this->trans('commands.yaml.diff.messages.key'), $this->trans('commands.yaml.diff.messages.value')];
                 $tableRows = [];
                 foreach ($diffFlatten as $yamlKey => $yamlValue) {
                     if ($this->isYamlKey($yamlValue)) {
                         unset($diffFlatten[$yamlKey]);
                     } else {
                         $tableRows[] = [$yamlKey, $yamlValue];
                     }
                 }
                 if (count($diffFlatten)) {
                     $io->writeln(sprintf($this->trans('commands.translation.pending.messages.pending-translations'), $languageName, $file->getBasename()));
                     $io->table($tableHeader, $tableRows, 'compact');
                     $pendingTranslations += count($diffFlatten);
                 }
             }
         }
     }
     return $pendingTranslations;
 }
 protected function calculateStats($io, $language = null, $languages)
 {
     $englishFilesFinder = new Finder();
     $yaml = new Parser();
     $statistics = [];
     $englishDirectory = $this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, 'en');
     $englishFiles = $englishFilesFinder->files()->name('*.yml')->in($englishDirectory);
     foreach ($englishFiles as $file) {
         $resource = $englishDirectory . '/' . $file->getBasename();
         $filename = $file->getBasename('.yml');
         try {
             $englishFileParsed = $yaml->parse(file_get_contents($resource));
         } catch (ParseException $e) {
             $io->error($filename . '.yml: ' . $e->getMessage());
             continue;
         }
         foreach ($languages as $langCode => $languageName) {
             $languageDir = $this->consoleRoot . sprintf(DRUPAL_CONSOLE_LANGUAGE, $langCode);
             //don't show that language if that repo isn't present
             if (!file_exists($languageDir)) {
                 continue;
             }
             if (isset($language) && $langCode != $language) {
                 continue;
             }
             if (!isset($statistics[$langCode])) {
                 $statistics[$langCode] = ['total' => 0, 'equal' => 0, 'diff' => 0];
             }
             $resourceTranslated = $languageDir . '/' . $file->getBasename();
             if (!file_exists($resourceTranslated)) {
                 $englishFileEntries = count($englishFileParsed, COUNT_RECURSIVE);
                 $statistics[$langCode]['total'] += $englishFileEntries;
                 $statistics[$langCode]['equal'] += $englishFileEntries;
                 continue;
             }
             try {
                 $resourceTranslatedParsed = $yaml->parse(file_get_contents($resourceTranslated));
             } catch (ParseException $e) {
                 $io->error($resourceTranslated . ':' . $e->getMessage());
             }
             $diffStatistics = ['total' => 0, 'equal' => 0, 'diff' => 0];
             $diff = $this->nestedArray->arrayDiff($englishFileParsed, $resourceTranslatedParsed, true, $diffStatistics);
             $yamlKeys = 0;
             if (!empty($diff)) {
                 $diffFlatten = array();
                 $keyFlatten = '';
                 $this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);
                 // Determine how many yaml keys were returned as values
                 foreach ($diffFlatten as $yamlKey => $yamlValue) {
                     if ($this->isYamlKey($yamlValue)) {
                         $yamlKeys++;
                     }
                 }
             }
             $statistics[$langCode]['total'] += $diffStatistics['total'];
             $statistics[$langCode]['equal'] += $diffStatistics['equal'] - $yamlKeys;
             $statistics[$langCode]['diff'] += $diffStatistics['diff'] + $yamlKeys;
         }
     }
     $stats = [];
     foreach ($statistics as $langCode => $statistic) {
         $index = isset($languages[$langCode]) ? $languages[$langCode] : $langCode;
         $stats[] = ['name' => $index, 'percentage' => round($statistic['diff'] / $statistic['total'] * 100, 2), 'iso' => $langCode];
     }
     usort($stats, function ($a, $b) {
         return $a["percentage"] < $b["percentage"];
     });
     return $stats;
 }