-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtility.php
336 lines (300 loc) · 10.6 KB
/
Utility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
namespace Zero1\ReleaseLibrary;
use Exception;
use Magento\Variable\Model\VariableFactory;
use Magento\Catalog\Api\CategoryManagementInterface;
use Magento\Framework\Module\Dir;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockInterfaceFactory;
use Magento\Cms\Model\PageFactory;
use Magento\Cms\Model\PageRepository;
use Magento\Framework\Exception\NoSuchEntityException;
use Zero1\ReleaseLibrary\EntityAlreadyExistsException;
use \Psr\Log\LoggerInterface;
use \Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriterInterface;
use Magento\Email\Model\ResourceModel\Template as EmailTemplateResource;
use Magento\Email\Model\BackendTemplateFactory as EmailTemplateFactory;
class Utility
{
/** @var \Magento\Variable\Model\VariableFactory **/
protected $customVariableFactory;
/** @var CategoryManagementInterface */
protected $categoryManagement;
/** @var Dir\Reader */
private $reader;
/** @var BlockRepositoryInterface */
private $blockRepository;
/** @var BlockInterfaceFactory */
private $blockInterfaceFactory;
/** @var \Magento\Cms\Model\PageRepository */
protected $pageRepository;
/** @var \Magento\Cms\Model\PageFactory */
protected $pageFactory;
/** @var LoggerInterface */
private $logger;
/** @var ConfigWriterInterface */
protected $configWriter;
/** @var string */
protected $sourceModule = 'Zero1_ClientSetup';
/** @var string */
protected $sourceDirectory;
/** @var EmailTemplateResource */
protected $emailTemplateResource;
/** @var EmailTemplateFactory */
protected $emailTemplateFactory;
public function __construct(
VariableFactory $customVariableFactory,
CategoryManagementInterface $categoryManagement,
Dir\Reader $reader,
BlockRepositoryInterface $blockRepository,
BlockInterfaceFactory $blockInterfaceFactory,
PageRepository $pageRepository,
PageFactory $pageFactory,
ConfigWriterInterface $configWriter,
EmailTemplateResource $emailTemplateResource,
EmailTemplateFactory $emailTemplateFactory,
\Psr\Log\LoggerInterface $logger
){
$this->customVariableFactory = $customVariableFactory;
$this->categoryManagement = $categoryManagement;
$this->reader = $reader;
$this->blockRepository = $blockRepository;
$this->blockInterfaceFactory = $blockInterfaceFactory;
$this->pageRepository = $pageRepository;
$this->pageFactory = $pageFactory;
$this->configWriter = $configWriter;
$this->emailTemplateResource = $emailTemplateResource;
$this->emailTemplateFactory = $emailTemplateFactory;
$this->logger = $logger;
}
/**
* Create a custom variable
* @param $code string
* @param $name string
* @param $htmlValue string
* @param $plainValue string
* @param bool $updateIfExists
* @throws \Zero1\ReleaseLibrary\EntityAlreadyExistsException
*/
public function createCustomVariable(
$code,
$name,
$htmlValue,
$plainValue,
$updateIfExists = true
){
$customVariable = $this->customVariableFactory->create();
$customVariable->loadByCode($code);
if($customVariable->getId() && !$updateIfExists){
throw new EntityAlreadyExistsException($customVariable, $code, 'code');
}
$customVariable->setCode($code)
->setName($name)
->setHtmlValue($htmlValue)
->setPlainValue($plainValue)
-> save();
}
/**
* Move a category
* @param $categoryId int
* @param $parentId int
* @param null|int $afterId
* @return bool
* @throws Exception
*/
public function moveCategory($categoryId,$parentId,$afterId = null)
{
$this->logger->alert('moveCategory', array($categoryId,$parentId)) ;
$isCategoryMoveSuccess = false;
try {
$isCategoryMoveSuccess = $this->categoryManagement->move($categoryId, $parentId, $afterId);
} catch (Exception $exception) {
$this->logger->alert($exception->getMessage().' '.$categoryId);
}
return $isCategoryMoveSuccess;
}
/**
* Set the "source module"
* This is the module that contains files to be imported
* e.g cms block html in a file instead of in upgrade code
* @param string $sourceModule
* @return $this
*/
public function setSourceModule($sourceModule)
{
$this->sourceModule = $sourceModule;
$this->sourceDirectory = null;
return $this;
}
/**
* Return the source directory for the configured source module
* @return string
*/
public function getSourceDirectory()
{
if(!$this->sourceDirectory){
$this->sourceDirectory = preg_replace('/\/view$/', '', $this->reader->getModuleDir(Dir::MODULE_VIEW_DIR, $this->sourceModule));
}
return $this->sourceDirectory;
}
/**
* Get the cms block source directory
* @return string
*/
public function getBlockSourceDirectory()
{
$this->logger->alert('getBlockSourceDirectory', []);
return sprintf(
'%s/%s',
$this->getSourceDirectory(),
'block_source'
);
}
/**
* Get the cms page source directory
* @return string
*/
public function getPageSourceDirectory()
{
$this->logger->alert('getPageSourceDirectory', []);
return sprintf(
'%s/%s',
$this->getSourceDirectory(),
'page_source'
);
}
/**
* Create all blocks from specified directory
* @param string $source
*/
public function createBlocksFromDir($source)
{
$blocks = array_diff(scandir($source), ['..', '.']);
foreach ($blocks as $block) {
$path = $source . '/' . $block;
if (is_dir($path)) {
$this->createBlocksFromDir($path);
continue;
}
$id = pathinfo($block, PATHINFO_FILENAME);
$title = str_replace('-', ' ', $id);
$contents = file_get_contents($path);
$this->makeBlock($id, $title, $contents);
}
}
/**
* Create all pages from specified directory
* @param string $source
*/
public function createPagesFromDir($source)
{
$pages = array_diff(scandir($source), ['..', '.']);
foreach ($pages as $page) {
$path = $source . '/' . $page;
if (is_dir($path)) {
$this->createPagesFromDir($path);
continue;
}
$id = pathinfo($page, PATHINFO_FILENAME);
$title = str_replace('-', ' ', $id);
$contents = file_get_contents($path);
$this->makePage($id, $title, $contents);
}
}
private function makeBlock(
string $identifier,
string $title,
string $content,
array $stores = [0]
) {
try {
$block = $this->blockRepository->getById($identifier);
} catch (NoSuchEntityException $exception) {
/** @var BlockInterface $block */
$block = $this->blockInterfaceFactory->create();
$this->logger->alert('makeBlock '.$title, array()) ;
$block->setTitle($title)
->setIdentifier($identifier)
->setIsActive(true)
->setData('stores', $stores);
}
$block->setContent($content);
try {
$this->logger->alert('saveBlock '.$title, array()) ;
$this->blockRepository->save($block);
} catch (NoSuchEntityException $exception) {
$this->logger->alert('saveBlock ERROR '.$exception, array()) ;
}
}
private function makePage(
string $identifier,
string $title,
string $content,
array $stores = [0]
) {
try {
$page = $this->pageRepository->getById($identifier);
} catch (NoSuchEntityException $exception) {
/** @var \Magento\Cms\Model\Page $page */
$this->logger->alert('makePage '.$title, array()) ;
$page = $this->pageFactory->create();
$page->setTitle($title)
->setIdentifier($identifier)
->setPageLayout('1column')
->setIsActive(true)
->setContent($content)
->setData('stores', $stores);
}
$page->setContent($content);
$this->pageRepository->save($page);
}
/**
* Set config
* @param array $pathAndValue
* must be an array of arrays, each sub array must contain between 2 - 4 elements
* index 0: config path (mandatory)
* index 1: value (mandatory)
* index 2: scope (optional - if not set will fall back to $defaultScope)
* index 3: scope id (optional - if not set will fall back to $defaultScopeId)
* @param string $defaultScope
* @param int $defaultScopeId
* @return Utility
* @throws \InvalidArgumentException
*/
public function setConfig($pathAndValue, $defaultScope = 'default', $defaultScopeId = 0)
{
foreach($pathAndValue as $configRow){
if(!array_key_exists(0, $configRow) || !array_key_exists(1, $configRow)){
throw new \InvalidArgumentException('You must provide at least two elements, you provided: '.json_encode($configRow));
}
$path = $configRow[0];
$value = $configRow[1];
$scope = isset($configRow[2])? $configRow[2] : $defaultScope;
$scopeId = isset($configRow[3])? $configRow[3] : $defaultScopeId;
$this->logger->debug('set config', [
'path' => $path,
'value' => $value,
'scope' => $scope,
'scope_id' => $scopeId
]);
$this->configWriter->save($path, $value, $scope, $scopeId);
}
return $this;
}
/**
* Update email template by id.
* The callback will receive one argument $template (\Magento\Email\Model\BackendTemplate)
* you can update any part of the template, then return the object.
* @param int|string $templateId
* @param callable $callback
* @return void
*/
public function updateEmailTemplate($templateId, callable $callback)
{
/** @var \Magento\Email\Model\BackendTemplate $template */
$template = $this->emailTemplateFactory->create();
$template->load($templateId);
$template = $callback($template);
$this->emailTemplateResource->save($template);
}
}