5 * Contains \Drupal\Console\Core\Command\Yaml\UpdateKeyCommand.
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 Drupal\Console\Core\Utils\NestedArray;
20 class UpdateKeyCommand extends Command
27 protected $nestedArray;
30 * UpdateKeyCommand 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:update:key')
44 ->setDescription($this->trans('commands.yaml.update.key.description'))
47 InputArgument::REQUIRED,
48 $this->trans('commands.yaml.update.value.arguments.yaml-file')
52 InputArgument::REQUIRED,
53 $this->trans('commands.yaml.update.value.arguments.yaml-key')
57 InputArgument::REQUIRED,
58 $this->trans('commands.yaml.update.value.arguments.yaml-new-key')
62 protected function execute(InputInterface $input, OutputInterface $output)
64 $io = new DrupalStyle($input, $output);
67 $dumper = new Dumper();
69 $yaml_file = $input->getArgument('yaml-file');
70 $yaml_key = $input->getArgument('yaml-key');
71 $yaml_new_key = $input->getArgument('yaml-new-key');
74 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
75 } catch (\Exception $e) {
76 $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
81 if (empty($yaml_parsed)) {
84 $this->trans('commands.yaml.merge.messages.wrong-parse'),
90 $parents = explode(".", $yaml_key);
91 $this->nestedArray->replaceKey($yaml_parsed, $parents, $yaml_new_key);
94 $yaml = $dumper->dump($yaml_parsed, 10);
95 } catch (\Exception $e) {
96 $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
102 file_put_contents($yaml_file, $yaml);
103 } catch (\Exception $e) {
104 $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
111 $this->trans('commands.yaml.update.value.messages.updated'),