5 * Contains \Drupal\Console\Core\Command\Yaml\DiffCommand.
8 namespace Drupal\Console\Core\Command\Yaml;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Yaml\Parser;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Core\Utils\NestedArray;
20 class DiffCommand extends Command
27 protected $nestedArray;
30 * DiffCommand constructor.
32 * @param NestedArray $nestedArray
34 public function __construct(NestedArray $nestedArray)
36 $this->nestedArray = $nestedArray;
37 parent::__construct();
40 protected function configure()
43 ->setName('yaml:diff')
44 ->setDescription($this->trans('commands.yaml.diff.description'))
47 InputArgument::REQUIRED,
48 $this->trans('commands.yaml.diff.arguments.yaml-left')
52 InputArgument::REQUIRED,
53 $this->trans('commands.yaml.diff.arguments.yaml-right')
58 InputOption::VALUE_NONE,
59 $this->trans('commands.yaml.diff.options.stats')
64 InputOption::VALUE_NONE,
65 $this->trans('commands.yaml.diff.options.negate')
70 InputOption::VALUE_OPTIONAL,
71 $this->trans('commands.yaml.diff.options.limit')
76 InputOption::VALUE_OPTIONAL,
77 $this->trans('commands.yaml.diff.options.offset')
81 protected function execute(InputInterface $input, OutputInterface $output)
83 $io = new DrupalStyle($input, $output);
87 $yaml_left = $input->getArgument('yaml-left');
88 $yaml_right = $input->getArgument('yaml-right');
90 $stats = $input->getOption('stats');
92 $negate = $input->getOption('negate');
94 $limit = $input->getOption('limit');
95 $offset = $input->getOption('offset');
97 if ($negate == 1 || $negate == 'TRUE') {
104 $yamlLeftParsed = $yaml->parse(file_get_contents($yaml_left));
106 if (empty($yamlLeftParsed)) {
109 $this->trans('commands.yaml.merge.messages.wrong-parse'),
115 $yamlRightParsed = $yaml->parse(file_get_contents($yaml_right));
117 if (empty($yamlRightParsed)) {
120 $this->trans('commands.yaml.merge.messages.wrong-parse'),
125 } catch (\Exception $e) {
126 $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
131 $statistics = ['total' => 0, 'equal'=> 0 , 'diff' => 0];
132 $diff = $this->nestedArray->arrayDiff($yamlLeftParsed, $yamlRightParsed, $negate, $statistics);
138 $this->trans('commands.yaml.diff.messages.total'),
145 $this->trans('commands.yaml.diff.messages.diff'),
152 $this->trans('commands.yaml.diff.messages.equal'),
159 // FLAT YAML file to display full yaml to be used with command yaml:update:key or yaml:update:value
162 $this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);
164 if ($limit !== null) {
168 $diffFlatten = array_slice($diffFlatten, $offset, $limit);
172 $this->trans('commands.yaml.diff.messages.key'),
173 $this->trans('commands.yaml.diff.messages.value'),
177 foreach ($diffFlatten as $yamlKey => $yamlValue) {
182 print $yamlKey . "\n";
183 print $yamlValue . "\n";
186 $io->table($tableHeader, $tableRows, 'compact');
192 protected function interact(InputInterface $input, OutputInterface $output)
194 $io = new DrupalStyle($input, $output);
196 $validator_filename = function ($value) use ($io) {
197 if (!strlen(trim($value)) || !is_file($value)) {
198 $io->error($this->trans('commands.common.errors.invalid-file-path'));
206 // --yaml-left option
207 $yaml_left = $input->getArgument('yaml-left');
210 $yaml_left = $io->ask(
211 $this->trans('commands.yaml.diff.questions.yaml-left'),
221 $input->setArgument('yaml-left', $yaml_left);
224 // --yaml-right option
225 $yaml_right = $input->getArgument('yaml-right');
228 $yaml_right = $io->ask(
229 $this->trans('commands.yaml.diff.questions.yaml-right'),
239 $input->setArgument('yaml-right', $yaml_right);