db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console-core / src / Command / Yaml / MergeCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Yaml\MergeCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Yaml;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Yaml\Dumper;
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 Symfony\Component\Filesystem\Filesystem;
19
20 class MergeCommand extends Command
21 {
22     use CommandTrait;
23
24     protected function configure()
25     {
26         $this
27             ->setName('yaml:merge')
28             ->setDescription($this->trans('commands.yaml.merge.description'))
29             ->addArgument(
30                 'yaml-destination',
31                 InputArgument::REQUIRED,
32                 $this->trans('commands.yaml.merge.arguments.yaml-destination')
33             )
34             ->addArgument(
35                 'yaml-files',
36                 InputArgument::IS_ARRAY,
37                 $this->trans('commands.yaml.merge.arguments.yaml-files')
38             );
39     }
40
41     protected function execute(InputInterface $input, OutputInterface $output)
42     {
43         $io = new DrupalStyle($input, $output);
44
45         $yaml = new Parser();
46         $dumper = new Dumper();
47
48         $final_yaml = [];
49         $yaml_destination = realpath($input->getArgument('yaml-destination'));
50         $yaml_files = $input->getArgument('yaml-files');
51
52         if (!$yaml_destination) {
53             $fs = new Filesystem();
54             try {
55                 $fs->touch($input->getArgument('yaml-destination'));
56                 $yaml_destination = realpath($input->getArgument('yaml-destination'));
57             } catch (\Exception $e) {
58                 $io->error(
59                     sprintf(
60                         '%s: %s',
61                         $this->trans('commands.yaml.merge.messages.error-writing'),
62                         $e->getMessage()
63                     )
64                 );
65
66                 return;
67             }
68         }
69
70         if (count($yaml_files) < 2) {
71             $io->error($this->trans('commands.yaml.merge.messages.two-files-required'));
72
73             return;
74         }
75
76         foreach ($yaml_files as $yaml_file) {
77             try {
78                 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
79             } catch (\Exception $e) {
80                 $io->error(
81                     sprintf(
82                         '%s: %s',
83                         $this->trans('commands.yaml.merge.messages.error-parsing'),
84                         $e->getMessage()
85                     )
86                 );
87
88                 return;
89             }
90
91             if (empty($yaml_parsed)) {
92                 $io->error(
93                     sprintf(
94                         $this->trans('commands.yaml.merge.messages.wrong-parse'),
95                         $yaml_file
96                     )
97                 );
98             }
99
100             // Merge arrays
101             $final_yaml = array_replace_recursive($final_yaml, $yaml_parsed);
102         }
103
104         try {
105             $yaml = $dumper->dump($final_yaml, 10);
106         } catch (\Exception $e) {
107             $io->error(
108                 sprintf(
109                     '%s: %s',
110                     $this->trans('commands.yaml.merge.messages.error-generating'),
111                     $e->getMessage()
112                 )
113             );
114
115             return;
116         }
117
118         try {
119             file_put_contents($yaml_destination, $yaml);
120         } catch (\Exception $e) {
121             $io->error(
122                 sprintf(
123                     '%s: %s',
124                     $this->trans('commands.yaml.merge.messages.error-writing'),
125                     $e->getMessage()
126                 )
127             );
128
129             return;
130         }
131
132         $io->success(
133             sprintf(
134                 $this->trans('commands.yaml.merge.messages.merged'),
135                 $yaml_destination
136             )
137         );
138     }
139
140     /**
141      * {@inheritdoc}
142      */
143     protected function interact(InputInterface $input, OutputInterface $output)
144     {
145         $io = new DrupalStyle($input, $output);
146
147         $validator_filename = function ($value) use ($io) {
148             if (!strlen(trim($value)) || !is_file($value)) {
149                 $io->error($this->trans('commands.common.errors.invalid-file-path'));
150
151                 return false;
152             }
153
154             return $value;
155         };
156
157         // --yaml-destination option
158         $yaml_destination = $input->getArgument('yaml-destination');
159         if (!$yaml_destination) {
160             while (true) {
161                 $yaml_destination = $io->ask(
162                     $this->trans('commands.yaml.merge.questions.yaml-destination'),
163                     '',
164                     $validator_filename
165                 );
166
167                 if ($yaml_destination) {
168                     break;
169                 }
170             }
171
172             $input->setArgument('yaml-destination', $yaml_destination);
173         }
174
175         $yaml_files = $input->getArgument('yaml-files');
176         if (!$yaml_files) {
177             $yaml_files = [];
178
179             while (true) {
180                 // Set the string key based on among files provided
181                 if (count($yaml_files) >= 2) {
182                     $questionStringKey = 'commands.yaml.merge.questions.other-file';
183                 } else {
184                     $questionStringKey = 'commands.yaml.merge.questions.file';
185                 }
186
187                 $yaml_file = $io->ask(
188                     $this->trans($questionStringKey),
189                     '',
190                     function ($file) use ($yaml_files, $io) {
191                         if (count($yaml_files) < 2 && empty($file)) {
192                             $io->error($this->trans('commands.yaml.merge.questions.invalid-file'));
193                             return false;
194                         } elseif (!empty($file) && in_array($file, $yaml_files)) {
195                             $io->error(
196                                 sprintf($this->trans('commands.yaml.merge.questions.file-already-added'), $file)
197                             );
198
199                             return false;
200                         } elseif ($file == '') {
201                             return true;
202                         } else {
203                             return $file;
204                         }
205                     }
206                 );
207
208                 if ($yaml_file && !is_string($yaml_file)) {
209                     break;
210                 }
211
212                 if ($yaml_file) {
213                     $yaml_files[] = realpath($yaml_file);
214                 }
215             }
216
217             $input->setArgument('yaml-files', $yaml_files);
218         }
219     }
220 }