5 * Contains \Drupal\Console\Command\Module\UninstallCommand.
8 namespace Drupal\Console\Command\Module;
10 use Drupal\Console\Extension\Manager;
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 Drupal\Console\Core\Command\Command;
16 use Drupal\Console\Command\Shared\ProjectDownloadTrait;
17 use Drupal\Console\Utils\Site;
18 use Drupal\Core\Extension\ModuleInstallerInterface;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Core\Config\ConfigFactoryInterface;
22 class UninstallCommand extends Command
24 use ProjectDownloadTrait;
32 * @var ModuleInstaller
34 protected $moduleInstaller;
39 protected $chainQueue;
44 protected $configFactory;
49 protected $extensionManager;
52 * InstallCommand constructor.
55 * @param ModuleInstaller $moduleInstaller
56 * @param ChainQueue $chainQueue
57 * @param ConfigFactory $configFactory
58 * @param Manager $extensionManager
60 public function __construct(
62 ModuleInstallerInterface $moduleInstaller,
63 ChainQueue $chainQueue,
64 ConfigFactoryInterface $configFactory,
65 Manager $extensionManager
68 $this->moduleInstaller = $moduleInstaller;
69 $this->chainQueue = $chainQueue;
70 $this->configFactory = $configFactory;
71 $this->extensionManager = $extensionManager;
72 parent::__construct();
78 protected function configure()
81 ->setName('module:uninstall')
82 ->setDescription($this->trans('commands.module.uninstall.description'))
85 InputArgument::IS_ARRAY,
86 $this->trans('commands.module.uninstall.questions.module')
91 InputOption::VALUE_NONE,
92 $this->trans('commands.module.uninstall.options.force')
97 InputOption::VALUE_NONE,
98 $this->trans('commands.module.uninstall.options.composer')
100 ->setAliases(['mou']);
105 protected function interact(InputInterface $input, OutputInterface $output)
107 $module = $input->getArgument('module');
110 $module = $this->modulesUninstallQuestion();
111 $input->setArgument('module', $module);
117 protected function execute(InputInterface $input, OutputInterface $output)
119 $composer = $input->getOption('composer');
120 $module = $input->getArgument('module');
122 $this->site->loadLegacyFile('/core/modules/system/system.module');
124 $coreExtension = $this->configFactory->getEditable('core.extension');
126 // Get info about modules available
127 $moduleData = system_rebuild_module_data();
128 $moduleList = array_combine($module, $module);
131 //@TODO: check with Composer if the module is previously required in composer.json!
132 foreach ($module as $moduleItem) {
134 'composer remove drupal/%s ',
138 $shellProcess = $this->get('shell_process');
139 if ($shellProcess->exec($command)) {
140 $this->getIo()->success(
142 $this->trans('commands.module.uninstall.messages.composer-success'),
150 if ($missingModules = array_diff_key($moduleList, $moduleData)) {
151 $this->getIo()->error(
153 $this->trans('commands.module.uninstall.messages.missing'),
154 implode(', ', $module),
155 implode(', ', $missingModules)
162 $installedModules = $coreExtension->get('module') ?: [];
163 if (!$moduleList = array_intersect_key($moduleList, $installedModules)) {
164 $this->getIo()->info($this->trans('commands.module.uninstall.messages.nothing'));
169 if (!$force = $input->getOption('force')) {
171 // Get a list of installed profiles that will be excluded when calculating
172 // the dependency tree.
173 if (\Drupal::hasService('profile_handler')) {
174 // #1356276 adds the profile_handler service but it hasn't been committed
175 // to core yet so we need to check if it exists.
176 $profiles = \Drupal::service('profile_handler')->getProfileInheritance();
178 $profiles[drupal_get_profile()] = [];
182 while (list($module) = each($moduleList)) {
183 foreach (array_keys($moduleData[$module]->required_by) as $dependency) {
184 if (isset($installedModules[$dependency]) && !isset($moduleList[$dependency]) && (!array_key_exists($dependency, $profiles))) {
185 $dependencies[] = $dependency;
190 if (!empty($dependencies)) {
191 $this->getIo()->error(
193 $this->trans('commands.module.uninstall.messages.dependents'),
194 implode('", "', $moduleList),
195 implode(', ', $dependencies)
204 $this->moduleInstaller->uninstall($moduleList);
206 $this->getIo()->info(
208 $this->trans('commands.module.uninstall.messages.success'),
209 implode(', ', $moduleList)
213 $this->getIo()->comment(
215 $this->trans('commands.module.uninstall.messages.composer-success'),
216 implode(', ', $moduleList),
220 } catch (\Exception $e) {
221 $this->getIo()->error($e->getMessage());
226 $this->site->removeCachedServicesFile();
227 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);