db backup prior to drupal security update
[yaffs-website] / vendor / drupal / console / src / Command / Theme / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Theme\Debugommand.
6  */
7
8 namespace Drupal\Console\Command\Theme;
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\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Core\Config\ConfigFactory;
16 use Drupal\Core\Extension\ThemeHandler;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 class DebugCommand extends Command
20 {
21     use CommandTrait;
22
23     /**
24      * @var ConfigFactory
25      */
26     protected $configFactory;
27
28     /**
29      * @var ThemeHandler
30      */
31     protected $themeHandler;
32
33     /**
34      * DebugCommand constructor.
35      *
36      * @param ConfigFactory $configFactory
37      * @param ThemeHandler  $themeHandler
38      */
39     public function __construct(
40         ConfigFactory $configFactory,
41         ThemeHandler $themeHandler
42     ) {
43         $this->configFactory = $configFactory;
44         $this->themeHandler = $themeHandler;
45         parent::__construct();
46     }
47
48     protected function configure()
49     {
50         $this
51             ->setName('theme:debug')
52             ->setDescription($this->trans('commands.theme.debug.description'))
53             ->addArgument('theme', InputArgument::OPTIONAL, $this->trans('commands.theme.debug.arguments.theme'));
54     }
55
56     protected function execute(InputInterface $input, OutputInterface $output)
57     {
58         $io = new DrupalStyle($input, $output);
59
60         $theme = $input->getArgument('theme');
61         if ($theme) {
62             $this->themeDetail($io, $theme);
63         } else {
64             $this->themeList($io);
65         }
66     }
67
68     protected function themeList(DrupalStyle $io)
69     {
70         $tableHeader = [
71             $this->trans('commands.theme.debug.messages.theme-id'),
72             $this->trans('commands.theme.debug.messages.theme-name'),
73             $this->trans('commands.theme.debug.messages.status'),
74             $this->trans('commands.theme.debug.messages.version'),
75         ];
76
77         $themes = $this->themeHandler->rebuildThemeData();
78         $tableRows = [];
79         foreach ($themes as $themeId => $theme) {
80             $status = $this->getThemeStatus($theme);
81             $tableRows[] = [
82                 $themeId, $theme->info['name'],
83                 $status, $theme->info['version'],
84             ];
85         }
86
87         $io->table($tableHeader, $tableRows);
88     }
89
90     protected function themeDetail(DrupalStyle $io, $themeId)
91     {
92         $theme = null;
93         $themes = $this->themeHandler->rebuildThemeData();
94
95         if (isset($themes[$themeId])) {
96             $theme = $themes[$themeId];
97         } else {
98             foreach ($themes as $themeAvailableId => $themeAvailable) {
99                 if ($themeAvailable->info['name'] == $themeId) {
100                     $themeId = $themeAvailableId;
101                     $theme = $themeAvailable;
102                     break;
103                 }
104             }
105         }
106
107         if ($theme) {
108             $theme = $themes[$themeId];
109             $status = $this->getThemeStatus($themeId);
110
111             $io->info($theme->info['name']);
112
113             $io->comment(
114                 sprintf(
115                     '%s : ',
116                     $this->trans('commands.theme.debug.messages.status')
117                 ),
118                 false
119             );
120             $io->writeln($status);
121             $io->comment(
122                 sprintf(
123                     '%s : ',
124                     $this->trans('commands.theme.debug.messages.version')
125                 ),
126                 false
127             );
128             $io->writeln($theme->info['version']);
129             $io->comment($this->trans('commands.theme.debug.messages.regions'));
130             $tableRows = $this->addThemeAttributes($theme->info['regions'], $tableRows);
131             $io->table([], $tableRows);
132         } else {
133             $io->error(
134                 sprintf(
135                     $this->trans('commands.theme.debug.messages.invalid-theme'),
136                     $themeId
137                 )
138             );
139         }
140     }
141
142     protected function getThemeStatus($theme)
143     {
144         $defaultTheme = $this->configFactory->get('system.theme')->get('default');
145
146         $status = ($theme->status)?$this->trans('commands.theme.debug.messages.installed'):$this->trans('commands.theme.debug.messages.uninstalled');
147         if ($defaultTheme == $theme) {
148             $status = $this->trans('commands.theme.debug.messages.default-theme');
149         }
150
151         return $status;
152     }
153
154     protected function addThemeAttributes($attr, $tableRows = [])
155     {
156         foreach ($attr as $key => $value) {
157             if (is_array($value)) {
158                 $tableRows = $this->addThemeAttributes($value, $tableRows);
159             } else {
160                 $tableRows[] = [
161                     $key,
162                     $value,
163                 ];
164             }
165         }
166
167         return $tableRows;
168     }
169 }