Skip to content

Commit

Permalink
New command config:data:mview #1042
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Jan 24, 2023
1 parent a8ca942 commit 497ca81
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 14 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,22 @@ n98-magerun2.phar config:data:di <type>
|----------------|---------------------------------------------------------------------------------------------------------|
| `--scope` `-s` | Config scope (`global`, `adminhtml`, `frontend`, `webapi_rest`, `webapi_soap`, ...) (default: `global`) |
### Print MView Config
Print the data of all merged mview.xml files.
```sh
n98-magerun2.phar config:data:mview [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` |
### Print Indexer Config
Print the data of all merged indexer.xml files.
Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ commands:
- N98\Magento\Command\Config\Store\SetCommand
- N98\Magento\Command\Config\Data\AclCommand
- N98\Magento\Command\Config\Data\DiCommand
- N98\Magento\Command\Config\Data\MViewCommand
- N98\Magento\Command\Config\Data\IndexerCommand
- N98\Magento\Command\Customer\CreateCommand
- N98\Magento\Command\Customer\DeleteCommand
Expand Down
48 changes: 34 additions & 14 deletions src/N98/Magento/Command/Config/Data/IndexerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,47 @@ protected function renderAsTree(array $data, OutputInterface $output)
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 (!empty($row['indexer_id'])) {
$node->newNode(
sprintf(
'<info>indexer_id: </info><comment>%s</comment>',
$row['indexer_id']
)
);
}

if (count($row['dependencies']) > 0) {
$dependenciesNode = $node->newNode('dependencies:');
foreach ($row['dependencies'] as $dependency) {#
$dependenciesNode->addValue('<comment>' . $dependency . '</comment>');
}
if (!empty($row['view_id'])) {
$node->newNode(
sprintf(
'<info>view_id: </info><comment>%s</comment>',
$row['view_id']
)
);
}

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

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

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

Expand Down
170 changes: 170 additions & 0 deletions src/N98/Magento/Command/Config/Data/MViewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace N98\Magento\Command\Config\Data;

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;
use Symfony\Component\Console\Output\OutputInterface;

class MViewCommand extends AbstractMagentoCommand
{
/**
* @var \Magento\Framework\Mview\Config\Reader
*/
private $mviewConfigReader;

protected function configure()
{
$this
->setName('config:data:mview')
->addOption(
'scope',
's',
InputOption::VALUE_OPTIONAL,
'Config scope (global, adminhtml, frontend, graphql, webapi_rest, webapi_soap, ...)',
'global'
)
->addOption('tree', 't', InputOption::VALUE_NONE, 'Show data as tree')
->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL,
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
)
->setDescription('Dump merged data of mview.xml files');
}

/**
* @param \Magento\Framework\Mview\Config\Reader $mviewConfigReader
*/
public function inject(\Magento\Framework\Mview\Config\Reader $mviewConfigReader)
{
$this->mviewConfigReader = $mviewConfigReader;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->mviewConfigReader->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('MView Data Tree');

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

if (!empty($row['group'])) {
$node->newNode(
sprintf(
'<info>group: </info><comment>%s</comment>',
$row['group']
)
);
}

if (!empty($row['walker'])) {
$node->newNode(
sprintf(
'<info>walker: </info><comment>%s</comment>',
$row['walker']
)
);
}

if (!empty($row['action_class'])) {
$node->newNode(
sprintf(
'<info>action_class: </info><comment>%s</comment>',
$row['action_class']
)
);
}

if (count($row['subscriptions']) > 0) {
$dependenciesNode = $node->newNode('subscriptions:');
foreach ($row['subscriptions'] as $subscriptionKey => $subscriptionData) {
$subscriptionNode = $dependenciesNode->newNode('<comment>' . $subscriptionKey . '</comment>');
$subscriptionNode
->newNode('<info>column: </info><comment>' . $subscriptionData['column'] . '</comment>');
if (!empty($subscriptionData['subscription_model'])) {
$subscriptionNode
->newNode(
sprintf(
'<info>subscription_model:</info><comment>%s</comment>',
$subscriptionData['subscription_model']
)
);
}

if (!empty($subscriptionData['additional_columns'])) {
$subscriptionNode
->newNode('additional_columns:')
->addValue('<comment>' . implode(',', $subscriptionData['additional_columns']) . '</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 $row) {
$table[] = [
'view_id' => $row['view_id'],
'group' => $row['group'],
'shared_index' => $row['walker'],
'action_class' => $row['action_class'],
'subscriptions' => $this->formatSubscriptions($row['subscriptions']),
];
}

$this->getHelper('table')
->setHeaders(['view_id', 'group', 'walker', 'action_class', 'subscriptions'])
->renderByFormat($output, $table, $input->getOption('format'));
}

/**
* @param array $subscriptions
* @return string
*/
private function formatSubscriptions(array $subscriptions)
{
$return = [];

foreach ($subscriptions as $subscription) {
$return[] = sprintf('%s(%s)', $subscription['name'], $subscription['column']);
}

return implode(',', $return);
}
}
3 changes: 3 additions & 0 deletions tests/phar-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ function test_magerun_commands() {
assert_command_contains "config:data:acl" "ACL Tree"
# config:data:di
assert_command_contains "config:data:di" "DateTimeInterface"
# config:data:mview
assert_command_contains "config:data:mview" "catalog_category_flat"
assert_command_contains "config:data:mview -t" "MView Data Tree"
# config:data:indexer
assert_command_contains "config:data:indexer" "catalog_product_flat"
assert_command_contains "config:data:indexer -t" "Indexer Data Tree"
Expand Down

0 comments on commit 497ca81

Please sign in to comment.