Version 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 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;
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     /**
51      * InstallCommand constructor.
52      *
53      * @param Site          $site
54      * @param Validator     $validator
55      * @param ChainQueue    $chainQueue
56      * @param ConfigFactory $configFactory
57      */
58     public function __construct(
59         Site $site,
60         ModuleInstaller $moduleInstaller,
61         ChainQueue $chainQueue,
62         ConfigFactory $configFactory
63     ) {
64         $this->site = $site;
65         $this->moduleInstaller = $moduleInstaller;
66         $this->chainQueue = $chainQueue;
67         $this->configFactory = $configFactory;
68         parent::__construct();
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     protected function configure()
75     {
76         $this
77             ->setName('module:uninstall')
78             ->setDescription($this->trans('commands.module.uninstall.description'))
79             ->addArgument(
80                 'module',
81                 InputArgument::IS_ARRAY,
82                 $this->trans('commands.module.uninstall.questions.module')
83             )
84             ->addOption(
85                 'force',
86                 '',
87                 InputOption::VALUE_NONE,
88                 $this->trans('commands.module.uninstall.options.force')
89             )
90             ->addOption(
91                 'composer',
92                 '',
93                 InputOption::VALUE_NONE,
94                 $this->trans('commands.module.uninstall.options.composer')
95             );
96     }
97     /**
98      * {@inheritdoc}
99      */
100     protected function interact(InputInterface $input, OutputInterface $output)
101     {
102         $io = new DrupalStyle($input, $output);
103         $module = $input->getArgument('module');
104
105         if (!$module) {
106             $module = $this->modulesUninstallQuestion($io);
107             $input->setArgument('module', $module);
108         }
109     }
110     /**
111      * {@inheritdoc}
112      */
113     protected function execute(InputInterface $input, OutputInterface $output)
114     {
115         $io =  new DrupalStyle($input, $output);
116         $composer = $input->getOption('composer');
117         $module = $input->getArgument('module');
118
119         $this->site->loadLegacyFile('/core/modules/system/system.module');
120
121         $coreExtension = $this->configFactory->getEditable('core.extension');
122
123         // Get info about modules available
124         $moduleData = system_rebuild_module_data();
125         $moduleList = array_combine($module, $module);
126
127         if ($composer) {
128             //@TODO: check with Composer if the module is previously required in composer.json!
129             foreach ($module as $moduleItem) {
130                 $command = sprintf(
131                     'composer remove drupal/%s ',
132                     $moduleItem
133                 );
134
135                 $shellProcess = $this->get('shell_process');
136                 if ($shellProcess->exec($command)) {
137                     $io->success(
138                         sprintf(
139                             $this->trans('commands.module.uninstall.messages.composer-success'),
140                             $moduleItem
141                         )
142                     );
143                 }
144             }
145         }
146
147         if ($missingModules = array_diff_key($moduleList, $moduleData)) {
148             $io->error(
149                 sprintf(
150                     $this->trans('commands.module.uninstall.messages.missing'),
151                     implode(', ', $module),
152                     implode(', ', $missingModules)
153                 )
154             );
155
156             return 1;
157         }
158
159         $installedModules = $coreExtension->get('module') ?: [];
160         if (!$moduleList = array_intersect_key($moduleList, $installedModules)) {
161             $io->info($this->trans('commands.module.uninstall.messages.nothing'));
162
163             return 0;
164         }
165
166         if (!$force = $input->getOption('force')) {
167             $dependencies = [];
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;
172                     }
173                 }
174             }
175
176             if (!empty($dependencies)) {
177                 $io->error(
178                     sprintf(
179                         $this->trans('commands.module.uninstall.messages.dependents'),
180                         implode('", "', $moduleList),
181                         implode(', ', $dependencies)
182                     )
183                 );
184
185                 return 1;
186             }
187         }
188
189         try {
190             $this->moduleInstaller->uninstall($moduleList);
191
192             $io->info(
193                 sprintf(
194                     $this->trans('commands.module.uninstall.messages.success'),
195                     implode(', ', $moduleList)
196                 )
197             );
198
199             $io->comment(
200                 sprintf(
201                     $this->trans('commands.module.uninstall.messages.composer-success'),
202                     implode(', ', $moduleList),
203                     false
204                 )
205             );
206         } catch (\Exception $e) {
207             $io->error($e->getMessage());
208
209             return 1;
210         }
211
212         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
213     }
214 }