Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Theme / InstallCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Theme\InstallCommand.
6  */
7
8 namespace Drupal\Console\Command\Theme;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Core\Config\ConfigFactory;
17 use Drupal\Core\Extension\ThemeHandler;
18 use Drupal\Core\Config\UnmetDependenciesException;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Core\Utils\ChainQueue;
21
22 class InstallCommand extends Command
23 {
24     use CommandTrait;
25
26     /**
27      * @var ConfigFactory
28      */
29     protected $configFactory;
30
31     /**
32      * @var ThemeHandler
33      */
34     protected $themeHandler;
35
36     /**
37      * @var ChainQueue
38      */
39     protected $chainQueue;
40
41     /**
42      * DebugCommand constructor.
43      *
44      * @param ConfigFactory $configFactory
45      * @param ThemeHandler  $themeHandler
46      * @param ChainQueue    $chainQueue
47      */
48     public function __construct(
49         ConfigFactory $configFactory,
50         ThemeHandler $themeHandler,
51         ChainQueue $chainQueue
52     ) {
53         $this->configFactory = $configFactory;
54         $this->themeHandler = $themeHandler;
55         $this->chainQueue = $chainQueue;
56         parent::__construct();
57     }
58
59     protected function configure()
60     {
61         $this
62             ->setName('theme:install')
63             ->setDescription($this->trans('commands.theme.install.description'))
64             ->addArgument('theme', InputArgument::IS_ARRAY, $this->trans('commands.theme.install.options.module'))
65             ->addOption(
66                 'set-default',
67                 null,
68                 InputOption::VALUE_NONE,
69                 $this->trans('commands.theme.install.options.set-default')
70             );
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     protected function interact(InputInterface $input, OutputInterface $output)
77     {
78         $io = new DrupalStyle($input, $output);
79
80         $theme = $input->getArgument('theme');
81
82         if (!$theme) {
83             $theme_list = [];
84
85             $themes = $this->themeHandler->rebuildThemeData();
86
87             foreach ($themes as $theme_id => $theme) {
88                 if (!empty($theme->info['hidden'])) {
89                     continue;
90                 }
91
92                 if ($theme->status) {
93                     continue;
94                 }
95
96                 $theme_list[$theme_id] = $theme->getName();
97             }
98
99             $io->info($this->trans('commands.theme.install.messages.disabled-themes'));
100
101             while (true) {
102                 $theme_name = $io->choiceNoList(
103                     $this->trans('commands.theme.install.questions.theme'),
104                     array_keys($theme_list)
105                 );
106
107                 if (empty($theme_name)) {
108                     break;
109                 }
110
111                 $theme_list_install[] = $theme_name;
112
113                 if (array_search($theme_name, $theme_list_install, true) >= 0) {
114                     unset($theme_list[$theme_name]);
115                 }
116             }
117
118             $input->setArgument('theme', $theme_list_install);
119         }
120     }
121
122     protected function execute(InputInterface $input, OutputInterface $output)
123     {
124         $io = new DrupalStyle($input, $output);
125
126         $config = $this->configFactory->getEditable('system.theme');
127
128         $this->themeHandler->refreshInfo();
129         $theme = $input->getArgument('theme');
130         $default = $input->getOption('set-default');
131
132         if ($default && count($theme) > 1) {
133             $io->error($this->trans('commands.theme.install.messages.invalid-theme-default'));
134
135             return 1;
136         }
137
138         $themes  = $this->themeHandler->rebuildThemeData();
139         $themesAvailable = [];
140         $themesInstalled = [];
141         $themesUnavailable = [];
142
143         foreach ($theme as $themeName) {
144             if (isset($themes[$themeName]) && $themes[$themeName]->status == 0) {
145                 $themesAvailable[] = $themes[$themeName]->info['name'];
146             } elseif (isset($themes[$themeName]) && $themes[$themeName]->status == 1) {
147                 $themesInstalled[] = $themes[$themeName]->info['name'];
148             } else {
149                 $themesUnavailable[] = $themeName;
150             }
151         }
152
153         if (count($themesAvailable) > 0) {
154             try {
155                 if ($this->themeHandler->install($theme)) {
156                     if (count($themesAvailable) > 1) {
157                         $io->info(
158                             sprintf(
159                                 $this->trans('commands.theme.install.messages.themes-success'),
160                                 implode(',', $themesAvailable)
161                             )
162                         );
163                     } else {
164                         if ($default) {
165                             // Set the default theme.
166                             $config->set('default', $theme[0])->save();
167                             $io->info(
168                                 sprintf(
169                                     $this->trans('commands.theme.install.messages.theme-default-success'),
170                                     $themesAvailable[0]
171                                 )
172                             );
173                         } else {
174                             $io->info(
175                                 sprintf(
176                                     $this->trans('commands.theme.install.messages.theme-success'),
177                                     $themesAvailable[0]
178                                 )
179                             );
180                         }
181                     }
182                 }
183             } catch (UnmetDependenciesException $e) {
184                 $io->error(
185                     sprintf(
186                         $this->trans('commands.theme.install.messages.success'),
187                         $theme
188                     )
189                 );
190                 drupal_set_message($e->getTranslatedMessage($this->getStringTranslation(), $theme), 'error');
191
192                 return 1;
193             }
194         } elseif (empty($themesAvailable) && count($themesInstalled) > 0) {
195             if (count($themesInstalled) > 1) {
196                 $io->info(
197                     sprintf(
198                         $this->trans('commands.theme.install.messages.themes-nothing'),
199                         implode(',', $themesInstalled)
200                     )
201                 );
202             } else {
203                 $io->info(
204                     sprintf(
205                         $this->trans('commands.theme.install.messages.theme-nothing'),
206                         implode(',', $themesInstalled)
207                     )
208                 );
209             }
210         } else {
211             if (count($themesUnavailable) > 1) {
212                 $io->error(
213                     sprintf(
214                         $this->trans('commands.theme.install.messages.themes-missing'),
215                         implode(',', $themesUnavailable)
216                     )
217                 );
218             } else {
219                 $io->error(
220                     sprintf(
221                         $this->trans('commands.theme.install.messages.theme-missing'),
222                         implode(',', $themesUnavailable)
223                     )
224                 );
225             }
226         }
227
228         // Run cache rebuild to see changes in Web UI
229         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
230
231         return 0;
232     }
233 }