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