Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add module status to dev:module:list command #1456

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 +1007,14 @@ n98-magerun2.phar dev:translate:export [--store=<storecode>] <locale> <filename>
### List modules

```sh
n98-magerun2.phar dev:module:list [--vendor=VENDOR]
n98-magerun2.phar dev:module:list [--vendor [VENDOR]] [-e|--only-enabled] [-d|--only-disabled] [--format [FORMAT]]
```

Lists all installed modules. If `--vendor` option is set, only modules of the given vendor are listed.
If `--only-enabled` option is set, only enabled modules are listed.
If `--only-disabled` option is set, only disabled modules are listed.
Format can be `csv`, `json`, `xml` or `yaml`.

### Encryption

Encrypt the given string using Magentos crypt key
Expand Down
57 changes: 49 additions & 8 deletions src/N98/Magento/Command/Developer/Module/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace N98\Magento\Command\Developer\Module;

use Magento\Framework\Module\FullModuleList;
use Magento\Framework\Module\Manager;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
use Symfony\Component\Console\Command\Command;
Expand All @@ -21,21 +23,31 @@ class ListCommand extends AbstractMagentoCommand
protected $moduleList;

/**
* @var \Magento\Framework\Module\ModuleListInterface
* @var \Magento\Framework\Module\FullModuleList
*/
protected $moduleListObject;

/**
* @var Manager
*/
protected $moduleManager;

/**
* @return array
*/
public function getModuleList()
{
return $this->moduleList;
}

/**
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
* @param FullModuleList $moduleList
* @param Manager $moduleManager
*/
public function inject(\Magento\Framework\Module\ModuleListInterface $moduleList)
public function inject(\Magento\Framework\Module\FullModuleList $moduleList, Manager $moduleManager)
{
$this->moduleListObject = $moduleList;
$this->moduleManager = $moduleManager;
}

protected function configure()
Expand All @@ -49,6 +61,8 @@ protected function configure()
'Show modules of a specific vendor (case insensitive)'
)
->setDescription('List all installed modules')
->addOption('only-enabled', 'e', InputOption::VALUE_NONE, 'Show only enabled modules')
->addOption('only-disabled', 'd', InputOption::VALUE_NONE, 'Show only disabled modules')
->addOption(
'format',
null,
Expand All @@ -65,35 +79,62 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('only-disabled') && $input->getOption('only-enabled')) {
throw new \Exception('You can only use one of the options --only-enabled or --only-disabled');
}

$this->detectMagento($output, true);

if ($input->getOption('format') == null) {
$this->writeSection($output, 'Magento Modules');
}

$this->initMagento();
$this->prepareModuleList($input->getOption('vendor'));
$this->prepareModuleList($input);

$this->getHelper('table')
->setHeaders(['Name', '(Schema) Version'])
->setHeaders(['Name', '(Schema) Version', 'Status'])
->renderByFormat($output, $this->moduleList, $input->getOption('format'));

return Command::SUCCESS;
}

protected function prepareModuleList($vendor)
/**
* @param string $vendor
* @return void
*/
protected function prepareModuleList(InputInterface $input)
{
$this->moduleList = [];

foreach ($this->moduleListObject->getAll() as $moduleName => $info) {
$vendor = $input->getOption('vendor');

foreach ($this->moduleListObject->getNames() as $moduleName) {

$info = $this->moduleListObject->getOne($moduleName);

// First index is (probably always) vendor
$moduleNameData = explode('_', $moduleName);

if ($vendor !== null && strtolower($moduleNameData[0]) !== strtolower($vendor)) {
continue;
}

$this->moduleList[] = [$info['name'], $info['setup_version']];
$isEnabled = $this->moduleManager->isEnabled($moduleName);

if ($isEnabled && $input->getOption('only-disabled')) {
continue;
}

if (!$isEnabled && $input->getOption('only-enabled')) {
continue;
}

$this->moduleList[] = [
$info['name'],
$info['setup_version'],
$isEnabled ? 'enabled' : 'disabled',
];
}
}
}