Skip to content

Commit

Permalink
feat: dev:di:preferences:list command
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Dec 19, 2024
1 parent 1bcc4b2 commit b8562b5
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
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
@@ -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

0 comments on commit b8562b5

Please sign in to comment.