|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * RocketWeb |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 8 | + * that is bundled with this package in the file LICENSE.txt. |
| 9 | + * It is also available through the world-wide-web at this URL: |
| 10 | + * http://opensource.org/licenses/osl-3.0.php |
| 11 | + * |
| 12 | + * @category RocketWeb |
| 13 | + * @copyright Copyright (c) 2020 RocketWeb (http://rocketweb.com) |
| 14 | + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
| 15 | + * @author Rocket Web Inc. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace RocketWeb\CmsImportExport\Model\Service; |
| 19 | + |
| 20 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 21 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 22 | + |
| 23 | +class DumpCmsDataService |
| 24 | +{ |
| 25 | + private \Magento\Cms\Api\PageRepositoryInterface $pageRepository; |
| 26 | + private \Magento\Cms\Api\BlockRepositoryInterface $blockRepository; |
| 27 | + private \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder; |
| 28 | + private \Magento\Framework\Filesystem\DirectoryList $directoryList; |
| 29 | + private \Magento\Framework\Filesystem $filesystem; |
| 30 | + private \Magento\Framework\Serialize\SerializerInterface $serializer; |
| 31 | + private \Magento\Catalog\Model\CategoryList $categoryList; |
| 32 | + private array $blockIdentifiers = []; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + \Magento\Cms\Api\PageRepositoryInterface $pageRepository, |
| 36 | + \Magento\Cms\Api\BlockRepositoryInterface $blockRepository, |
| 37 | + \Magento\Catalog\Model\CategoryList $categoryList, |
| 38 | + \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder, |
| 39 | + \Magento\Framework\Filesystem\DirectoryList $directoryList, |
| 40 | + \Magento\Framework\Filesystem $filesystem, |
| 41 | + \Magento\Framework\Serialize\SerializerInterface $serializer |
| 42 | + ) { |
| 43 | + $this->pageRepository = $pageRepository; |
| 44 | + $this->blockRepository = $blockRepository; |
| 45 | + $this->criteriaBuilder = $criteriaBuilder; |
| 46 | + $this->directoryList = $directoryList; |
| 47 | + $this->filesystem = $filesystem; |
| 48 | + $this->serializer = $serializer; |
| 49 | + $this->categoryList = $categoryList; |
| 50 | + } |
| 51 | + |
| 52 | + public function execute(array $types, ?array $identifiers) |
| 53 | + { |
| 54 | + $varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 55 | + $varPath = $this->directoryList->getPath(DirectoryList::VAR_DIR); |
| 56 | + $workingDirPath = $varPath . '/sync_cms_data'; |
| 57 | + if ($varDirectory->isExist($workingDirPath)) { |
| 58 | + $varDirectory->delete($workingDirPath); |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($types as $type) { |
| 62 | + if ($type == 'block') { |
| 63 | + $this->dumpBlocks($workingDirPath . '/cms/blocks/', $varDirectory, $identifiers); |
| 64 | + } else if ($type == 'page') { |
| 65 | + $this->dumpPages($workingDirPath . '/cms/pages/', $varDirectory, $identifiers); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private function write(WriteInterface $writeDirectory, string $filePath, string $content): void |
| 71 | + { |
| 72 | + $stream = $writeDirectory->openFile($filePath, 'w+'); |
| 73 | + $stream->lock(); |
| 74 | + $stream->write($content); |
| 75 | + $stream->unlock(); |
| 76 | + $stream->close(); |
| 77 | + } |
| 78 | + |
| 79 | + private function dumpPages(string $path, WriteInterface $varDirectory, ?array $identifiers): void |
| 80 | + { |
| 81 | + $searchCriteria = $this->criteriaBuilder; |
| 82 | + if ($identifiers) { |
| 83 | + $searchCriteria->addFilter('identifier', $identifiers, 'in'); |
| 84 | + } |
| 85 | + |
| 86 | + $pagesList = $this->pageRepository->getList($searchCriteria->create()); |
| 87 | + $pages = $pagesList->getItems(); |
| 88 | + |
| 89 | + foreach ($pages as $page) { |
| 90 | + $identifier = str_replace('/', '|', trim($page->getIdentifier())); |
| 91 | + if (strpos($identifier, '.html') !== false) { |
| 92 | + $identifier = str_replace('.html', '_html', $identifier); |
| 93 | + } |
| 94 | + $htmlPath = $path . $identifier . '.html'; |
| 95 | + $this->write($varDirectory, $htmlPath, $page->getContent()); |
| 96 | + $jsonPath = $path . $identifier . '.json'; |
| 97 | + $jsonContent = [ |
| 98 | + 'title' => $page->getTitle(), |
| 99 | + 'is_active' => $page->isActive(), |
| 100 | + 'page_layout' => $page->getPageLayout(), |
| 101 | + 'identifier' => $page->getIdentifier(), |
| 102 | + 'content_heading' => $page->getContentHeading(), |
| 103 | + |
| 104 | + ]; |
| 105 | + $this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private function dumpBlocks(string $path, WriteInterface $varDirectory, ?array $identifiers): void |
| 110 | + { |
| 111 | + $searchCriteria = $this->criteriaBuilder; |
| 112 | + if ($identifiers) { |
| 113 | + $searchCriteria->addFilter('identifier', $identifiers, 'in'); |
| 114 | + } |
| 115 | + |
| 116 | + $blocksList = $this->blockRepository->getList($searchCriteria->create()); |
| 117 | + $blocks = $blocksList->getItems(); |
| 118 | + |
| 119 | + foreach ($blocks as $block) { |
| 120 | + if (strpos($block->getIdentifier(), 'series_build_cms_') !== false |
| 121 | + || strpos($block->getIdentifier(), '-block-') !== false |
| 122 | + ) { |
| 123 | + // Skipping all generated CMS blocks from old system |
| 124 | + continue; |
| 125 | + } |
| 126 | + $this->blockIdentifiers[$block->getId()] = $block->getIdentifier(); |
| 127 | + $htmlPath = $path . trim($block->getIdentifier()) . '.html'; |
| 128 | + $this->write($varDirectory, $htmlPath, $block->getContent()); |
| 129 | + $jsonPath = $path . trim($block->getIdentifier()) . '.json'; |
| 130 | + $jsonContent = [ |
| 131 | + 'title' => $block->getTitle(), |
| 132 | + 'identifier' => $block->getIdentifier(), |
| 133 | + 'stores' => [1], |
| 134 | + 'is_active' => $block->isActive() |
| 135 | + ]; |
| 136 | + $this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent)); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments