Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Command / Yaml / GetValueCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Yaml\GetValueCommand.
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\Parser;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Core\Utils\NestedArray;
18
19 class GetValueCommand extends Command
20 {
21     use CommandTrait;
22
23     /**
24      * @var NestedArray
25      */
26     protected $nestedArray;
27
28     /**
29      * GetValueCommand constructor.
30      *
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:get:value')
43             ->setDescription($this->trans('commands.yaml.get.value.description'))
44             ->addArgument(
45                 'yaml-file',
46                 InputArgument::REQUIRED,
47                 $this->trans('commands.yaml.get.value.arguments.yaml-file')
48             )
49             ->addArgument(
50                 'yaml-key',
51                 InputArgument::REQUIRED,
52                 $this->trans('commands.yaml.get.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
62         $yaml_file = $input->getArgument('yaml-file');
63         $yaml_key = $input->getArgument('yaml-key');
64
65         try {
66             $yaml_parsed = $yaml->parse(file_get_contents($yaml_file), true);
67         } catch (\Exception $e) {
68             $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
69             return;
70         }
71
72         if (empty($yaml_parsed)) {
73             $io->info(
74                 sprintf(
75                     $this->trans('commands.yaml.merge.messages.wrong-parse'),
76                     $yaml_file
77                 )
78             );
79         } else {
80             $key_exists = null;
81             $parents = explode(".", $yaml_key);
82             $yaml_value = $this->nestedArray->getValue($yaml_parsed, $parents, $key_exists);
83
84             if (!$key_exists) {
85                 $io->info(
86                     sprintf(
87                         $this->trans('commands.yaml.get.value.messages.invalid-key'),
88                         $yaml_key,
89                         $yaml_file
90                     )
91                 );
92             }
93
94             $io->writeln($yaml_value);
95         }
96     }
97 }