Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractModuleManageCommand.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputOption;
16 
18 {
22  const INPUT_KEY_ALL = 'all';
23  const INPUT_KEY_FORCE = 'force';
24 
28  protected $generatedFiles;
29 
33  protected $deploymentConfig;
34 
38  protected function configure()
39  {
40  $this->addOption(
41  self::INPUT_KEY_FORCE,
42  'f',
43  InputOption::VALUE_NONE,
44  'Bypass dependencies check'
45  );
46  $this->addOption(
47  self::INPUT_KEY_ALL,
48  null,
49  InputOption::VALUE_NONE,
50  ($this->isEnable() ? 'Enable' : 'Disable') . ' all modules'
51  );
52 
53  parent::configure();
54  }
55 
59  protected function isModuleRequired()
60  {
61  return false;
62  }
63 
67  protected function execute(InputInterface $input, OutputInterface $output)
68  {
69  $isEnable = $this->isEnable();
70  if ($input->getOption(self::INPUT_KEY_ALL)) {
72  $fullModulesList = $this->objectManager->get(\Magento\Framework\Module\FullModuleList::class);
73  $modules = $fullModulesList->getNames();
74  } else {
75  $modules = $input->getArgument(self::INPUT_KEY_MODULES);
76  }
77  $messages = $this->validate($modules);
78  if (!empty($messages)) {
79  $output->writeln(implode(PHP_EOL, $messages));
80  // we must have an exit code higher than zero to indicate something was wrong
81  return Cli::RETURN_FAILURE;
82  }
83  try {
84  $modulesToChange = $this->getStatus()->getModulesToChange($isEnable, $modules);
85  } catch (\LogicException $e) {
86  $output->writeln('<error>' . $e->getMessage() . '</error>');
87  // we must have an exit code higher than zero to indicate something was wrong
88  return Cli::RETURN_FAILURE;
89  }
90  if (!empty($modulesToChange)) {
91  $force = $input->getOption(self::INPUT_KEY_FORCE);
92  if (!$force) {
93  $constraints = $this->getStatus()->checkConstraints($isEnable, $modulesToChange);
94  if ($constraints) {
95  $output->writeln(
96  "<error>Unable to change status of modules because of the following constraints:</error>"
97  );
98  $output->writeln('<error>' . implode("</error>\n<error>", $constraints) . '</error>');
99  // we must have an exit code higher than zero to indicate something was wrong
100  return Cli::RETURN_FAILURE;
101  }
102  }
103  $this->setIsEnabled($isEnable, $modulesToChange, $output);
104  $this->cleanup($input, $output);
105  $this->getGeneratedFiles()->requestRegeneration();
106  if ($force) {
107  $output->writeln(
108  '<error>Alert: You used the --force option.'
109  . ' As a result, modules might not function properly.</error>'
110  );
111  }
112  } else {
113  $output->writeln('<info>No modules were changed.</info>');
114  }
115  return Cli::RETURN_SUCCESS;
116  }
117 
126  private function setIsEnabled($isEnable, $modulesToChange, $output)
127  {
128  $this->getStatus()->setIsEnabled($isEnable, $modulesToChange);
129  if ($isEnable) {
130  $output->writeln('<info>The following modules have been enabled:</info>');
131  $output->writeln('<info>- ' . implode("\n- ", $modulesToChange) . '</info>');
132  $output->writeln('');
133  if ($this->getDeploymentConfig()->isAvailable()) {
134  $output->writeln(
135  '<info>To make sure that the enabled modules are properly registered,'
136  . " run 'setup:upgrade'.</info>"
137  );
138  }
139  } else {
140  $output->writeln('<info>The following modules have been disabled:</info>');
141  $output->writeln('<info>- ' . implode("\n- ", $modulesToChange) . '</info>');
142  $output->writeln('');
143  }
144  }
145 
151  private function getStatus()
152  {
153  return $this->objectManager->get(Status::class);
154  }
155 
162  protected function validate(array $modules)
163  {
164  $messages = [];
165  if (empty($modules)) {
166  $messages[] = '<error>No modules specified. Specify a space-separated list of modules' .
167  ' or use the --all option</error>';
168  }
169  return $messages;
170  }
171 
177  abstract protected function isEnable();
178 
185  private function getDeploymentConfig()
186  {
187  if (!($this->deploymentConfig instanceof DeploymentConfig)) {
188  return $this->objectManager->get(DeploymentConfig::class);
189  }
191  }
192 
199  private function getGeneratedFiles()
200  {
201  if (!($this->generatedFiles instanceof GeneratedFiles)) {
202  return $this->objectManager->get(GeneratedFiles::class);
203  }
204  return $this->generatedFiles;
205  }
206 }
cleanup($repo, $mainline)