40d00138c402df557738ffd59f0b9e25d6bf015b
[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      * @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:unset:key')
43             ->setDescription($this->trans('commands.yaml.unset.key.description'))
44             ->addArgument(
45                 'yaml-file',
46                 InputArgument::REQUIRED,
47                 $this->trans('commands.yaml.unset.value.arguments.yaml-file')
48             )
49             ->addArgument(
50                 'yaml-key',
51                 InputArgument::REQUIRED,
52                 $this->trans('commands.yaml.unset.value.arguments.yaml-key')
53             );
54     }
55
56     protected function execute(InputInterface $input, OutputInterface $output)
57     {
58         $io = new DrupalStyle($input, $output);
59
60         $yaml = new Parser();
61         $dumper = new Dumper();
62
63         $yaml_file = $input->getArgument('yaml-file');
64         $yaml_key = $input->getArgument('yaml-key');
65
66         try {
67             $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
68         } catch (\Exception $e) {
69             $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
70
71             return;
72         }
73
74         if (empty($yaml_parsed)) {
75             $io->info(
76                 sprintf(
77                     $this->trans('commands.yaml.merge.messages.wrong-parse'),
78                     $yaml_file
79                 )
80             );
81         }
82
83         $parents = explode(".", $yaml_key);
84         $this->nestedArray->unsetValue($yaml_parsed, $parents);
85
86         try {
87             $yaml = $dumper->dump($yaml_parsed, 10);
88         } catch (\Exception $e) {
89             $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
90
91             return;
92         }
93
94         try {
95             file_put_contents($yaml_file, $yaml);
96         } catch (\Exception $e) {
97             $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
98
99             return;
100         }
101
102         $io->info(
103             sprintf(
104                 $this->trans('commands.yaml.unset.value.messages.unset'),
105                 $yaml_file
106             )
107         );
108     }
109 }