5 * Contains \Drupal\Console\Core\Command\Yaml\SplitCommand.
8 namespace Drupal\Console\Core\Command\Yaml;
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;
21 class SplitCommand extends Command
28 protected $nestedArray;
31 * SplitCommand constructor.
33 * @param NestedArray $nestedArray
35 public function __construct(NestedArray $nestedArray)
37 $this->nestedArray = $nestedArray;
38 parent::__construct();
41 protected function configure()
44 ->setName('yaml:split')
45 ->setDescription($this->trans('commands.yaml.split.description'))
48 InputArgument::REQUIRED,
49 $this->trans('commands.yaml.split.value.arguments.yaml-file')
54 InputOption::VALUE_REQUIRED,
55 $this->trans('commands.yaml.split.options.indent-level')
60 InputOption::VALUE_REQUIRED,
61 $this->trans('commands.yaml.split.options.file-output-prefix')
66 InputOption::VALUE_REQUIRED,
67 $this->trans('commands.yaml.split.options.file-output-suffix')
72 InputOption::VALUE_REQUIRED,
73 $this->trans('commands.yaml.split.options.starting-key')
76 'exclude-parents-key',
78 InputOption::VALUE_NONE,
79 $this->trans('commands.yaml.split.options.exclude-parents-key')
83 protected function execute(InputInterface $input, OutputInterface $output)
85 $io = new DrupalStyle($input, $output);
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');
96 if ($exclude_parents_key == 1 || $exclude_parents_key == 'TRUE') {
97 $exclude_parents_key = true;
99 $exclude_parents_key = false;
103 $yaml_file_parsed = $yaml->parse(file_get_contents($yaml_file));
105 if (empty($yaml_file_parsed)) {
108 $this->trans('commands.yaml.merge.messages.wrong-parse'),
115 } catch (\Exception $e) {
119 $this->trans('commands.yaml.merge.messages.error-parsing'),
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);
132 $io->error($this->trans('commands.yaml.merge.messages.invalid-key'));
135 if ($indent_level == 0) {
136 $yaml_split[$starting_key] = $yaml_file_parsed;
139 // Set minimum level to split
140 $indent_level = empty($indent_level) ? 1 : $indent_level;
146 $this->nestedArray->yamlSplitArray($yaml_file_parsed, $yaml_split, $indent_level, $key_flatten, $initial_level, $exclude_parents_key);
149 $this->writeSplittedFile($yaml_split, $file_output_prefix, $file_output_suffix, $io);
152 protected function writeSplittedFile($yaml_splitted, $file_output_prefix = '', $file_output_suffix = '', DrupalStyle $io)
154 $dumper = new Dumper();
156 $io->info($this->trans('commands.yaml.split.messages.generating-split'));
158 foreach ($yaml_splitted as $key => $value) {
159 if ($file_output_prefix) {
160 $key = $file_output_prefix . '.' . $key;
163 if ($file_output_suffix) {
164 $key.= '.' . $file_output_suffix;
166 $filename = $key . '.yml';
169 $yaml = $dumper->dump($value, 10);
170 } catch (\Exception $e) {
174 $this->trans('commands.yaml.merge.messages.error-generating'),
183 file_put_contents($filename, $yaml);
184 } catch (\Exception $e) {
188 $this->trans('commands.yaml.merge.messages.error-writing'),
198 $this->trans('commands.yaml.split.messages.split-generated'),
208 protected function interact(InputInterface $input, OutputInterface $output)
210 $io = new DrupalStyle($input, $output);
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'));
222 // --yaml-left option
223 $yaml_file = $input->getArgument('yaml-file');
226 $yaml_file = $io->ask(
227 $this->trans('commands.yaml.diff.questions.yaml-left'),
237 $input->setArgument('yaml-file', $yaml_file);