5 * Contains \Drupal\Console\Core\Command\Yaml\MergeCommand.
8 namespace Drupal\Console\Core\Command\Yaml;
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;
20 class MergeCommand extends Command
24 protected function configure()
27 ->setName('yaml:merge')
28 ->setDescription($this->trans('commands.yaml.merge.description'))
31 InputArgument::REQUIRED,
32 $this->trans('commands.yaml.merge.arguments.yaml-destination')
36 InputArgument::IS_ARRAY,
37 $this->trans('commands.yaml.merge.arguments.yaml-files')
41 protected function execute(InputInterface $input, OutputInterface $output)
43 $io = new DrupalStyle($input, $output);
46 $dumper = new Dumper();
49 $yaml_destination = realpath($input->getArgument('yaml-destination'));
50 $yaml_files = $input->getArgument('yaml-files');
52 if (!$yaml_destination) {
53 $fs = new Filesystem();
55 $fs->touch($input->getArgument('yaml-destination'));
56 $yaml_destination = realpath($input->getArgument('yaml-destination'));
57 } catch (\Exception $e) {
61 $this->trans('commands.yaml.merge.messages.error-writing'),
70 if (count($yaml_files) < 2) {
71 $io->error($this->trans('commands.yaml.merge.messages.two-files-required'));
76 foreach ($yaml_files as $yaml_file) {
78 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
79 } catch (\Exception $e) {
83 $this->trans('commands.yaml.merge.messages.error-parsing'),
91 if (empty($yaml_parsed)) {
94 $this->trans('commands.yaml.merge.messages.wrong-parse'),
101 $final_yaml = array_replace_recursive($final_yaml, $yaml_parsed);
105 $yaml = $dumper->dump($final_yaml, 10);
106 } catch (\Exception $e) {
110 $this->trans('commands.yaml.merge.messages.error-generating'),
119 file_put_contents($yaml_destination, $yaml);
120 } catch (\Exception $e) {
124 $this->trans('commands.yaml.merge.messages.error-writing'),
134 $this->trans('commands.yaml.merge.messages.merged'),
143 protected function interact(InputInterface $input, OutputInterface $output)
145 $io = new DrupalStyle($input, $output);
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'));
157 // --yaml-destination option
158 $yaml_destination = $input->getArgument('yaml-destination');
159 if (!$yaml_destination) {
161 $yaml_destination = $io->ask(
162 $this->trans('commands.yaml.merge.questions.yaml-destination'),
167 if ($yaml_destination) {
172 $input->setArgument('yaml-destination', $yaml_destination);
175 $yaml_files = $input->getArgument('yaml-files');
180 // Set the string key based on among files provided
181 if (count($yaml_files) >= 2) {
182 $questionStringKey = 'commands.yaml.merge.questions.other-file';
184 $questionStringKey = 'commands.yaml.merge.questions.file';
187 $yaml_file = $io->ask(
188 $this->trans($questionStringKey),
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'));
194 } elseif (!empty($file) && in_array($file, $yaml_files)) {
196 sprintf($this->trans('commands.yaml.merge.questions.file-already-added'), $file)
200 } elseif ($file == '') {
208 if ($yaml_file && !is_string($yaml_file)) {
213 $yaml_files[] = realpath($yaml_file);
217 $input->setArgument('yaml-files', $yaml_files);