Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Command / Yaml / UnsetKeyCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Yaml\UnsetKeyCommand.
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 UnsetKeyCommand extends Command
21 {
22     use CommandTrait;
23
24     /**
25    * @var NestedArray
26    */
27     protected $nestedArray;
28
29     /**
30      * UnsetKeyCommand 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:unset:key')
44             ->setDescription($this->trans('commands.yaml.unset.key.description'))
45             ->addArgument(
46                 'yaml-file',
47                 InputArgument::REQUIRED,
48                 $this->trans('commands.yaml.unset.value.arguments.yaml-file')
49             )
50             ->addArgument(
51                 'yaml-key',
52                 InputArgument::REQUIRED,
53                 $this->trans('commands.yaml.unset.value.arguments.yaml-key')
54             );
55     }
56
57     protected function execute(InputInterface $input, OutputInterface $output)
58     {
59         $io = new DrupalStyle($input, $output);
60
61         $yaml = new Parser();
62         $dumper = new Dumper();
63
64         $yaml_file = $input->getArgument('yaml-file');
65         $yaml_key = $input->getArgument('yaml-key');
66
67         try {
68             $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
69         } catch (\Exception $e) {
70             $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
71
72             return;
73         }
74
75         if (empty($yaml_parsed)) {
76             $io->info(
77                 sprintf(
78                     $this->trans('commands.yaml.merge.messages.wrong-parse'),
79                     $yaml_file
80                 )
81             );
82         }
83
84         $parents = explode(".", $yaml_key);
85         $this->nestedArray->unsetValue($yaml_parsed, $parents);
86
87         try {
88             $yaml = $dumper->dump($yaml_parsed, 10);
89         } catch (\Exception $e) {
90             $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
91
92             return;
93         }
94
95         try {
96             file_put_contents($yaml_file, $yaml);
97         } catch (\Exception $e) {
98             $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
99
100             return;
101         }
102
103         $io->info(
104             sprintf(
105                 $this->trans('commands.yaml.unset.value.messages.unset'),
106                 $yaml_file
107             )
108         );
109     }
110 }