Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ModuleStatusCommand.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Input\InputArgument;
18 
23 {
29  private $objectManagerProvider;
30 
36  public function __construct(ObjectManagerProvider $objectManagerProvider)
37  {
38  $this->objectManagerProvider = $objectManagerProvider;
39  parent::__construct();
40  }
41 
45  protected function configure()
46  {
47  $this->setName('module:status')
48  ->setDescription('Displays status of modules')
49  ->addArgument('module', InputArgument::OPTIONAL, 'Optional module name')
50  ->addOption('enabled', null, null, 'Print only enabled modules')
51  ->addOption('disabled', null, null, 'Print only disabled modules');
52  parent::configure();
53  }
54 
58  protected function execute(InputInterface $input, OutputInterface $output)
59  {
60  $moduleName = (string)$input->getArgument('module');
61  if ($moduleName) {
62  return $this->showSpecificModule($moduleName, $output);
63  }
64 
65  $onlyEnabled = $input->getOption('enabled');
66  if ($onlyEnabled) {
67  return $this->showEnabledModules($output);
68  }
69 
70  $onlyDisabled = $input->getOption('disabled');
71  if ($onlyDisabled) {
72  return $this->showDisabledModules($output);
73  }
74 
75  $output->writeln('<info>List of enabled modules:</info>');
76  $this->showEnabledModules($output);
77  $output->writeln('');
78 
79  $output->writeln("<info>List of disabled modules:</info>");
80  $this->showDisabledModules($output);
81  $output->writeln('');
82  }
83 
88  private function showSpecificModule(string $moduleName, OutputInterface $output)
89  {
90  $allModules = $this->getAllModules();
91  if (!in_array($moduleName, $allModules->getNames())) {
92  $output->writeln('<error>Module does not exist</error>');
93  return Cli::RETURN_FAILURE;
94  }
95 
96  $enabledModules = $this->getEnabledModules();
97  if (in_array($moduleName, $enabledModules->getNames())) {
98  $output->writeln('<info>Module is enabled</info>');
99  return Cli::RETURN_FAILURE;
100  }
101 
102  $output->writeln('<info>Module is disabled</info>');
103  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
104  }
105 
109  private function showEnabledModules(OutputInterface $output)
110  {
111  $enabledModules = $this->getEnabledModules();
112  $enabledModuleNames = $enabledModules->getNames();
113  if (count($enabledModuleNames) === 0) {
114  $output->writeln('None');
115  return Cli::RETURN_FAILURE;
116  }
117 
118  $output->writeln(join("\n", $enabledModuleNames));
119  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
120  }
121 
125  private function showDisabledModules(OutputInterface $output)
126  {
127  $disabledModuleNames = $this->getDisabledModuleNames();
128  if (count($disabledModuleNames) === 0) {
129  $output->writeln('None');
130  return Cli::RETURN_FAILURE;
131  }
132 
133  $output->writeln(join("\n", $disabledModuleNames));
134  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
135  }
136 
140  private function getAllModules(): FullModuleList
141  {
142  return $this->objectManagerProvider->get()->create(FullModuleList::class);
143  }
144 
148  private function getEnabledModules(): ModuleList
149  {
150  return $this->objectManagerProvider->get()->create(ModuleList::class);
151  }
152 
156  private function getDisabledModuleNames(): array
157  {
158  $fullModuleList = $this->getAllModules();
159  $enabledModules = $this->getEnabledModules();
160  return array_diff($fullModuleList->getNames(), $enabledModules->getNames());
161  }
162 }
__construct(ObjectManagerProvider $objectManagerProvider)
execute(InputInterface $input, OutputInterface $output)