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.
31 * @param NestedArray $nestedArray
33 public function __construct(NestedArray $nestedArray)
35 $this->nestedArray = $nestedArray;
36 parent::__construct();
39 protected function configure()
42 ->setName('yaml:update:key')
43 ->setDescription($this->trans('commands.yaml.update.key.description'))
46 InputArgument::REQUIRED,
47 $this->trans('commands.yaml.update.value.arguments.yaml-file')
51 InputArgument::REQUIRED,
52 $this->trans('commands.yaml.update.value.arguments.yaml-key')
56 InputArgument::REQUIRED,
57 $this->trans('commands.yaml.update.value.arguments.yaml-new-key')
61 protected function execute(InputInterface $input, OutputInterface $output)
63 $io = new DrupalStyle($input, $output);
66 $dumper = new Dumper();
68 $yaml_file = $input->getArgument('yaml-file');
69 $yaml_key = $input->getArgument('yaml-key');
70 $yaml_new_key = $input->getArgument('yaml-new-key');
73 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
74 } catch (\Exception $e) {
75 $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
80 if (empty($yaml_parsed)) {
83 $this->trans('commands.yaml.merge.messages.wrong-parse'),
89 $parents = explode(".", $yaml_key);
90 $this->nestedArray->replaceKey($yaml_parsed, $parents, $yaml_new_key);
93 $yaml = $dumper->dump($yaml_parsed, 10);
94 } catch (\Exception $e) {
95 $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
101 file_put_contents($yaml_file, $yaml);
102 } catch (\Exception $e) {
103 $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
110 $this->trans('commands.yaml.update.value.messages.updated'),