Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Module / UninstallCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Module\UninstallCommand.
6  */
7
8 namespace Drupal\Console\Command\Module;
9
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Drupal\Console\Extension\Manager;
12 use Symfony\Component\Console\Input\InputArgument;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Command\Shared\ProjectDownloadTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Utils\Site;
20 use Drupal\Core\ProxyClass\Extension\ModuleInstaller;
21 use Drupal\Console\Core\Utils\ChainQueue;
22 use Drupal\Core\Config\ConfigFactory;
23
24 class UninstallCommand extends Command
25 {
26     use CommandTrait;
27     use ProjectDownloadTrait;
28
29     /**
30      * @var Site
31      */
32     protected $site;
33
34     /**
35      * @var ModuleInstaller
36      */
37     protected $moduleInstaller;
38
39     /**
40      * @var ChainQueue
41      */
42     protected $chainQueue;
43
44     /**
45      * @var ConfigFactory
46      */
47     protected $configFactory;
48
49     /**
50      * @var Manager
51      */
52     protected $extensionManager;
53
54     /**
55      * InstallCommand constructor.
56      *
57      * @param Site            $site
58      * @param ModuleInstaller $moduleInstaller
59      * @param ChainQueue      $chainQueue
60      * @param ConfigFactory   $configFactory
61      * @param Manager         $extensionManager
62      */
63     public function __construct(
64         Site $site,
65         ModuleInstaller $moduleInstaller,
66         ChainQueue $chainQueue,
67         ConfigFactory $configFactory,
68         Manager $extensionManager
69     ) {
70         $this->site = $site;
71         $this->moduleInstaller = $moduleInstaller;
72         $this->chainQueue = $chainQueue;
73         $this->configFactory = $configFactory;
74         $this->extensionManager = $extensionManager;
75         parent::__construct();
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     protected function configure()
82     {
83         $this
84             ->setName('module:uninstall')
85             ->setDescription($this->trans('commands.module.uninstall.description'))
86             ->addArgument(
87                 'module',
88                 InputArgument::IS_ARRAY,
89                 $this->trans('commands.module.uninstall.questions.module')
90             )
91             ->addOption(
92                 'force',
93                 null,
94                 InputOption::VALUE_NONE,
95                 $this->trans('commands.module.uninstall.options.force')
96             )
97             ->addOption(
98                 'composer',
99                 null,
100                 InputOption::VALUE_NONE,
101                 $this->trans('commands.module.uninstall.options.composer')
102             );
103     }
104     /**
105      * {@inheritdoc}
106      */
107     protected function interact(InputInterface $input, OutputInterface $output)
108     {
109         $io = new DrupalStyle($input, $output);
110         $module = $input->getArgument('module');
111
112         if (!$module) {
113             $module = $this->modulesUninstallQuestion($io);
114             $input->setArgument('module', $module);
115         }
116     }
117     /**
118      * {@inheritdoc}
119      */
120     protected function execute(InputInterface $input, OutputInterface $output)
121     {
122         $io =  new DrupalStyle($input, $output);
123         $composer = $input->getOption('composer');
124         $module = $input->getArgument('module');
125
126         $this->site->loadLegacyFile('/core/modules/system/system.module');
127
128         $coreExtension = $this->configFactory->getEditable('core.extension');
129
130         // Get info about modules available
131         $moduleData = system_rebuild_module_data();
132         $moduleList = array_combine($module, $module);
133
134         if ($composer) {
135             //@TODO: check with Composer if the module is previously required in composer.json!
136             foreach ($module as $moduleItem) {
137                 $command = sprintf(
138                     'composer remove drupal/%s ',
139                     $moduleItem
140                 );
141
142                 $shellProcess = $this->get('shell_process');
143                 if ($shellProcess->exec($command)) {
144                     $io->success(
145                         sprintf(
146                             $this->trans('commands.module.uninstall.messages.composer-success'),
147                             $moduleItem
148                         )
149                     );
150                 }
151             }
152         }
153
154         if ($missingModules = array_diff_key($moduleList, $moduleData)) {
155             $io->error(
156                 sprintf(
157                     $this->trans('commands.module.uninstall.messages.missing'),
158                     implode(', ', $module),
159                     implode(', ', $missingModules)
160                 )
161             );
162
163             return 1;
164         }
165
166         $installedModules = $coreExtension->get('module') ?: [];
167         if (!$moduleList = array_intersect_key($moduleList, $installedModules)) {
168             $io->info($this->trans('commands.module.uninstall.messages.nothing'));
169
170             return 0;
171         }
172
173         if (!$force = $input->getOption('force')) {
174             $dependencies = [];
175             while (list($module) = each($moduleList)) {
176                 foreach (array_keys($moduleData[$module]->required_by) as $dependency) {
177                     if (isset($installedModules[$dependency]) && !isset($moduleList[$dependency]) && $dependency != $profile) {
178                         $dependencies[] = $dependency;
179                     }
180                 }
181             }
182
183             if (!empty($dependencies)) {
184                 $io->error(
185                     sprintf(
186                         $this->trans('commands.module.uninstall.messages.dependents'),
187                         implode('", "', $moduleList),
188                         implode(', ', $dependencies)
189                     )
190                 );
191
192                 return 1;
193             }
194         }
195
196         try {
197             $this->moduleInstaller->uninstall($moduleList);
198
199             $io->info(
200                 sprintf(
201                     $this->trans('commands.module.uninstall.messages.success'),
202                     implode(', ', $moduleList)
203                 )
204             );
205
206             $io->comment(
207                 sprintf(
208                     $this->trans('commands.module.uninstall.messages.composer-success'),
209                     implode(', ', $moduleList),
210                     false
211                 )
212             );
213         } catch (\Exception $e) {
214             $io->error($e->getMessage());
215
216             return 1;
217         }
218
219         $this->site->removeCachedServicesFile();
220         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
221     }
222 }