Updated to Drupal 8.5. Core Media not yet in use.
[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,
82                 $theme->info['name'],
83                 $status,
84                 (isset($theme->info['version'])) ? $theme->info['version'] : '',
85             ];
86         }
87
88         $this->getIo()->table($tableHeader, $tableRows);
89     }
90
91     protected function themeDetail($themeId)
92     {
93         $theme = null;
94         $themes = $this->themeHandler->rebuildThemeData();
95
96         if (isset($themes[$themeId])) {
97             $theme = $themes[$themeId];
98         } else {
99             foreach ($themes as $themeAvailableId => $themeAvailable) {
100                 if ($themeAvailable->info['name'] == $themeId) {
101                     $themeId = $themeAvailableId;
102                     $theme = $themeAvailable;
103                     break;
104                 }
105             }
106         }
107
108         if ($theme) {
109             $theme = $themes[$themeId];
110             $status = $this->getThemeStatus($themeId);
111
112             $this->getIo()->info($theme->info['name']);
113
114             $this->getIo()->comment(
115                 sprintf(
116                     '%s : ',
117                     $this->trans('commands.debug.theme.messages.status')
118                 ),
119                 false
120             );
121             $this->getIo()->writeln($status);
122             $this->getIo()->comment(
123                 sprintf(
124                     '%s : ',
125                     $this->trans('commands.debug.theme.messages.version')
126                 ),
127                 false
128             );
129             $this->getIo()->writeln($theme->info['version']);
130             $this->getIo()->comment($this->trans('commands.debug.theme.messages.regions'));
131             $tableRows = $this->addThemeAttributes($theme->info['regions'], $tableRows);
132             $this->getIo()->table([], $tableRows);
133         } else {
134             $this->getIo()->error(
135                 sprintf(
136                     $this->trans('commands.debug.theme.messages.invalid-theme'),
137                     $themeId
138                 )
139             );
140         }
141     }
142
143     protected function getThemeStatus($theme)
144     {
145         $defaultTheme = $this->configFactory->get('system.theme')->get('default');
146
147         $status = ($theme->status)?$this->trans('commands.debug.theme.messages.installed'):$this->trans('commands.debug.theme.messages.uninstalled');
148         if ($defaultTheme == $theme) {
149             $status = $this->trans('commands.debug.theme.messages.default-theme');
150         }
151
152         return $status;
153     }
154
155     protected function addThemeAttributes($attr, $tableRows = [])
156     {
157         foreach ($attr as $key => $value) {
158             if (is_array($value)) {
159                 $tableRows = $this->addThemeAttributes($value, $tableRows);
160             } else {
161                 $tableRows[] = [
162                     $key,
163                     $value,
164                 ];
165             }
166         }
167
168         return $tableRows;
169     }
170 }