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;
19 class MergeCommand extends Command
23 protected function configure()
26 ->setName('yaml:merge')
27 ->setDescription($this->trans('commands.yaml.merge.description'))
30 InputArgument::REQUIRED,
31 $this->trans('commands.yaml.merge.arguments.yaml-destination')
35 InputArgument::IS_ARRAY,
36 $this->trans('commands.yaml.merge.arguments.yaml-files')
40 protected function execute(InputInterface $input, OutputInterface $output)
42 $io = new DrupalStyle($input, $output);
45 $dumper = new Dumper();
47 $final_yaml = array();
48 $yaml_destination = realpath($input->getArgument('yaml-destination'));
49 $yaml_files = $input->getArgument('yaml-files');
51 if (count($yaml_files) < 2) {
52 $io->error($this->trans('commands.yaml.merge.messages.two-files-required'));
57 foreach ($yaml_files as $yaml_file) {
59 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
60 } catch (\Exception $e) {
64 $this->trans('commands.yaml.merge.messages.error-parsing'),
72 if (empty($yaml_parsed)) {
75 $this->trans('commands.yaml.merge.messages.wrong-parse'),
82 $final_yaml = array_replace_recursive($final_yaml, $yaml_parsed);
86 $yaml = $dumper->dump($final_yaml, 10);
87 } catch (\Exception $e) {
91 $this->trans('commands.yaml.merge.messages.error-generating'),
100 file_put_contents($yaml_destination, $yaml);
101 } catch (\Exception $e) {
105 $this->trans('commands.yaml.merge.messages.error-writing'),
115 $this->trans('commands.yaml.merge.messages.merged'),
124 protected function interact(InputInterface $input, OutputInterface $output)
126 $io = new DrupalStyle($input, $output);
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'));
138 // --yaml-destination option
139 $yaml_destination = $input->getArgument('yaml-destination');
140 if (!$yaml_destination) {
142 $yaml_destination = $io->ask(
143 $this->trans('commands.yaml.merge.questions.yaml-destination'),
148 if ($yaml_destination) {
153 $input->setArgument('yaml-destination', $yaml_destination);
156 $yaml_files = $input->getArgument('yaml-files');
158 $yaml_files = array();
161 // Set the string key based on among files provided
162 if (count($yaml_files) >= 2) {
163 $questionStringKey = 'commands.yaml.merge.questions.other-file';
165 $questionStringKey = 'commands.yaml.merge.questions.file';
168 $yaml_file = $io->ask(
169 $this->trans($questionStringKey),
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'));
175 } elseif (!empty($file) && in_array($file, $yaml_files)) {
177 sprintf($this->trans('commands.yaml.merge.questions.file-already-added'), $file)
181 } elseif ($file == '') {
189 if ($yaml_file && !is_string($yaml_file)) {
194 $yaml_files[] = realpath($yaml_file);
198 $input->setArgument('yaml-files', $yaml_files);