bc26145463f98a5416a4d2d22ccbfccb095dd17c
[yaffs-website] / vendor / drupal / console-core / src / Command / Yaml / SplitCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Yaml\SplitCommand.
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\Dumper;
15 use Symfony\Component\Yaml\Parser;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Utils\NestedArray;
20
21 class SplitCommand extends Command
22 {
23     use CommandTrait;
24
25      /**
26       * @var NestedArray
27       */
28     protected $nestedArray;
29
30     /**
31      * SplitCommand constructor.
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:split')
44             ->setDescription($this->trans('commands.yaml.split.description'))
45             ->addArgument(
46                 'yaml-file',
47                 InputArgument::REQUIRED,
48                 $this->trans('commands.yaml.split.value.arguments.yaml-file')
49             )
50             ->addOption(
51                 'indent-level',
52                 false,
53                 InputOption::VALUE_REQUIRED,
54                 $this->trans('commands.yaml.split.options.indent-level')
55             )
56             ->addOption(
57                 'file-output-prefix',
58                 false,
59                 InputOption::VALUE_REQUIRED,
60                 $this->trans('commands.yaml.split.options.file-output-prefix')
61             )
62             ->addOption(
63                 'file-output-suffix',
64                 false,
65                 InputOption::VALUE_REQUIRED,
66                 $this->trans('commands.yaml.split.options.file-output-suffix')
67             )
68             ->addOption(
69                 'starting-key',
70                 false,
71                 InputOption::VALUE_REQUIRED,
72                 $this->trans('commands.yaml.split.options.starting-key')
73             )
74             ->addOption(
75                 'exclude-parents-key',
76                 false,
77                 InputOption::VALUE_NONE,
78                 $this->trans('commands.yaml.split.options.exclude-parents-key')
79             );
80     }
81
82     protected function execute(InputInterface $input, OutputInterface $output)
83     {
84         $io = new DrupalStyle($input, $output);
85
86         $yaml = new Parser();
87
88         $yaml_file = $input->getArgument('yaml-file');
89         $indent_level = $input->getOption('indent-level');
90         $exclude_parents_key = $input->getOption('exclude-parents-key');
91         $starting_key = $input->getOption('starting-key');
92         $file_output_prefix = $input->getOption('file-output-prefix');
93         $file_output_suffix = $input->getOption('file-output-suffix');
94
95         if ($exclude_parents_key == 1 || $exclude_parents_key == 'TRUE') {
96             $exclude_parents_key = true;
97         } else {
98             $exclude_parents_key = false;
99         }
100
101         try {
102             $yaml_file_parsed = $yaml->parse(file_get_contents($yaml_file));
103
104             if (empty($yaml_file_parsed)) {
105                 $io->error(
106                     sprintf(
107                         $this->trans('commands.yaml.merge.messages.wrong-parse'),
108                         $yaml_file
109                     )
110                 );
111
112                 return;
113             }
114         } catch (\Exception $e) {
115             $io->error(
116                 sprintf(
117                     '%s: %s',
118                     $this->trans('commands.yaml.merge.messages.error-parsing'),
119                     $e->getMessage()
120                 )
121             );
122
123             return;
124         }
125
126         if ($starting_key) {
127             $parents = explode(".", $starting_key);
128             if ($this->nestedArray->keyExists($yaml_file_parsed, $parents)) {
129                 $yaml_file_parsed = $this->nestedArray->getValue($yaml_file_parsed, $parents);
130             } else {
131                 $io->error($this->trans('commands.yaml.merge.messages.invalid-key'));
132             }
133
134             if ($indent_level == 0) {
135                 $yaml_split[$starting_key] = $yaml_file_parsed;
136             }
137         } else {
138             // Set minimum level to split
139             $indent_level = empty($indent_level) ? 1 : $indent_level;
140
141             $yaml_split = array();
142             $key_flatten = '';
143             $initial_level = 1;
144
145             $this->nestedArray->yamlSplitArray($yaml_file_parsed, $yaml_split, $indent_level, $key_flatten, $initial_level, $exclude_parents_key);
146         }
147
148         $this->writeSplittedFile($yaml_split, $file_output_prefix, $file_output_suffix, $io);
149     }
150
151     protected function writeSplittedFile($yaml_splitted, $file_output_prefix = '', $file_output_suffix = '', DrupalStyle $io)
152     {
153         $dumper = new Dumper();
154
155         $io->info($this->trans('commands.yaml.split.messages.generating-split'));
156
157         foreach ($yaml_splitted as $key => $value) {
158             if ($file_output_prefix) {
159                 $key = $file_output_prefix .  '.' . $key;
160             }
161
162             if ($file_output_suffix) {
163                 $key.= '.' . $file_output_suffix;
164             }
165             $filename = $key . '.yml';
166
167             try {
168                 $yaml = $dumper->dump($value, 10);
169             } catch (\Exception $e) {
170                 $io->error(
171                     sprintf(
172                         '%s: %s',
173                         $this->trans('commands.yaml.merge.messages.error-generating'),
174                         $e->getMessage()
175                     )
176                 );
177
178                 return;
179             }
180
181             try {
182                 file_put_contents($filename, $yaml);
183             } catch (\Exception $e) {
184                 $io->error(
185                     sprintf(
186                         '%s: %s',
187                         $this->trans('commands.yaml.merge.messages.error-writing'),
188                         $e->getMessage()
189                     )
190                 );
191
192                 return;
193             }
194
195             $io->success(
196                 sprintf(
197                     $this->trans('commands.yaml.split.messages.split-generated'),
198                     $filename
199                 )
200             );
201         }
202     }
203
204     /**
205      * {@inheritdoc}
206      */
207     protected function interact(InputInterface $input, OutputInterface $output)
208     {
209         $io = new DrupalStyle($input, $output);
210
211         $validator_filename = function ($value) use ($io) {
212             if (!strlen(trim($value)) || !is_file($value)) {
213                 $io->error($this->trans('commands.common.errors.invalid-file-path'));
214
215                 return false;
216             }
217
218             return $value;
219         };
220
221         // --yaml-left option
222         $yaml_file = $input->getArgument('yaml-file');
223         if (!$yaml_file) {
224             while (true) {
225                 $yaml_file = $io->ask(
226                     $this->trans('commands.yaml.diff.questions.yaml-left'),
227                     '',
228                     $validator_filename
229                 );
230
231                 if ($yaml_file) {
232                     break;
233                 }
234             }
235
236             $input->setArgument('yaml-file', $yaml_file);
237         }
238     }
239 }