Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Theme.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Theme\Model;
7 
11 
37 {
43  protected $_eventPrefix = 'theme';
44 
50  protected $_eventObject = 'theme';
51 
55  protected $_themeFactory;
56 
60  protected $_domainFactory;
61 
65  protected $_imageFactory;
66 
70  protected $_validator;
71 
75  protected $_customization;
76 
80  protected $_customFactory;
81 
85  private $themeModelFactory;
86 
91 
108  public function __construct(
109  \Magento\Framework\Model\Context $context,
110  \Magento\Framework\Registry $registry,
111  \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory,
112  \Magento\Framework\View\Design\Theme\Domain\Factory $domainFactory,
113  \Magento\Framework\View\Design\Theme\ImageFactory $imageFactory,
114  \Magento\Framework\View\Design\Theme\Validator $validator,
115  \Magento\Framework\View\Design\Theme\CustomizationFactory $customizationFactory,
116  \Magento\Theme\Model\ResourceModel\Theme $resource = null,
117  ThemeCollection $resourceCollection = null,
118  array $data = [],
119  ThemeFactory $themeModelFactory = null
120  ) {
121  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
122  $this->_themeFactory = $themeFactory;
123  $this->_domainFactory = $domainFactory;
124  $this->_imageFactory = $imageFactory;
125  $this->_validator = $validator;
126  $this->_customFactory = $customizationFactory;
127  $this->themeModelFactory = $themeModelFactory ?: ObjectManager::getInstance()->get(ThemeFactory::class);
128  $this->addData(['type' => self::TYPE_VIRTUAL]);
129  }
130 
136  protected function _construct()
137  {
138  $this->_init(\Magento\Theme\Model\ResourceModel\Theme::class);
139  }
140 
146  public function getThemeImage()
147  {
148  return $this->_imageFactory->create(['theme' => $this]);
149  }
150 
154  public function getCustomization()
155  {
156  if ($this->_customization === null) {
157  $this->_customization = $this->_customFactory->create(['theme' => $this]);
158  }
159  return $this->_customization;
160  }
161 
167  public function isDeletable()
168  {
169  return $this->isEditable();
170  }
171 
177  public function isEditable()
178  {
179  return self::TYPE_PHYSICAL != $this->getType();
180  }
181 
187  public function isVirtual()
188  {
189  return $this->getType() == self::TYPE_VIRTUAL;
190  }
191 
197  public function isPhysical()
198  {
199  return $this->getType() == self::TYPE_PHYSICAL;
200  }
201 
207  public function isVisible()
208  {
209  return in_array($this->getType(), [self::TYPE_PHYSICAL, self::TYPE_VIRTUAL]);
210  }
211 
217  public function hasChildThemes()
218  {
219  return (bool)$this->getCollection()->addTypeFilter(
220  self::TYPE_VIRTUAL
221  )->addFieldToFilter(
222  'parent_id',
223  ['eq' => $this->getId()]
224  )->getSize();
225  }
226 
232  public function getStagingVersion()
233  {
234  if ($this->getId()) {
235  $collection = $this->getCollection();
236  $collection->addFieldToFilter('parent_id', $this->getId());
237  $collection->addFieldToFilter('type', self::TYPE_STAGING);
238  $stagingTheme = $collection->getFirstItem();
239  if ($stagingTheme->getId()) {
240  return $stagingTheme;
241  }
242  }
243  return null;
244  }
245 
249  public function getParentTheme()
250  {
251  if ($this->hasData('parent_theme')) {
252  return $this->getData('parent_theme');
253  }
254 
255  $theme = null;
256  if ($this->getParentId()) {
257  $theme = $this->_themeFactory->create($this->getParentId());
258  }
259  $this->setParentTheme($theme);
260  return $theme;
261  }
262 
266  public function getArea()
267  {
268  // In order to support environment emulation of area, if area is set, return it
269  if ($this->getData('area') && !$this->_appState->isAreaCodeEmulated()) {
270  return $this->getData('area');
271  }
272  return $this->_appState->getAreaCode();
273  }
274 
278  public function getThemePath()
279  {
280  return $this->getData('theme_path');
281  }
282 
291  public function getFullPath()
292  {
293  return $this->getThemePath() ? $this->getArea() . self::PATH_SEPARATOR . $this->getThemePath() : null;
294  }
295 
299  public function getCode()
300  {
301  return (string)$this->getData('code');
302  }
303 
311  public function getDomainModel($type = null)
312  {
313  if ($type !== null && $type != $this->getType()) {
314  throw new \InvalidArgumentException(
315  sprintf(
316  'Invalid domain model "%s" requested for theme "%s" of type "%s"',
317  $type,
318  $this->getId(),
319  $this->getType()
320  )
321  );
322  }
323 
324  return $this->_domainFactory->create($this);
325  }
326 
333  protected function _validate()
334  {
335  if (!$this->_validator->validate($this)) {
336  $messages = $this->_validator->getErrorMessages();
337  throw new \Magento\Framework\Exception\LocalizedException(__(implode(PHP_EOL, reset($messages))));
338  }
339  return $this;
340  }
341 
347  public function beforeSave()
348  {
349  $this->_validate();
350  return parent::beforeSave();
351  }
352 
358  public function afterDelete()
359  {
360  $stagingVersion = $this->getStagingVersion();
361  if ($stagingVersion) {
362  $stagingVersion->delete();
363  }
364  $this->getCollection()->updateChildRelations($this);
365  return parent::afterDelete();
366  }
367 
373  public function getInheritedThemes()
374  {
375  if (null === $this->inheritanceSequence) {
376  $theme = $this;
377  $result = [];
378  while ($theme) {
379  $result[] = $theme;
380  $theme = $theme->getParentTheme();
381  }
382  $this->inheritanceSequence = array_reverse($result);
383  }
385  }
386 
390  public function toArray(array $keys = [])
391  {
392  $data = parent::toArray($keys);
393  if (isset($data['parent_theme'])) {
394  $data['parent_theme'] = $this->getParentTheme()->toArray();
395  }
396 
397  if (isset($data['inherited_themes'])) {
398  foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
399  $data['inherited_themes'][$key] = $inheritedTheme->toArray();
400  }
401  }
402 
403  return $data;
404  }
405 
412  public function populateFromArray(array $data)
413  {
414  $this->_data = $data;
415  if (isset($data['parent_theme'])) {
416  $this->_data['parent_theme'] = $this->createThemeInstance()->populateFromArray($data['parent_theme']);
417  }
418 
419  if (isset($data['inherited_themes'])) {
420  foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
421  $themeInstance = $this->createThemeInstance()->populateFromArray($inheritedTheme);
422  $this->_data['inherited_themes'][$key] = $themeInstance;
423  }
424  }
425 
426  return $this;
427  }
428 
434  private function createThemeInstance()
435  {
436  return $this->themeModelFactory->create();
437  }
438 }
__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)
Definition: Theme.php:108
getData($key='', $index=null)
Definition: DataObject.php:119
toArray(array $keys=[])
Definition: Theme.php:390
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$theme
getDomainModel($type=null)
Definition: Theme.php:311
populateFromArray(array $data)
Definition: Theme.php:412