Skip to content

Commit f62685f

Browse files
committed
adding stores support [#3]
1 parent d0bc272 commit f62685f

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

Model/Service/DumpCmsDataService.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
class DumpCmsDataService
2424
{
25+
public const STORE_SCOPE_ALL = '_all_';
2526
private \Magento\Cms\Api\PageRepositoryInterface $pageRepository;
2627
private \Magento\Cms\Api\BlockRepositoryInterface $blockRepository;
2728
private \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder;
2829
private \Magento\Framework\Filesystem\DirectoryList $directoryList;
2930
private \Magento\Framework\Filesystem $filesystem;
3031
private \Magento\Framework\Serialize\SerializerInterface $serializer;
3132
private \Magento\Catalog\Model\CategoryList $categoryList;
33+
private \Magento\Store\Model\StoreManagerInterface $storeManager;
3234
private array $blockIdentifiers = [];
3335

3436
public function __construct(
@@ -38,7 +40,8 @@ public function __construct(
3840
\Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
3941
\Magento\Framework\Filesystem\DirectoryList $directoryList,
4042
\Magento\Framework\Filesystem $filesystem,
41-
\Magento\Framework\Serialize\SerializerInterface $serializer
43+
\Magento\Framework\Serialize\SerializerInterface $serializer,
44+
\Magento\Store\Model\StoreManagerInterface $storeManager
4245
) {
4346
$this->pageRepository = $pageRepository;
4447
$this->blockRepository = $blockRepository;
@@ -47,6 +50,7 @@ public function __construct(
4750
$this->filesystem = $filesystem;
4851
$this->serializer = $serializer;
4952
$this->categoryList = $categoryList;
53+
$this->storeManager = $storeManager;
5054
}
5155

5256
public function execute(array $types, ?array $identifiers)
@@ -76,6 +80,24 @@ private function write(WriteInterface $writeDirectory, string $filePath, string
7680
$stream->close();
7781
}
7882

83+
private function getStoreCodes($stores): array
84+
{
85+
$storeCodes = [];
86+
if ($stores) {
87+
foreach ($stores as $storeId) {
88+
try {
89+
$store = $this->storeManager->getStore($storeId);
90+
$storeCode = $storeId ? $store->getCode() : self::STORE_SCOPE_ALL;
91+
$storeCodes[] = $storeCode;
92+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
93+
echo $exception->getMessage() . "\n";
94+
}
95+
}
96+
}
97+
98+
return $storeCodes;
99+
}
100+
79101
private function dumpPages(string $path, WriteInterface $varDirectory, ?array $identifiers): void
80102
{
81103
$searchCriteria = $this->criteriaBuilder;
@@ -91,14 +113,16 @@ private function dumpPages(string $path, WriteInterface $varDirectory, ?array $i
91113
if (strpos($identifier, '.html') !== false) {
92114
$identifier = str_replace('.html', '_html', $identifier);
93115
}
94-
$htmlPath = $path . $identifier . '.html';
116+
$storeCodes = $this->getStoreCodes($page->getStores());
117+
$htmlPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.html';
95118
$this->write($varDirectory, $htmlPath, $page->getContent());
96-
$jsonPath = $path . $identifier . '.json';
119+
$jsonPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.json';
97120
$jsonContent = [
98121
'title' => $page->getTitle(),
99122
'is_active' => $page->isActive(),
100123
'page_layout' => $page->getPageLayout(),
101124
'identifier' => $page->getIdentifier(),
125+
'stores' => $storeCodes,
102126
'content_heading' => $page->getContentHeading(),
103127

104128
];
@@ -124,13 +148,14 @@ private function dumpBlocks(string $path, WriteInterface $varDirectory, ?array $
124148
continue;
125149
}
126150
$this->blockIdentifiers[$block->getId()] = $block->getIdentifier();
127-
$htmlPath = $path . trim($block->getIdentifier()) . '.html';
151+
$storeCodes = $this->getStoreCodes($block->getStores());
152+
$htmlPath = $path . trim($block->getIdentifier()) . '|' . implode('|', $storeCodes) . '.html';
128153
$this->write($varDirectory, $htmlPath, $block->getContent());
129-
$jsonPath = $path . trim($block->getIdentifier()) . '.json';
154+
$jsonPath = $path . trim($block->getIdentifier()) . '|' . implode('|', $storeCodes) . '.json';
130155
$jsonContent = [
131156
'title' => $block->getTitle(),
132157
'identifier' => $block->getIdentifier(),
133-
'stores' => [1],
158+
'stores' => $storeCodes,
134159
'is_active' => $block->isActive()
135160
];
136161
$this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent));

Model/Service/ImportCmsDataService.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
class ImportCmsDataService
2323
{
24+
private const STORE_SCOPE_ADMIN = 'admin';
2425
private \Magento\Cms\Api\PageRepositoryInterface $pageRepository;
2526
private \Magento\Cms\Api\BlockRepositoryInterface $blockRepository;
2627
private \Magento\Framework\Serialize\SerializerInterface $serializer;
2728
private \Magento\Framework\Filesystem\Directory\ReadInterface $directoryRead;
2829
private \Magento\Cms\Api\Data\BlockInterfaceFactory $blockFactory;
2930
private \Magento\Cms\Api\Data\PageInterfaceFactory $pageFactory;
31+
private \Magento\Store\Api\StoreRepositoryInterface $storeRepository;
3032
private string $varPath;
3133

3234
public function __construct(
@@ -36,7 +38,8 @@ public function __construct(
3638
\Magento\Cms\Api\Data\BlockInterfaceFactory $blockFactory,
3739
\Magento\Framework\Filesystem\DirectoryList $directoryList,
3840
\Magento\Framework\Filesystem $filesystem,
39-
\Magento\Framework\Serialize\SerializerInterface $serializer
41+
\Magento\Framework\Serialize\SerializerInterface $serializer,
42+
\Magento\Store\Api\StoreRepositoryInterface $storeRepository
4043
) {
4144
$this->pageRepository = $pageRepository;
4245
$this->blockRepository = $blockRepository;
@@ -45,6 +48,7 @@ public function __construct(
4548
$this->varPath = $directoryList->getPath(DirectoryList::VAR_DIR) . '/';
4649
$this->blockFactory = $blockFactory;
4750
$this->pageFactory = $pageFactory;
51+
$this->storeRepository = $storeRepository;
4852
}
4953

5054
public function execute(array $types, ?array $identifiers)
@@ -69,6 +73,26 @@ public function execute(array $types, ?array $identifiers)
6973
}
7074
}
7175

76+
private function getStoreIds($storeCodes): array
77+
{
78+
$storeIds = [];
79+
if (is_array($storeCodes) && count($storeCodes)) {
80+
foreach ($storeCodes as $storeCode) {
81+
try {
82+
if ($storeCode === DumpCmsDataService::STORE_SCOPE_ALL) {
83+
$storeCode = self::STORE_SCOPE_ADMIN;
84+
}
85+
$store = $this->storeRepository->get($storeCode);
86+
$storeIds[] = $store->getId();
87+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
88+
echo $exception->getMessage() . "\n";
89+
}
90+
}
91+
}
92+
93+
return $storeIds;
94+
}
95+
7296
private function importBlocks(string $dirPath, ?array $identifiers): void
7397
{
7498
$filePaths = $this->directoryRead->read($this->varPath . $dirPath);
@@ -79,6 +103,7 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
79103
}
80104
$identifier = str_replace($dirPath, '', $filePath);
81105
$identifier = str_replace('.html', '', $identifier);
106+
$identifier = substr_replace($identifier, '', strpos($identifier, '|'));
82107
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
83108
// If we have a list of items, we skip if its not in the list
84109
continue;
@@ -98,12 +123,12 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
98123
'stores' => [1],
99124
'is_active' => $block->isActive()
100125
];*/
126+
$storeIds = $this->getStoreIds($jsonData['stores']);
101127
$block->setTitle($jsonData['title']);
102128
$block->setContent($content);
103129
$block->setIdentifier($jsonData['identifier']);
104-
$block->setStoreId($jsonData['stores'][0]);
105130
$block->setIsActive((bool)$jsonData['is_active']);
106-
131+
$block->setStores($storeIds);
107132
try {
108133
$this->blockRepository->save($block);
109134
} catch (\Exception $exception) {
@@ -121,8 +146,9 @@ private function importPages(string $dirPath, ?array $identifiers): void
121146
continue;
122147
}
123148
$identifier = str_replace($dirPath, '', $filePath);
124-
$identifier = str_replace('|', '/', $identifier);
125149
$identifier = str_replace('.html', '', $identifier);
150+
$identifier = substr_replace($identifier, '', strpos($identifier, '|'));
151+
$identifier = str_replace('|', '/', $identifier);
126152
$identifier = str_replace('_html', '.html', $identifier);
127153
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
128154
// If we have a list of items, we skip if its not in the list
@@ -137,6 +163,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
137163
$content = $this->directoryRead->readFile($filePath);
138164
$jsonData = $this->directoryRead->readFile(str_replace('.html', '.json', $filePath));
139165
$jsonData = $this->serializer->unserialize($jsonData);
166+
$storeIds = $this->getStoreIds($jsonData['stores']);
140167
/*$jsonContent = [
141168
'title' => $page->getTitle(),
142169
'is_active' => $page->isActive(),
@@ -150,6 +177,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
150177
$page->setPageLayout($jsonData['page_layout']);
151178
$page->setContentHeading($jsonData['content_heading']);
152179
$page->setIsActive((bool)$jsonData['is_active']);
180+
$page->setStores($storeIds);
153181

154182
try {
155183
$this->pageRepository->save($page);

0 commit comments

Comments
 (0)