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