5 * Contains \Drupal\Console\Command\Module\UninstallCommand.
8 namespace Drupal\Console\Command\Module;
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Command\Shared\ProjectDownloadTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Utils\Site;
19 use Drupal\Console\Utils\Validator;
20 use Drupal\Core\ProxyClass\Extension\ModuleInstaller;
21 use Drupal\Console\Core\Utils\ChainQueue;
22 use Drupal\Core\Config\ConfigFactory;
24 class UninstallCommand extends Command
27 use ProjectDownloadTrait;
35 * @var ModuleInstaller
37 protected $moduleInstaller;
42 protected $chainQueue;
47 protected $configFactory;
51 * InstallCommand constructor.
54 * @param Validator $validator
55 * @param ChainQueue $chainQueue
56 * @param ConfigFactory $configFactory
58 public function __construct(
60 ModuleInstaller $moduleInstaller,
61 ChainQueue $chainQueue,
62 ConfigFactory $configFactory
65 $this->moduleInstaller = $moduleInstaller;
66 $this->chainQueue = $chainQueue;
67 $this->configFactory = $configFactory;
68 parent::__construct();
74 protected function configure()
77 ->setName('module:uninstall')
78 ->setDescription($this->trans('commands.module.uninstall.description'))
81 InputArgument::IS_ARRAY,
82 $this->trans('commands.module.uninstall.questions.module')
87 InputOption::VALUE_NONE,
88 $this->trans('commands.module.uninstall.options.force')
93 InputOption::VALUE_NONE,
94 $this->trans('commands.module.uninstall.options.composer')
100 protected function interact(InputInterface $input, OutputInterface $output)
102 $io = new DrupalStyle($input, $output);
103 $module = $input->getArgument('module');
106 $module = $this->modulesUninstallQuestion($io);
107 $input->setArgument('module', $module);
113 protected function execute(InputInterface $input, OutputInterface $output)
115 $io = new DrupalStyle($input, $output);
116 $composer = $input->getOption('composer');
117 $module = $input->getArgument('module');
119 $this->site->loadLegacyFile('/core/modules/system/system.module');
121 $coreExtension = $this->configFactory->getEditable('core.extension');
123 // Get info about modules available
124 $moduleData = system_rebuild_module_data();
125 $moduleList = array_combine($module, $module);
128 //@TODO: check with Composer if the module is previously required in composer.json!
129 foreach ($module as $moduleItem) {
131 'composer remove drupal/%s ',
135 $shellProcess = $this->get('shell_process');
136 if ($shellProcess->exec($command)) {
139 $this->trans('commands.module.uninstall.messages.composer-success'),
147 if ($missingModules = array_diff_key($moduleList, $moduleData)) {
150 $this->trans('commands.module.uninstall.messages.missing'),
151 implode(', ', $module),
152 implode(', ', $missingModules)
159 $installedModules = $coreExtension->get('module') ?: [];
160 if (!$moduleList = array_intersect_key($moduleList, $installedModules)) {
161 $io->info($this->trans('commands.module.uninstall.messages.nothing'));
166 if (!$force = $input->getOption('force')) {
168 while (list($module) = each($moduleList)) {
169 foreach (array_keys($moduleData[$module]->required_by) as $dependency) {
170 if (isset($installedModules[$dependency]) && !isset($moduleList[$dependency]) && $dependency != $profile) {
171 $dependencies[] = $dependency;
176 if (!empty($dependencies)) {
179 $this->trans('commands.module.uninstall.messages.dependents'),
180 implode('", "', $moduleList),
181 implode(', ', $dependencies)
190 $this->moduleInstaller->uninstall($moduleList);
194 $this->trans('commands.module.uninstall.messages.success'),
195 implode(', ', $moduleList)
201 $this->trans('commands.module.uninstall.messages.composer-success'),
202 implode(', ', $moduleList),
206 } catch (\Exception $e) {
207 $io->error($e->getMessage());
212 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);