From 497ca81c02761d6f04664dff4af065db93cf4dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCnch?= Date: Tue, 24 Jan 2023 17:09:51 +0100 Subject: [PATCH] New command config:data:mview #1042 --- README.md | 16 ++ config.yaml | 1 + .../Command/Config/Data/IndexerCommand.php | 48 +++-- .../Command/Config/Data/MViewCommand.php | 170 ++++++++++++++++++ tests/phar-test.sh | 3 + 5 files changed, 224 insertions(+), 14 deletions(-) create mode 100644 src/N98/Magento/Command/Config/Data/MViewCommand.php diff --git a/README.md b/README.md index 4cd3bf9d..0aaa1acf 100644 --- a/README.md +++ b/README.md @@ -538,6 +538,22 @@ n98-magerun2.phar config:data:di |----------------|---------------------------------------------------------------------------------------------------------| | `--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. diff --git a/config.yaml b/config.yaml index 0a1248a1..9f960e73 100644 --- a/config.yaml +++ b/config.yaml @@ -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 diff --git a/src/N98/Magento/Command/Config/Data/IndexerCommand.php b/src/N98/Magento/Command/Config/Data/IndexerCommand.php index aa230f58..d3996757 100644 --- a/src/N98/Magento/Command/Config/Data/IndexerCommand.php +++ b/src/N98/Magento/Command/Config/Data/IndexerCommand.php @@ -73,27 +73,47 @@ protected function renderAsTree(array $data, OutputInterface $output) foreach ($data as $row) { $node = $tree->newNode('' . $row['title'] . ''); - $actionClassNode = $node->newNode('indexer_id:'); - $actionClassNode->addValue('' . $row['indexer_id'] . ''); - - $viewIdNode = $node->newNode('view_id:'); - $viewIdNode->addValue('' . $row['view_id'] . ''); + if (!empty($row['indexer_id'])) { + $node->newNode( + sprintf( + 'indexer_id: %s', + $row['indexer_id'] + ) + ); + } - if (count($row['dependencies']) > 0) { - $dependenciesNode = $node->newNode('dependencies:'); - foreach ($row['dependencies'] as $dependency) {# - $dependenciesNode->addValue('' . $dependency . ''); - } + if (!empty($row['view_id'])) { + $node->newNode( + sprintf( + 'view_id: %s', + $row['view_id'] + ) + ); } if (!empty($row['action_class'])) { - $actionClassNode = $node->newNode('action_class:'); - $actionClassNode->addValue('' . $row['action_class'] . ''); + $node->newNode( + sprintf( + 'action_class: %s', + $row['action_class'] + ) + ); } if (!empty($row['shared_index'])) { - $sharedIndexNode = $node->newNode('shared_index:'); - $sharedIndexNode->addValue('' . $row['shared_index'] . ''); + $node->newNode( + sprintf( + 'shared_index: %s', + $row['shared_index'] + ) + ); + } + + if (count($row['dependencies']) > 0) { + $dependenciesNode = $node->newNode('dependencies:'); + foreach ($row['dependencies'] as $dependency) {# + $dependenciesNode->addValue('' . $dependency . ''); + } } } diff --git a/src/N98/Magento/Command/Config/Data/MViewCommand.php b/src/N98/Magento/Command/Config/Data/MViewCommand.php new file mode 100644 index 00000000..1dc0e482 --- /dev/null +++ b/src/N98/Magento/Command/Config/Data/MViewCommand.php @@ -0,0 +1,170 @@ +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('' . $row['view_id'] . ''); + + if (!empty($row['group'])) { + $node->newNode( + sprintf( + 'group: %s', + $row['group'] + ) + ); + } + + if (!empty($row['walker'])) { + $node->newNode( + sprintf( + 'walker: %s', + $row['walker'] + ) + ); + } + + if (!empty($row['action_class'])) { + $node->newNode( + sprintf( + 'action_class: %s', + $row['action_class'] + ) + ); + } + + if (count($row['subscriptions']) > 0) { + $dependenciesNode = $node->newNode('subscriptions:'); + foreach ($row['subscriptions'] as $subscriptionKey => $subscriptionData) { + $subscriptionNode = $dependenciesNode->newNode('' . $subscriptionKey . ''); + $subscriptionNode + ->newNode('column: ' . $subscriptionData['column'] . ''); + if (!empty($subscriptionData['subscription_model'])) { + $subscriptionNode + ->newNode( + sprintf( + 'subscription_model:%s', + $subscriptionData['subscription_model'] + ) + ); + } + + if (!empty($subscriptionData['additional_columns'])) { + $subscriptionNode + ->newNode('additional_columns:') + ->addValue('' . implode(',', $subscriptionData['additional_columns']) . ''); + } + } + } + } + + $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); + } +} diff --git a/tests/phar-test.sh b/tests/phar-test.sh index 5937e5ac..1a392d87 100755 --- a/tests/phar-test.sh +++ b/tests/phar-test.sh @@ -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"