addOption( self::INPUT_KEY_FORCE, 'f', InputOption::VALUE_NONE, 'Bypass dependencies check' ); $this->addOption( self::INPUT_KEY_ALL, null, InputOption::VALUE_NONE, ($this->isEnable() ? 'Enable' : 'Disable') . ' all modules' ); parent::configure(); } /** * {@inheritdoc} */ protected function isModuleRequired() { return false; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $isEnable = $this->isEnable(); if ($input->getOption(self::INPUT_KEY_ALL)) { /** @var \Magento\Framework\Module\FullModuleList $fullModulesList */ $fullModulesList = $this->objectManager->get(\Magento\Framework\Module\FullModuleList::class); $modules = $fullModulesList->getNames(); } else { $modules = $input->getArgument(self::INPUT_KEY_MODULES); } $messages = $this->validate($modules); if (!empty($messages)) { $output->writeln(implode(PHP_EOL, $messages)); // we must have an exit code higher than zero to indicate something was wrong return Cli::RETURN_FAILURE; } try { $modulesToChange = $this->getStatus()->getModulesToChange($isEnable, $modules); } catch (\LogicException $e) { $output->writeln('' . $e->getMessage() . ''); // we must have an exit code higher than zero to indicate something was wrong return Cli::RETURN_FAILURE; } if (!empty($modulesToChange)) { $force = $input->getOption(self::INPUT_KEY_FORCE); if (!$force) { $constraints = $this->getStatus()->checkConstraints($isEnable, $modulesToChange); if ($constraints) { $output->writeln( "Unable to change status of modules because of the following constraints:" ); $output->writeln('' . implode("\n", $constraints) . ''); // we must have an exit code higher than zero to indicate something was wrong return Cli::RETURN_FAILURE; } } $this->setIsEnabled($isEnable, $modulesToChange, $output); $this->cleanup($input, $output); $this->getGeneratedFiles()->requestRegeneration(); if ($force) { $output->writeln( 'Alert: You used the --force option.' . ' As a result, modules might not function properly.' ); } } else { $output->writeln('No modules were changed.'); } return Cli::RETURN_SUCCESS; } /** * Enable/disable modules * * @param bool $isEnable * @param string[] $modulesToChange * @param OutputInterface $output * @return void */ private function setIsEnabled($isEnable, $modulesToChange, $output) { $this->getStatus()->setIsEnabled($isEnable, $modulesToChange); if ($isEnable) { $output->writeln('The following modules have been enabled:'); $output->writeln('- ' . implode("\n- ", $modulesToChange) . ''); $output->writeln(''); if ($this->getDeploymentConfig()->isAvailable()) { $output->writeln( 'To make sure that the enabled modules are properly registered,' . " run 'setup:upgrade'." ); } } else { $output->writeln('The following modules have been disabled:'); $output->writeln('- ' . implode("\n- ", $modulesToChange) . ''); $output->writeln(''); } } /** * Get module status * * @return Status */ private function getStatus() { return $this->objectManager->get(Status::class); } /** * Validate list of modules and return error messages * * @param string[] $modules * @return string[] */ protected function validate(array $modules) { $messages = []; if (empty($modules)) { $messages[] = 'No modules specified. Specify a space-separated list of modules' . ' or use the --all option'; } return $messages; } /** * Is it "enable" or "disable" command * * @return bool */ abstract protected function isEnable(); /** * Get deployment config * * @return DeploymentConfig * @deprecated 2.0.6 */ private function getDeploymentConfig() { if (!($this->deploymentConfig instanceof DeploymentConfig)) { return $this->objectManager->get(DeploymentConfig::class); } return $this->deploymentConfig; } /** * Get deployment config * * @return GeneratedFiles * @deprecated 2.1.0 */ private function getGeneratedFiles() { if (!($this->generatedFiles instanceof GeneratedFiles)) { return $this->objectManager->get(GeneratedFiles::class); } return $this->generatedFiles; } }