Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

dev:di:preferences:list command #1553

Merged
merged 3 commits into from
Dec 19, 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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,8 @@ Creates an empty module and registers it in current Magento shop.
n98-magerun2.phar dev:module:create [-m|--minimal] [--add-blocks] [--add-helpers] [--add-models] [--add-setup] [--add-all] [-e|--enable] [--modman] [--add-readme] [--add-composer] [--add-strict-types] [--author-name [AUTHOR-NAME]] [--author-email [AUTHOR-EMAIL]] [--description [DESCRIPTION]] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [--root-dir [ROOT-DIR]] [--skip-config] [--skip-root-check] [--skip-core-commands [SKIP-CORE-COMMANDS]] [--skip-magento-compatibility-check] [--] <command> <vendorNamespace> <moduleName>
```

---

### Detect Composer Dependencies in Module

The source code of one or more modules can be scanned for dependencies.
Expand All @@ -978,6 +980,10 @@ The source code of one or more modules can be scanned for dependencies.
n98-magerun2.phar dev:module:detect-composer-dependencies [--only-missing] <directory>
```

---

### Translations

The `--only-missing` option will filter the output so that only the missing dependencies are listed.

Enable/disable inline translation feature for Magento Admin:
Expand All @@ -1004,6 +1010,22 @@ Export inline translations
n98-magerun2.phar dev:translate:export [--store=<storecode>] <locale> <filename>
```

---

### DI

List Preferences:

```sh
n98-magerun2.phar dev:di:preferece:list [--format [FORMAT]] [<area>]
```

`area` is one of [global, adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql, doc, admin]

Format can be `csv`, `json`, `xml` or `yaml`.

---

### List modules

```sh
Expand Down
5 changes: 3 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ commands:
- N98\Magento\Command\Design\DemoNoticeCommand
- N98\Magento\Command\Developer\Asset\ClearCommand
- N98\Magento\Command\Developer\ConsoleCommand
- N98\Magento\Command\Developer\Di\Preference\ListCommand
- N98\Magento\Command\Developer\EncryptCommand
- N98\Magento\Command\Developer\DecryptCommand
- N98\Magento\Command\Developer\Module\CreateCommand
- N98\Magento\Command\Developer\Module\DetectComposerDependenciesCommand
- N98\Magento\Command\Developer\Module\ListCommand
- N98\Magento\Command\Developer\Module\Observer\ListCommand
- N98\Magento\Command\Developer\Report\CountCommand
- N98\Magento\Command\Developer\SymlinksCommand
- N98\Magento\Command\Developer\TemplateHintsBlocksCommand
Expand Down Expand Up @@ -171,8 +174,6 @@ commands:
- N98\Magento\Command\Integration\ListCommand
- N98\Magento\Command\Integration\ShowCommand
- N98\Magento\Command\Installer\InstallCommand
- N98\Magento\Command\Developer\Module\ListCommand
- N98\Magento\Command\Developer\Module\Observer\ListCommand
- N98\Magento\Command\ScriptCommand
- N98\Magento\Command\SelfUpdateCommand
- N98\Magento\Command\Route\ListCommand
Expand Down
113 changes: 113 additions & 0 deletions src/N98/Magento/Command/Developer/Di/Preference/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace N98\Magento\Command\Developer\Di\Preference;

use Exception;
use InvalidArgumentException;
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

/**
* Class ListCommand
* @package N98\Magento\Command\Developer\Module\Preference
*/
class ListCommand extends AbstractMagentoCommand
{
protected $areas = [
'global',
'adminhtml',
'frontend',
'crontab',
'webapi_rest',
'webapi_soap',
'graphql',
'doc',

// 'admin' has been declared deprecated since 5448233
// https://github.com/magento/magento2/commit/5448233#diff-5bc6336cfbfd5aeb18404416f508b6c4
'admin',
];

protected function configure()
{
$this
->setName('dev:di:preferences:list')
->setDescription('Lists all registered preferences')
->addArgument(
'area',
InputArgument::OPTIONAL,
'Filter observers in specific area. One of [' . implode(',', $this->areas) . ']'
)
->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL,
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
);
}

protected function interact(InputInterface $input, OutputInterface $output)
{
$area = $input->getArgument('area');

if ($area === null || !in_array($area, $this->areas)) {
$choices = [];
foreach ($this->areas as $key => $area) {
$choices[$key + 1] = '<comment>[' . $area . ']</comment> ';
}

$question = new ChoiceQuestion('<question>Please select an area:</question>', $choices);
$question->setValidator(function ($areaIndex) {
if (!in_array($areaIndex - 1, range(0, count($this->areas) - 1), true)) {
throw new InvalidArgumentException('Invalid selection.' . $areaIndex);
}

return $this->areas[$areaIndex - 1];
});
$area = $this->getHelper('question')->ask($input, $output, $question);
}
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->detectMagento($output);
if (!$this->initMagento()) {
return Command::FAILURE;
}

$configLoader = $this->getObjectManager()->get(ConfigLoaderInterface::class);
$data = $configLoader->load($input->getArgument('area'));

$table = [];

foreach ($data['preferences'] as $for => $type) {
$table[] = [
$for,
$type,
];
}

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

$this->getHelper('table')
->setHeaders(['for', 'type'])
->renderByFormat($output, $table, $input->getOption('format'));

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace N98\Magento\Command\Developer\Module\Observer;

use Exception;
use InvalidArgumentException;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -25,6 +27,7 @@ class ListCommand extends AbstractMagentoCommand
'crontab',
'webapi_rest',
'webapi_soap',
'graphql',
'doc',

// 'admin' has been declared deprecated since 5448233
Expand Down Expand Up @@ -63,7 +66,7 @@ protected function configure()

/**
* {@inheritdoc}
* @throws \Exception
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -82,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$question = new ChoiceQuestion('<question>Please select an area:</question>', $choices);
$question->setValidator(function ($areaIndex) {
if (!in_array($areaIndex - 1, range(0, count($this->areas) - 1), true)) {
throw new \InvalidArgumentException('Invalid selection.' . $areaIndex);
throw new InvalidArgumentException('Invalid selection.' . $areaIndex);
}

return $this->areas[$areaIndex - 1];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace N98\Magento\Command\Developer\Di\Preference;

use N98\Magento\Command\TestCase;

class ListCommandTest extends TestCase
{
public function testGlobalList()
{
$this->assertDisplayContains(
['command' => 'dev:di:preference:list', 'area' => 'global'],
'Magento\Store\Api\Data\StoreInterface'
);
}

public function testCrontabList()
{
$this->assertDisplayContains(
['command' => 'dev:di:preference:list', 'area' => 'crontab'],
'Magento\Backend\App\ConfigInterface'
);
}
}
8 changes: 8 additions & 0 deletions tests/bats/functional_magerun_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ function cleanup_files_in_magento() {
cleanup_files_in_magento "app/code/N98/Magerun123"
}

@test "Command: dev:di:preference:list" {
run $BIN "dev:di:preference:list" global
assert_output --partial "Magento\Store\Api\Data\StoreInterface"

run $BIN "dev:di:preference:list" crontab
assert_output --partial "Magento\Backend\App\ConfigInterface"
}

@test "Command: dev:module:detect-composer-dependencies" {
if [ -d "${N98_MAGERUN2_TEST_MAGENTO_ROOT}/vendor/magento/module-catalog-rule" ]; then
run $BIN "dev:module:detect-composer-dependencies" "${N98_MAGERUN2_TEST_MAGENTO_ROOT}/vendor/magento/module-catalog-rule"
Expand Down
Loading