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.
32 * @param NestedArray $nestedArray
34 public function __construct(NestedArray $nestedArray)
36 $this->nestedArray = $nestedArray;
37 parent::__construct();
40 protected function configure()
43 ->setName('yaml:split')
44 ->setDescription($this->trans('commands.yaml.split.description'))
47 InputArgument::REQUIRED,
48 $this->trans('commands.yaml.split.value.arguments.yaml-file')
53 InputOption::VALUE_REQUIRED,
54 $this->trans('commands.yaml.split.options.indent-level')
59 InputOption::VALUE_REQUIRED,
60 $this->trans('commands.yaml.split.options.file-output-prefix')
65 InputOption::VALUE_REQUIRED,
66 $this->trans('commands.yaml.split.options.file-output-suffix')
71 InputOption::VALUE_REQUIRED,
72 $this->trans('commands.yaml.split.options.starting-key')
75 'exclude-parents-key',
77 InputOption::VALUE_NONE,
78 $this->trans('commands.yaml.split.options.exclude-parents-key')
82 protected function execute(InputInterface $input, OutputInterface $output)
84 $io = new DrupalStyle($input, $output);
88 $yaml_file = $input->getArgument('yaml-file');
89 $indent_level = $input->getOption('indent-level');
90 $exclude_parents_key = $input->getOption('exclude-parents-key');
91 $starting_key = $input->getOption('starting-key');
92 $file_output_prefix = $input->getOption('file-output-prefix');
93 $file_output_suffix = $input->getOption('file-output-suffix');
95 if ($exclude_parents_key == 1 || $exclude_parents_key == 'TRUE') {
96 $exclude_parents_key = true;
98 $exclude_parents_key = false;
102 $yaml_file_parsed = $yaml->parse(file_get_contents($yaml_file));
104 if (empty($yaml_file_parsed)) {
107 $this->trans('commands.yaml.merge.messages.wrong-parse'),
114 } catch (\Exception $e) {
118 $this->trans('commands.yaml.merge.messages.error-parsing'),
127 $parents = explode(".", $starting_key);
128 if ($this->nestedArray->keyExists($yaml_file_parsed, $parents)) {
129 $yaml_file_parsed = $this->nestedArray->getValue($yaml_file_parsed, $parents);
131 $io->error($this->trans('commands.yaml.merge.messages.invalid-key'));
134 if ($indent_level == 0) {
135 $yaml_split[$starting_key] = $yaml_file_parsed;
138 // Set minimum level to split
139 $indent_level = empty($indent_level) ? 1 : $indent_level;
141 $yaml_split = array();
145 $this->nestedArray->yamlSplitArray($yaml_file_parsed, $yaml_split, $indent_level, $key_flatten, $initial_level, $exclude_parents_key);
148 $this->writeSplittedFile($yaml_split, $file_output_prefix, $file_output_suffix, $io);
151 protected function writeSplittedFile($yaml_splitted, $file_output_prefix = '', $file_output_suffix = '', DrupalStyle $io)
153 $dumper = new Dumper();
155 $io->info($this->trans('commands.yaml.split.messages.generating-split'));
157 foreach ($yaml_splitted as $key => $value) {
158 if ($file_output_prefix) {
159 $key = $file_output_prefix . '.' . $key;
162 if ($file_output_suffix) {
163 $key.= '.' . $file_output_suffix;
165 $filename = $key . '.yml';
168 $yaml = $dumper->dump($value, 10);
169 } catch (\Exception $e) {
173 $this->trans('commands.yaml.merge.messages.error-generating'),
182 file_put_contents($filename, $yaml);
183 } catch (\Exception $e) {
187 $this->trans('commands.yaml.merge.messages.error-writing'),
197 $this->trans('commands.yaml.split.messages.split-generated'),
207 protected function interact(InputInterface $input, OutputInterface $output)
209 $io = new DrupalStyle($input, $output);
211 $validator_filename = function ($value) use ($io) {
212 if (!strlen(trim($value)) || !is_file($value)) {
213 $io->error($this->trans('commands.common.errors.invalid-file-path'));
221 // --yaml-left option
222 $yaml_file = $input->getArgument('yaml-file');
225 $yaml_file = $io->ask(
226 $this->trans('commands.yaml.diff.questions.yaml-left'),
236 $input->setArgument('yaml-file', $yaml_file);