761b59a20935957f3d6dc012299d7b1293a04cb6
[yaffs-website] / vendor / drupal / console-core / src / Command / Yaml / UpdateKeyCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Yaml\UpdateKeyCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Yaml;
9
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;
19
20 class UpdateKeyCommand extends Command
21 {
22     use CommandTrait;
23
24     /**
25      * @var NestedArray
26      */
27     protected $nestedArray;
28
29     /**
30      * UpdateKeyCommand constructor.
31      * @param NestedArray $nestedArray
32      */
33     public function __construct(NestedArray $nestedArray)
34     {
35         $this->nestedArray = $nestedArray;
36         parent::__construct();
37     }
38
39     protected function configure()
40     {
41         $this
42             ->setName('yaml:update:key')
43             ->setDescription($this->trans('commands.yaml.update.key.description'))
44             ->addArgument(
45                 'yaml-file',
46                 InputArgument::REQUIRED,
47                 $this->trans('commands.yaml.update.value.arguments.yaml-file')
48             )
49             ->addArgument(
50                 'yaml-key',
51                 InputArgument::REQUIRED,
52                 $this->trans('commands.yaml.update.value.arguments.yaml-key')
53             )
54             ->addArgument(
55                 'yaml-new-key',
56                 InputArgument::REQUIRED,
57                 $this->trans('commands.yaml.update.value.arguments.yaml-new-key')
58             );
59     }
60
61     protected function execute(InputInterface $input, OutputInterface $output)
62     {
63         $io = new DrupalStyle($input, $output);
64
65         $yaml = new Parser();
66         $dumper = new Dumper();
67
68         $yaml_file = $input->getArgument('yaml-file');
69         $yaml_key = $input->getArgument('yaml-key');
70         $yaml_new_key = $input->getArgument('yaml-new-key');
71
72         try {
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());
76
77             return;
78         }
79
80         if (empty($yaml_parsed)) {
81             $io->info(
82                 sprintf(
83                     $this->trans('commands.yaml.merge.messages.wrong-parse'),
84                     $yaml_file
85                 )
86             );
87         }
88
89         $parents = explode(".", $yaml_key);
90         $this->nestedArray->replaceKey($yaml_parsed, $parents, $yaml_new_key);
91
92         try {
93             $yaml = $dumper->dump($yaml_parsed, 10);
94         } catch (\Exception $e) {
95             $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
96
97             return;
98         }
99
100         try {
101             file_put_contents($yaml_file, $yaml);
102         } catch (\Exception $e) {
103             $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
104
105             return;
106         }
107
108         $io->info(
109             sprintf(
110                 $this->trans('commands.yaml.update.value.messages.updated'),
111                 $yaml_file
112             )
113         );
114     }
115 }