/
Registration.php
208 lines (189 loc) · 6.04 KB
/
Registration.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
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Model\Theme;
use Magento\Framework\Filesystem;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Design\ThemeInterface;
/**
* Theme registration model class
*/
class Registration
{
/**
* @var \Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory
*/
protected $_collectionFactory;
/**
* Collection of themes in file-system
*
* @var Collection
*/
protected $_themeCollection;
/**
* Allowed sequence relation by type, array(parent theme, child theme)
*
* @var array
*/
protected $_allowedRelations = [
[ThemeInterface::TYPE_PHYSICAL, ThemeInterface::TYPE_VIRTUAL],
[ThemeInterface::TYPE_VIRTUAL, ThemeInterface::TYPE_STAGING],
];
/**
* Forbidden sequence relation by type
*
* @var array
*/
protected $_forbiddenRelations = [
[ThemeInterface::TYPE_VIRTUAL, ThemeInterface::TYPE_VIRTUAL],
[ThemeInterface::TYPE_PHYSICAL, ThemeInterface::TYPE_STAGING],
];
/**
* Initialize dependencies
*
* @param \Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory $collectionFactory
* @param \Magento\Theme\Model\Theme\Data\Collection $filesystemCollection
*/
public function __construct(
\Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory $collectionFactory,
\Magento\Theme\Model\Theme\Data\Collection $filesystemCollection
) {
$this->_collectionFactory = $collectionFactory;
$this->_themeCollection = $filesystemCollection;
}
/**
* Theme registration
*
* @return $this
*/
public function register()
{
$this->_themeCollection->clear();
foreach ($this->_themeCollection as $theme) {
$this->_registerThemeRecursively($theme);
}
$this->checkPhysicalThemes()->checkAllowedThemeRelations();
return $this;
}
/**
* Register theme and recursively all its ascendants
* Second param is optional and is used to prevent circular references in inheritance chain
*
* @param ThemeInterface &$theme
* @param array $inheritanceChain
* @return $this
* @throws LocalizedException
*/
protected function _registerThemeRecursively(&$theme, $inheritanceChain = [])
{
if ($theme->getId()) {
return $this;
}
$themeModel = $this->getThemeFromDb($theme->getFullPath());
if ($themeModel->getId()) {
$theme = $themeModel;
return $this;
}
$tempId = $theme->getFullPath();
if (in_array($tempId, $inheritanceChain)) {
throw new LocalizedException(__('Circular-reference in theme inheritance detected for "%1"', $tempId));
}
$inheritanceChain[] = $tempId;
$parentTheme = $theme->getParentTheme();
if ($parentTheme) {
$this->_registerThemeRecursively($parentTheme, $inheritanceChain);
$theme->setParentId($parentTheme->getId());
}
$this->_savePreviewImage($theme);
$theme->setType(ThemeInterface::TYPE_PHYSICAL);
$theme->save();
return $this;
}
/**
* Save preview image for theme
*
* @param ThemeInterface $theme
* @return $this
*/
protected function _savePreviewImage(ThemeInterface $theme)
{
$themeDirectory = $theme->getCustomization()->getThemeFilesPath();
if (!$theme->getPreviewImage() || !$themeDirectory) {
return $this;
}
$imagePath = $themeDirectory . '/' . $theme->getPreviewImage();
if (0 === strpos($imagePath, $themeDirectory)) {
$theme->getThemeImage()->createPreviewImage($imagePath);
}
return $this;
}
/**
* Get theme from DB by full path
*
* @param string $fullPath
* @return ThemeInterface
*/
public function getThemeFromDb($fullPath)
{
return $this->_collectionFactory->create()->getThemeByFullPath($fullPath);
}
/**
* Checks all physical themes that they were not deleted
*
* @return $this
*/
public function checkPhysicalThemes()
{
$themes = $this->_collectionFactory->create()->addTypeFilter(ThemeInterface::TYPE_PHYSICAL);
/** @var $theme ThemeInterface */
foreach ($themes as $theme) {
if (!$this->_themeCollection->hasTheme($theme)) {
$theme->setType(ThemeInterface::TYPE_VIRTUAL)->save();
}
}
return $this;
}
/**
* Check whether all themes have correct parent theme by type
*
* @return $this
*/
public function checkAllowedThemeRelations()
{
foreach ($this->_forbiddenRelations as $typesSequence) {
list($parentType, $childType) = $typesSequence;
$collection = $this->_collectionFactory->create();
$collection->addTypeRelationFilter($parentType, $childType);
/** @var $theme ThemeInterface */
foreach ($collection as $theme) {
$parentId = $this->_getResetParentId($theme);
if ($theme->getParentId() != $parentId) {
$theme->setParentId($parentId)->save();
}
}
}
return $this;
}
/**
* Reset parent themes by type
*
* @param ThemeInterface $theme
* @return int|null
*/
protected function _getResetParentId(ThemeInterface $theme)
{
$parentTheme = $theme->getParentTheme();
while ($parentTheme) {
foreach ($this->_allowedRelations as $typesSequence) {
list($parentType, $childType) = $typesSequence;
if ($theme->getType() == $childType && $parentTheme->getType() == $parentType) {
return $parentTheme->getId();
}
}
$parentTheme = $parentTheme->getParentTheme();
}
return null;
}
}