Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Command / Yaml / DiffCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Yaml\DiffCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Yaml;
9
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;
19
20 class DiffCommand extends Command
21 {
22     use CommandTrait;
23
24     /**
25      * @var NestedArray
26      */
27     protected $nestedArray;
28
29     /**
30      * DiffCommand constructor.
31      *
32      * @param NestedArray $nestedArray
33      */
34     public function __construct(NestedArray $nestedArray)
35     {
36         $this->nestedArray = $nestedArray;
37         parent::__construct();
38     }
39
40     protected function configure()
41     {
42         $this
43             ->setName('yaml:diff')
44             ->setDescription($this->trans('commands.yaml.diff.description'))
45             ->addArgument(
46                 'yaml-left',
47                 InputArgument::REQUIRED,
48                 $this->trans('commands.yaml.diff.arguments.yaml-left')
49             )
50             ->addArgument(
51                 'yaml-right',
52                 InputArgument::REQUIRED,
53                 $this->trans('commands.yaml.diff.arguments.yaml-right')
54             )
55             ->addOption(
56                 'stats',
57                 false,
58                 InputOption::VALUE_NONE,
59                 $this->trans('commands.yaml.diff.options.stats')
60             )
61             ->addOption(
62                 'negate',
63                 false,
64                 InputOption::VALUE_NONE,
65                 $this->trans('commands.yaml.diff.options.negate')
66             )
67             ->addOption(
68                 'limit',
69                 null,
70                 InputOption::VALUE_OPTIONAL,
71                 $this->trans('commands.yaml.diff.options.limit')
72             )
73             ->addOption(
74                 'offset',
75                 null,
76                 InputOption::VALUE_OPTIONAL,
77                 $this->trans('commands.yaml.diff.options.offset')
78             );
79     }
80
81     protected function execute(InputInterface $input, OutputInterface $output)
82     {
83         $io = new DrupalStyle($input, $output);
84
85         $yaml = new Parser();
86
87         $yaml_left = $input->getArgument('yaml-left');
88         $yaml_right = $input->getArgument('yaml-right');
89
90         $stats = $input->getOption('stats');
91
92         $negate = $input->getOption('negate');
93
94         $limit = $input->getOption('limit');
95         $offset = $input->getOption('offset');
96
97         if ($negate == 1 || $negate == 'TRUE') {
98             $negate = true;
99         } else {
100             $negate = false;
101         }
102
103         try {
104             $yamlLeftParsed = $yaml->parse(file_get_contents($yaml_left));
105
106             if (empty($yamlLeftParsed)) {
107                 $io->error(
108                     sprintf(
109                         $this->trans('commands.yaml.merge.messages.wrong-parse'),
110                         $yaml_left
111                     )
112                 );
113             }
114
115             $yamlRightParsed = $yaml->parse(file_get_contents($yaml_right));
116
117             if (empty($yamlRightParsed)) {
118                 $io->error(
119                     sprintf(
120                         $this->trans('commands.yaml.merge.messages.wrong-parse'),
121                         $yaml_right
122                     )
123                 );
124             }
125         } catch (\Exception $e) {
126             $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
127
128             return;
129         }
130
131         $statistics = ['total' => 0, 'equal'=> 0 , 'diff' => 0];
132         $diff = $this->nestedArray->arrayDiff($yamlLeftParsed, $yamlRightParsed, $negate, $statistics);
133         print_r($diff);
134
135         if ($stats) {
136             $io->info(
137                 sprintf(
138                     $this->trans('commands.yaml.diff.messages.total'),
139                     $statistics['total']
140                 )
141             );
142
143             $io->info(
144                 sprintf(
145                     $this->trans('commands.yaml.diff.messages.diff'),
146                     $statistics['diff']
147                 )
148             );
149
150             $io->info(
151                 sprintf(
152                     $this->trans('commands.yaml.diff.messages.equal'),
153                     $statistics['equal']
154                 )
155             );
156
157             return;
158         }
159         // FLAT YAML file to display full yaml to be used with command yaml:update:key or yaml:update:value
160         $diffFlatten = [];
161         $keyFlatten = '';
162         $this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);
163
164         if ($limit !== null) {
165             if (!$offset) {
166                 $offset = 0;
167             }
168             $diffFlatten = array_slice($diffFlatten, $offset, $limit);
169         }
170
171         $tableHeader = [
172             $this->trans('commands.yaml.diff.messages.key'),
173             $this->trans('commands.yaml.diff.messages.value'),
174         ];
175
176         $tableRows = [];
177         foreach ($diffFlatten as $yamlKey => $yamlValue) {
178             $tableRows[] = [
179                 $yamlKey,
180                 $yamlValue
181             ];
182             print $yamlKey . "\n";
183             print $yamlValue . "\n";
184         }
185
186         $io->table($tableHeader, $tableRows, 'compact');
187     }
188
189     /**
190      * {@inheritdoc}
191      */
192     protected function interact(InputInterface $input, OutputInterface $output)
193     {
194         $io = new DrupalStyle($input, $output);
195
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'));
199
200                 return false;
201             }
202
203             return $value;
204         };
205
206         // --yaml-left option
207         $yaml_left = $input->getArgument('yaml-left');
208         if (!$yaml_left) {
209             while (true) {
210                 $yaml_left = $io->ask(
211                     $this->trans('commands.yaml.diff.questions.yaml-left'),
212                     null,
213                     $validator_filename
214                 );
215
216                 if ($yaml_left) {
217                     break;
218                 }
219             }
220
221             $input->setArgument('yaml-left', $yaml_left);
222         }
223
224         // --yaml-right option
225         $yaml_right = $input->getArgument('yaml-right');
226         if (!$yaml_right) {
227             while (true) {
228                 $yaml_right = $io->ask(
229                     $this->trans('commands.yaml.diff.questions.yaml-right'),
230                     null,
231                     $validator_filename
232                 );
233
234                 if ($yaml_right) {
235                     break;
236                 }
237             }
238
239             $input->setArgument('yaml-right', $yaml_right);
240         }
241     }
242 }