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