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