db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console / src / Command / Config / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Config;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Component\Serialization\Yaml;
14 use Drupal\Core\Config\CachedStorage;
15 use Drupal\Core\Config\ConfigFactory;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 class DebugCommand extends Command
21 {
22     use CommandTrait;
23
24     /**
25      * @var ConfigFactory
26      */
27     protected $configFactory;
28
29     /**
30      * @var CachedStorage
31      */
32     protected $configStorage;
33
34     /**
35      * DebugCommand constructor.
36      *
37      * @param ConfigFactory $configFactory
38      * @param CachedStorage $configStorage
39      */
40     public function __construct(
41         ConfigFactory $configFactory,
42         CachedStorage $configStorage
43     ) {
44         $this->configFactory = $configFactory;
45         $this->configStorage = $configStorage;
46         parent::__construct();
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     protected function configure()
53     {
54         $this
55             ->setName('config:debug')
56             ->setDescription($this->trans('commands.config.debug.description'))
57             ->addArgument(
58                 'name',
59                 InputArgument::OPTIONAL,
60                 $this->trans('commands.config.debug.arguments.name')
61             );
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function execute(InputInterface $input, OutputInterface $output)
68     {
69         $io = new DrupalStyle($input, $output);
70
71         $configName = $input->getArgument('name');
72         if (!$configName) {
73             $this->getAllConfigurations($io);
74         } else {
75             $this->getConfigurationByName($io, $configName);
76         }
77     }
78
79     /**
80      * @param $io         DrupalStyle
81      */
82     private function getAllConfigurations(DrupalStyle $io)
83     {
84         $names = $this->configFactory->listAll();
85         $tableHeader = [
86             $this->trans('commands.config.debug.arguments.name'),
87         ];
88         $tableRows = [];
89         foreach ($names as $name) {
90             $tableRows[] = [
91                 $name,
92             ];
93         }
94
95         $io->table($tableHeader, $tableRows, 'compact');
96     }
97
98     /**
99      * @param $io             DrupalStyle
100      * @param $config_name    String
101      */
102     private function getConfigurationByName(DrupalStyle $io, $config_name)
103     {
104         if ($this->configStorage->exists($config_name)) {
105             $tableHeader = [
106                 $config_name,
107             ];
108
109             $configuration = $this->configStorage->read($config_name);
110             $configurationEncoded = Yaml::encode($configuration);
111             $tableRows = [];
112             $tableRows[] = [
113                 $configurationEncoded,
114             ];
115
116             $io->table($tableHeader, $tableRows, 'compact');
117         } else {
118             $io->error(
119                 sprintf($this->trans('commands.config.debug.errors.not-exists'), $config_name)
120             );
121         }
122     }
123 }