protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $yaml = new Parser();
     $dumper = new Dumper();
     $yaml_file = $input->getArgument('yaml-file');
     $yaml_key = $input->getArgument('yaml-key');
     try {
         $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
     } catch (\Exception $e) {
         $io->error($this->trans('commands.yaml.merge.messages.error-parsing') . ': ' . $e->getMessage());
         return;
     }
     if (empty($yaml_parsed)) {
         $io->info(sprintf($this->trans('commands.yaml.merge.messages.wrong-parse'), $yaml_file));
     }
     $parents = explode(".", $yaml_key);
     $this->nestedArray->unsetValue($yaml_parsed, $parents);
     try {
         $yaml = $dumper->dump($yaml_parsed, 10);
     } catch (\Exception $e) {
         $io->error($this->trans('commands.yaml.merge.messages.error-generating') . ': ' . $e->getMessage());
         return;
     }
     try {
         file_put_contents($yaml_file, $yaml);
     } catch (\Exception $e) {
         $io->error($this->trans('commands.yaml.merge.messages.error-writing') . ': ' . $e->getMessage());
         return;
     }
     $io->info(sprintf($this->trans('commands.yaml.unset.value.messages.unset'), $yaml_file));
 }
Beispiel #2
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 execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $yaml = new Parser();
     $yaml_file = $input->getArgument('yaml-file');
     $indent_level = $input->getOption('indent-level');
     $exclude_parents_key = $input->getOption('exclude-parents-key');
     $starting_key = $input->getOption('starting-key');
     $file_output_prefix = $input->getOption('file-output-prefix');
     $file_output_suffix = $input->getOption('file-output-suffix');
     if ($exclude_parents_key == 1 || $exclude_parents_key == 'TRUE') {
         $exclude_parents_key = true;
     } else {
         $exclude_parents_key = false;
     }
     try {
         $yaml_file_parsed = $yaml->parse(file_get_contents($yaml_file));
         if (empty($yaml_file_parsed)) {
             $io->error(sprintf($this->trans('commands.yaml.merge.messages.wrong-parse'), $yaml_file));
             return;
         }
     } catch (\Exception $e) {
         $io->error(sprintf('%s: %s', $this->trans('commands.yaml.merge.messages.error-parsing'), $e->getMessage()));
         return;
     }
     if ($starting_key) {
         $parents = explode(".", $starting_key);
         if ($this->nestedArray->keyExists($yaml_file_parsed, $parents)) {
             $yaml_file_parsed = $this->nestedArray->getValue($yaml_file_parsed, $parents);
         } else {
             $io->error($this->trans('commands.yaml.merge.messages.invalid-key'));
         }
         if ($indent_level == 0) {
             $yaml_split[$starting_key] = $yaml_file_parsed;
         }
     } else {
         // Set minimum level to split
         $indent_level = empty($indent_level) ? 1 : $indent_level;
         $yaml_split = array();
         $key_flatten = '';
         $initial_level = 1;
         $this->nestedArray->yamlSplitArray($yaml_file_parsed, $yaml_split, $indent_level, $key_flatten, $initial_level, $exclude_parents_key);
     }
     $this->writeSplittedFile($yaml_split, $file_output_prefix, $file_output_suffix, $io);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $yaml = new Parser();
     $yaml_file = $input->getArgument('yaml-file');
     $yaml_key = $input->getArgument('yaml-key');
     try {
         $yaml_parsed = $yaml->parse(file_get_contents($yaml_file), true);
     } catch (\Exception $e) {
         $io->error($this->trans('commands.yaml.merge.messages.error-parsing') . ': ' . $e->getMessage());
         return;
     }
     if (empty($yaml_parsed)) {
         $io->info(sprintf($this->trans('commands.yaml.merge.messages.wrong-parse'), $yaml_file));
     } else {
         $key_exists = null;
         $parents = explode(".", $yaml_key);
         $yaml_value = $this->nestedArray->getValue($yaml_parsed, $parents, $key_exists);
         if (!$key_exists) {
             $io->info(sprintf($this->trans('commands.yaml.get.value.messages.invalid-key'), $yaml_key, $yaml_file));
         }
         $output->writeln($yaml_value);
     }
 }
 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;
 }