Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Command / Site / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Site\DebugCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Site;
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\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Utils\ConfigurationManager;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 /**
20  * Class SiteDebugCommand
21  *
22  * @package Drupal\Console\Core\Command\Site
23  */
24 class DebugCommand extends Command
25 {
26     use CommandTrait;
27
28     /**
29      * @var ConfigurationManager
30      */
31     protected $configurationManager;
32
33     /**
34      * DebugCommand constructor.
35      *
36      * @param ConfigurationManager $configurationManager
37      */
38     public function __construct(
39         ConfigurationManager $configurationManager
40     ) {
41         $this->configurationManager = $configurationManager;
42         parent::__construct();
43     }
44
45     /**
46      * @{@inheritdoc}
47      */
48     public function configure()
49     {
50         $this
51             ->setName('site:debug')
52             ->setDescription($this->trans('commands.site.debug.description'))
53             ->addArgument(
54                 'target',
55                 InputArgument::OPTIONAL,
56                 $this->trans('commands.site.debug.options.target'),
57                 null
58             )
59             ->addArgument(
60                 'property',
61                 InputArgument::OPTIONAL,
62                 $this->trans('commands.site.debug.options.property'),
63                 null
64             )
65             ->setHelp($this->trans('commands.site.debug.help'));
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     protected function execute(InputInterface $input, OutputInterface $output)
72     {
73         $io = new DrupalStyle($input, $output);
74
75         $sites = array_keys($this->configurationManager->getSites());
76
77         if (!$sites) {
78             $io->error($this->trans('commands.site.debug.messages.invalid-sites'));
79
80             return 1;
81         }
82
83
84         // --target argument
85         $target = $input->getArgument('target');
86         if (!$target) {
87             $tableHeader =[
88                 $this->trans('commands.site.debug.messages.site'),
89             ];
90
91             $io->table($tableHeader, $sites);
92
93             return 0;
94         }
95
96         $targetConfig = $this->configurationManager->readTarget($target);
97         if (!$targetConfig) {
98             $io->error($this->trans('commands.site.debug.messages.invalid-site'));
99
100             return 1;
101         }
102
103         // --property argument, allows the user to fetch specific properties of the selected site
104         $property = $input->getArgument('property');
105         if ($property) {
106             $property_keys = explode('.', $property);
107
108             $val = $targetConfig;
109             foreach ($property_keys as $property_key) {
110                 $val = &$val[$property_key];
111             }
112
113             $io->writeln($val);
114             return 0;
115         }
116
117         $io->info($target);
118         $dumper = new Dumper();
119         $io->writeln($dumper->dump($targetConfig, 2));
120
121         return 0;
122     }
123 }