Yaffs site version 1.1
[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      *
32      * @param NestedArray $nestedArray
33      */
34     public function __construct(NestedArray $nestedArray)
35     {
36         $this->nestedArray = $nestedArray;
37         parent::__construct();
38     }
39
40     protected function configure()
41     {
42         $this
43             ->setName('yaml:update:key')
44             ->setDescription($this->trans('commands.yaml.update.key.description'))
45             ->addArgument(
46                 'yaml-file',
47                 InputArgument::REQUIRED,
48                 $this->trans('commands.yaml.update.value.arguments.yaml-file')
49             )
50             ->addArgument(
51                 'yaml-key',
52                 InputArgument::REQUIRED,
53                 $this->trans('commands.yaml.update.value.arguments.yaml-key')
54             )
55             ->addArgument(
56                 'yaml-new-key',
57                 InputArgument::REQUIRED,
58                 $this->trans('commands.yaml.update.value.arguments.yaml-new-key')
59             );
60     }
61
62     protected function execute(InputInterface $input, OutputInterface $output)
63     {
64         $io = new DrupalStyle($input, $output);
65
66         $yaml = new Parser();
67         $dumper = new Dumper();
68
69         $yaml_file = $input->getArgument('yaml-file');
70         $yaml_key = $input->getArgument('yaml-key');
71         $yaml_new_key = $input->getArgument('yaml-new-key');
72
73         try {
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());
77
78             return;
79         }
80
81         if (empty($yaml_parsed)) {
82             $io->info(
83                 sprintf(
84                     $this->trans('commands.yaml.merge.messages.wrong-parse'),
85                     $yaml_file
86                 )
87             );
88         }
89
90         $parents = explode(".", $yaml_key);
91         $this->nestedArray->replaceKey($yaml_parsed, $parents, $yaml_new_key);
92
93         try {
94             $yaml = $dumper->dump($yaml_parsed, 10);
95         } catch (\Exception $e) {
96             $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
97
98             return;
99         }
100
101         try {
102             file_put_contents($yaml_file, $yaml);
103         } catch (\Exception $e) {
104             $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
105
106             return;
107         }
108
109         $io->info(
110             sprintf(
111                 $this->trans('commands.yaml.update.value.messages.updated'),
112                 $yaml_file
113             )
114         );
115     }
116 }