Skip to content

Commit

Permalink
Add tree renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Jan 24, 2023
1 parent 454b3b5 commit a8ca942
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,17 @@ n98-magerun2.phar config:data:di <type>
Print the data of all merged indexer.xml files.
```sh
n98-magerun2.phar config:data:indexer
n98-magerun2.phar config:data:indexer [options]
```
**Options:**
| Option | Description |
|-------------------|---------------------------------------------------------------|
| `--scope` `-s` | Config scope (`global`, `adminhtml`, `frontend`, `webapi_rest`, `webapi_soap`, ...) (default: `global`) |
| `--tree` `-t` | Print data as tree |
| `--format` | Output as `json`, `xml` or `csv` |
---
### List Magento cache status
Expand Down
59 changes: 56 additions & 3 deletions src/N98/Magento/Command/Config/Data/IndexerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
use PBergman\Console\Helper\TreeHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -27,6 +28,7 @@ protected function configure()
'Config scope (global, adminhtml, frontend, graphql, webapi_rest, webapi_soap, ...)',
'global'
)
->addOption('tree', 't', InputOption::VALUE_NONE, 'Show data as tree')
->addOption(
'format',
null,
Expand Down Expand Up @@ -54,6 +56,58 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->indexConfigReader->read();

if ($input->getOption('tree')) {
$this->renderAsTree($data, $output);
} else {
$this->renderAsTable($data, $output, $input);
}

return Command::SUCCESS;
}

protected function renderAsTree(array $data, OutputInterface $output)
{
$tree = new TreeHelper();
$tree->setTitle('Indexer Data Tree');

foreach ($data as $row) {
$node = $tree->newNode('<info>' . $row['title'] . '</info>');

$actionClassNode = $node->newNode('indexer_id:');
$actionClassNode->addValue('<comment>' . $row['indexer_id'] . '</comment>');

$viewIdNode = $node->newNode('view_id:');
$viewIdNode->addValue('<comment>' . $row['view_id'] . '</comment>');

if (count($row['dependencies']) > 0) {
$dependenciesNode = $node->newNode('dependencies:');
foreach ($row['dependencies'] as $dependency) {#
$dependenciesNode->addValue('<comment>' . $dependency . '</comment>');
}
}

if (!empty($row['action_class'])) {
$actionClassNode = $node->newNode('action_class:');
$actionClassNode->addValue('<comment>' . $row['action_class'] . '</comment>');
}

if (!empty($row['shared_index'])) {
$sharedIndexNode = $node->newNode('shared_index:');
$sharedIndexNode->addValue('<comment>' . $row['shared_index'] . '</comment>');
}
}

$tree->printTree($output);
}

/**
* @param array $data
* @param OutputInterface $output
* @param InputInterface $input
* @return void
*/
protected function renderAsTable(array $data, OutputInterface $output, InputInterface $input): void
{
$table = [];

foreach ($data as $indexer) {
Expand All @@ -63,13 +117,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
'shared_index' => $indexer['shared_index'],
'title' => $indexer['title'],
'dependencies' => implode(',', $indexer['dependencies']),
'action_class' => $indexer['action_class'],
];
}

$this->getHelper('table')
->setHeaders(['indexer_id', 'view_id', 'shared_index', 'title', 'dependencies'])
->setHeaders(['indexer_id', 'view_id', 'shared_index', 'title', 'dependencies', 'action_class'])
->renderByFormat($output, $table, $input->getOption('format'));

return Command::SUCCESS;
}
}
3 changes: 2 additions & 1 deletion tests/phar-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ function test_magerun_commands() {
# config:data:di
assert_command_contains "config:data:di" "DateTimeInterface"
# config:data:indexer
assert_command_contains "config:data:indexer" "catalog_product_flat"
assert_command_contains "config:data:indexer" "catalog_product_flat"
assert_command_contains "config:data:indexer -t" "Indexer Data Tree"
# config:env:create
# config:env:set
assert_command_contains "config:env:set magerun.example foo" "Config magerun.example successfully set to foo"
Expand Down

0 comments on commit a8ca942

Please sign in to comment.