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