From 0e33dfb7b8d074010c915f6fd61c8f7e43277983 Mon Sep 17 00:00:00 2001 From: Zach Stein Date: Mon, 9 May 2022 10:00:22 -0400 Subject: [PATCH 1/2] Add feature to export blocks and pages as PHP arrays --- Console/ExportItem.php | 152 +++++++++++++++++++++++++++++++++++++++++ etc/di.xml | 13 ++++ 2 files changed, 165 insertions(+) create mode 100644 Console/ExportItem.php create mode 100644 etc/di.xml diff --git a/Console/ExportItem.php b/Console/ExportItem.php new file mode 100644 index 0000000..6deb6d2 --- /dev/null +++ b/Console/ExportItem.php @@ -0,0 +1,152 @@ +getBlockByIdentifier = $getBlockByIdentifier; + $this->getPageByIdentifier = $getPageByIdentifier; + parent::__construct($name); + } + + protected function configure() + { + $this->setName(self::COMMAND_NAME); + $this->setDescription('Export CMS object data as a PHP array for use with simple data scripts'); + $this->setDefinition( + [ + new InputArgument( + self::TYPE_ARGUMENT, + InputArgument::REQUIRED + ), + new InputArgument( + self::IDENTIFIER_ARGUMENT, + InputArgument::REQUIRED + ), + new InputArgument( + self::STORE_ARGUMENT, + InputArgument::OPTIONAL, + 'Store Id', + Store::DEFAULT_STORE_ID + ) + ] + ); + + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $entityType = $input->getArgument(self::TYPE_ARGUMENT); + $identifier = $input->getArgument(self::IDENTIFIER_ARGUMENT); + $storeId = $input->getArgument(self::STORE_ARGUMENT); + $errors = $this->validate($entityType); + if ($errors) { + foreach ($errors as $error) { + $output->writeln(''.$error.''); + } + return Cli::RETURN_FAILURE; + } + try { + if ($entityType == self::TYPE_BLOCK) { + $output->writeln($this->generateBlock($identifier, $storeId)); + } + if ($entityType == self::TYPE_PAGE) { + $output->writeln($this->generatePage($identifier, $storeId)); + } + } catch (NoSuchEntityException $nse) { + $output->writeln("No {$entityType} with the identifier {$identifier} was found"); + return Cli::RETURN_FAILURE; + } + return Cli::RETURN_SUCCESS; + } + + private function validate(string $entityType) + { + $errors = []; + if ($entityType != self::TYPE_BLOCK && $entityType != self::TYPE_PAGE) { + $errors[] = "Invalid value for type must be one of: page, block"; + } + return $errors; + } + + /** + * @param string $identifier + * @return string + * @throws NoSuchEntityException + */ + private function generatePage(string $identifier, int $storeId) + { + $page = $this->getPageByIdentifier->execute($identifier, $storeId); + return sprintf( + "[ + 'identifier' => '%s', + 'title' => '%s', + 'content' => <<getTitle(), + $page->getContent() + ); + } + + /** + * @param string $identifier + * @return string + * @throws NoSuchEntityException + */ + private function generateBlock(string $identifier, int $storeId) + { + $block = $this->getBlockByIdentifier->execute($identifier, $storeId); + return sprintf( + "[ + 'identifier' => '%s', + 'title' => '%s', + 'content' => <<getTitle(), + $block->getContent() + ); + } + } diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..189b365 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,13 @@ + + + + + + + MarkShust\SimpleData\Console\ExportItem + + + + + From e29a301bdfc3e7bbdf97ed0198790b0d697d41a6 Mon Sep 17 00:00:00 2001 From: Zach Stein Date: Mon, 9 May 2022 10:03:41 -0400 Subject: [PATCH 2/2] Remove unused imports --- Console/ExportItem.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Console/ExportItem.php b/Console/ExportItem.php index 6deb6d2..51f4432 100644 --- a/Console/ExportItem.php +++ b/Console/ExportItem.php @@ -2,8 +2,6 @@ namespace MarkShust\SimpleData\Console; - use Amasty\Shopby\Api\CmsPageRepositoryInterface; - use Magento\Cms\Api\BlockRepositoryInterface; use Magento\Cms\Api\GetBlockByIdentifierInterface; use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Framework\Console\Cli;