Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Registration.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
21 
27  protected $_themeCollection;
28 
34  protected $_allowedRelations = [
37  ];
38 
44  protected $_forbiddenRelations = [
47  ];
48 
55  public function __construct(
56  \Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory $collectionFactory,
57  \Magento\Theme\Model\Theme\Data\Collection $filesystemCollection
58  ) {
59  $this->_collectionFactory = $collectionFactory;
60  $this->_themeCollection = $filesystemCollection;
61  }
62 
68  public function register()
69  {
70  $this->_themeCollection->clear();
71 
72  foreach ($this->_themeCollection as $theme) {
73  $this->_registerThemeRecursively($theme);
74  }
75 
76  $this->checkPhysicalThemes()->checkAllowedThemeRelations();
77 
78  return $this;
79  }
80 
90  protected function _registerThemeRecursively(&$theme, $inheritanceChain = [])
91  {
92  if ($theme->getId()) {
93  return $this;
94  }
95  $themeModel = $this->getThemeFromDb($theme->getFullPath());
96  if ($themeModel->getId()) {
97  $theme = $themeModel;
98  return $this;
99  }
100 
101  $tempId = $theme->getFullPath();
102  if (in_array($tempId, $inheritanceChain)) {
103  throw new LocalizedException(__('Circular-reference in theme inheritance detected for "%1"', $tempId));
104  }
105  $inheritanceChain[] = $tempId;
106  $parentTheme = $theme->getParentTheme();
107  if ($parentTheme) {
108  $this->_registerThemeRecursively($parentTheme, $inheritanceChain);
109  $theme->setParentId($parentTheme->getId());
110  }
111 
112  $this->_savePreviewImage($theme);
113  $theme->setType(ThemeInterface::TYPE_PHYSICAL);
114  $theme->save();
115 
116  return $this;
117  }
118 
126  {
127  $themeDirectory = $theme->getCustomization()->getThemeFilesPath();
128  if (!$theme->getPreviewImage() || !$themeDirectory) {
129  return $this;
130  }
131  $imagePath = $themeDirectory . '/' . $theme->getPreviewImage();
132  if (0 === strpos($imagePath, $themeDirectory)) {
133  $theme->getThemeImage()->createPreviewImage($imagePath);
134  }
135  return $this;
136  }
137 
144  public function getThemeFromDb($fullPath)
145  {
146  return $this->_collectionFactory->create()->getThemeByFullPath($fullPath);
147  }
148 
154  public function checkPhysicalThemes()
155  {
156  $themes = $this->_collectionFactory->create()->addTypeFilter(ThemeInterface::TYPE_PHYSICAL);
158  foreach ($themes as $theme) {
159  if (!$this->_themeCollection->hasTheme($theme)) {
160  $theme->setType(ThemeInterface::TYPE_VIRTUAL)->save();
161  }
162  }
163  return $this;
164  }
165 
171  public function checkAllowedThemeRelations()
172  {
173  foreach ($this->_forbiddenRelations as $typesSequence) {
174  list($parentType, $childType) = $typesSequence;
175  $collection = $this->_collectionFactory->create();
176  $collection->addTypeRelationFilter($parentType, $childType);
178  foreach ($collection as $theme) {
179  $parentId = $this->_getResetParentId($theme);
180  if ($theme->getParentId() != $parentId) {
181  $theme->setParentId($parentId)->save();
182  }
183  }
184  }
185  return $this;
186  }
187 
195  {
196  $parentTheme = $theme->getParentTheme();
197  while ($parentTheme) {
198  foreach ($this->_allowedRelations as $typesSequence) {
199  list($parentType, $childType) = $typesSequence;
200  if ($theme->getType() == $childType && $parentTheme->getType() == $parentType) {
201  return $parentTheme->getId();
202  }
203  }
204  $parentTheme = $parentTheme->getParentTheme();
205  }
206  return null;
207  }
208 }
_getResetParentId(ThemeInterface $theme)
__()
Definition: __.php:13
_registerThemeRecursively(&$theme, $inheritanceChain=[])
$theme
__construct(\Magento\Theme\Model\ResourceModel\Theme\Data\CollectionFactory $collectionFactory, \Magento\Theme\Model\Theme\Data\Collection $filesystemCollection)
_savePreviewImage(ThemeInterface $theme)