-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathTheme.php
438 lines (397 loc) · 11.8 KB
/
Theme.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Model;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Design\ThemeInterface;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
/**
* Theme model class
*
* @method string getPackageCode()
* @method string getParentThemePath()
* @method string getParentId()
* @method string getThemeTitle()
* @method string getPreviewImage()
* @method bool getIsFeatured()
* @method int getThemeId()
* @method int getType()
* @method array getAssignedStores()
* @method ThemeInterface setAssignedStores(array $stores)
* @method ThemeInterface setParentId(int $id)
* @method ThemeInterface setParentTheme($parentTheme)
* @method ThemeInterface setPackageCode(string $packageCode)
* @method ThemeInterface setThemeCode(string $themeCode)
* @method ThemeInterface setThemePath(string $themePath)
* @method ThemeInterface setThemeTitle(string $themeTitle)
* @method ThemeInterface setType(int $type)
* @method ThemeInterface setCode(string $code)
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Theme extends \Magento\Framework\Model\AbstractModel implements ThemeInterface
{
/**
* {@inheritdoc}
*
* @var string
*/
protected $_eventPrefix = 'theme';
/**
* {@inheritdoc}
*
* @var string
*/
protected $_eventObject = 'theme';
/**
* @var \Magento\Framework\View\Design\Theme\FlyweightFactory
*/
protected $_themeFactory;
/**
* @var \Magento\Framework\View\Design\Theme\Domain\Factory
*/
protected $_domainFactory;
/**
* @var \Magento\Framework\View\Design\Theme\ImageFactory
*/
protected $_imageFactory;
/**
* @var \Magento\Framework\View\Design\Theme\Validator
*/
protected $_validator;
/**
* @var \Magento\Framework\View\Design\Theme\Customization
*/
protected $_customization;
/**
* @var \Magento\Framework\View\Design\Theme\CustomizationFactory
*/
protected $_customFactory;
/**
* @var ThemeFactory
*/
private $themeModelFactory;
/**
* @var ThemeInterface[]
*/
protected $inheritanceSequence;
/**
* Initialize dependencies
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
* @param \Magento\Framework\View\Design\Theme\Domain\Factory $domainFactory
* @param \Magento\Framework\View\Design\Theme\ImageFactory $imageFactory
* @param \Magento\Framework\View\Design\Theme\Validator $validator
* @param \Magento\Framework\View\Design\Theme\CustomizationFactory $customizationFactory
* @param \Magento\Theme\Model\ResourceModel\Theme $resource
* @param \Magento\Theme\Model\ResourceModel\Theme\Collection $resourceCollection
* @param array $data
* @param ThemeFactory $themeModelFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory,
\Magento\Framework\View\Design\Theme\Domain\Factory $domainFactory,
\Magento\Framework\View\Design\Theme\ImageFactory $imageFactory,
\Magento\Framework\View\Design\Theme\Validator $validator,
\Magento\Framework\View\Design\Theme\CustomizationFactory $customizationFactory,
?\Magento\Theme\Model\ResourceModel\Theme $resource = null,
?ThemeCollection $resourceCollection = null,
array $data = [],
?ThemeFactory $themeModelFactory = null
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->_themeFactory = $themeFactory;
$this->_domainFactory = $domainFactory;
$this->_imageFactory = $imageFactory;
$this->_validator = $validator;
$this->_customFactory = $customizationFactory;
$this->themeModelFactory = $themeModelFactory ?: ObjectManager::getInstance()->get(ThemeFactory::class);
$this->addData(['type' => self::TYPE_VIRTUAL]);
}
/**
* Init resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(\Magento\Theme\Model\ResourceModel\Theme::class);
}
/**
* Get theme image model
*
* @return \Magento\Framework\View\Design\Theme\Image
*/
public function getThemeImage()
{
return $this->_imageFactory->create(['theme' => $this]);
}
/**
* @return \Magento\Framework\View\Design\Theme\Customization
*/
public function getCustomization()
{
if ($this->_customization === null) {
$this->_customization = $this->_customFactory->create(['theme' => $this]);
}
return $this->_customization;
}
/**
* Check if theme is deletable
*
* @return bool
*/
public function isDeletable()
{
return $this->isEditable();
}
/**
* Check if theme is editable
*
* @return bool
*/
public function isEditable()
{
return self::TYPE_PHYSICAL != $this->getType();
}
/**
* Check if theme is virtual
*
* @return bool
*/
public function isVirtual()
{
return $this->getType() == self::TYPE_VIRTUAL;
}
/**
* Check if theme is physical
*
* @return bool
*/
public function isPhysical()
{
return $this->getType() == self::TYPE_PHYSICAL;
}
/**
* Check theme is visible in backend
*
* @return bool
*/
public function isVisible()
{
return in_array($this->getType(), [self::TYPE_PHYSICAL, self::TYPE_VIRTUAL]);
}
/**
* Check is theme has child virtual themes
*
* @return bool
*/
public function hasChildThemes()
{
return (bool)$this->getCollection()->addTypeFilter(
self::TYPE_VIRTUAL
)->addFieldToFilter(
'parent_id',
['eq' => $this->getId()]
)->getSize();
}
/**
* Retrieve theme instance representing the latest changes to a theme
*
* @return Theme|null
*/
public function getStagingVersion()
{
if ($this->getId()) {
$collection = $this->getCollection();
$collection->addFieldToFilter('parent_id', $this->getId());
$collection->addFieldToFilter('type', self::TYPE_STAGING);
$stagingTheme = $collection->getFirstItem();
if ($stagingTheme->getId()) {
return $stagingTheme;
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function getParentTheme()
{
if ($this->hasData('parent_theme')) {
return $this->getData('parent_theme');
}
$theme = null;
if ($this->getParentId()) {
$theme = $this->_themeFactory->create($this->getParentId());
}
$this->setParentTheme($theme);
return $theme;
}
/**
* {@inheritdoc}
*/
public function getArea()
{
// In order to support environment emulation of area, if area is set, return it
if ($this->getData('area') && !$this->_appState->isAreaCodeEmulated()) {
return $this->getData('area');
}
return $this->_appState->getAreaCode();
}
/**
* {@inheritdoc}
*/
public function getThemePath()
{
return $this->getData('theme_path');
}
/**
* Retrieve theme full path which is used to distinguish themes if they are not in DB yet
*
* Alternative id looks like "<area>/<theme_path>".
* Used as id in file-system theme collection
*
* @return string|null
*/
public function getFullPath()
{
return $this->getThemePath() ? $this->getArea() . self::PATH_SEPARATOR . $this->getThemePath() : null;
}
/**
* {@inheritdoc}
*/
public function getCode()
{
return (string)$this->getData('code');
}
/**
* Get one of theme domain models
*
* @param int|null $type
* @return \Magento\Theme\Model\Theme\Domain\Virtual|\Magento\Theme\Model\Theme\Domain\Staging
* @throws \InvalidArgumentException
*/
public function getDomainModel($type = null)
{
if ($type !== null && $type != $this->getType()) {
throw new \InvalidArgumentException(
sprintf(
'Invalid domain model "%s" requested for theme "%s" of type "%s"',
$type,
$this->getId(),
$this->getType()
)
);
}
return $this->_domainFactory->create($this);
}
/**
* Validate theme data
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _validate()
{
if (!$this->_validator->validate($this)) {
$messages = $this->_validator->getErrorMessages();
throw new \Magento\Framework\Exception\LocalizedException(__(implode(PHP_EOL, reset($messages))));
}
return $this;
}
/**
* Before theme save
*
* @return $this
*/
public function beforeSave()
{
$this->_validate();
return parent::beforeSave();
}
/**
* Update all relations after deleting theme
*
* @return $this
*/
public function afterDelete()
{
$stagingVersion = $this->getStagingVersion();
if ($stagingVersion) {
$stagingVersion->delete();
}
$this->getCollection()->updateChildRelations($this);
return parent::afterDelete();
}
/**
* Return the full theme inheritance sequence, from the root theme till a specified one
*
* @return ThemeInterface[]
*/
public function getInheritedThemes()
{
if (null === $this->inheritanceSequence) {
$theme = $this;
$result = [];
while ($theme) {
$result[] = $theme;
$theme = $theme->getParentTheme();
}
$this->inheritanceSequence = array_reverse($result);
}
return $this->inheritanceSequence;
}
/**
* @inheritdoc
*/
public function toArray(array $keys = [])
{
$data = parent::toArray($keys);
if (isset($data['parent_theme'])) {
$data['parent_theme'] = $this->getParentTheme()->toArray();
}
if (isset($data['inherited_themes'])) {
foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
$data['inherited_themes'][$key] = $inheritedTheme->toArray();
}
}
return $data;
}
/**
* Populate Theme object from an array
*
* @param array $data
* @return Theme
*/
public function populateFromArray(array $data)
{
$this->_data = $data;
if (isset($data['parent_theme'])) {
$this->_data['parent_theme'] = $this->createThemeInstance()->populateFromArray($data['parent_theme']);
}
if (isset($data['inherited_themes'])) {
foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
$themeInstance = $this->createThemeInstance()->populateFromArray($inheritedTheme);
$this->_data['inherited_themes'][$key] = $themeInstance;
}
}
return $this;
}
/**
* Create Theme instance
*
* @return \Magento\Theme\Model\Theme
*/
private function createThemeInstance()
{
return $this->themeModelFactory->create();
}
}