Skip to content

Commit bc3f67e

Browse files
Merge pull request #809 from magento-jackalopes/MAGETWO-59761-n-MAGETWO-62497
[Jackalopes] Magetwo 59761 n magetwo 62497
2 parents 53b0776 + 2eb5047 commit bc3f67e

File tree

18 files changed

+764
-154
lines changed

18 files changed

+764
-154
lines changed

app/code/Magento/Theme/Model/Theme.php

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Theme\Model;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\View\Design\ThemeInterface;
910
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
1011

@@ -78,6 +79,11 @@ class Theme extends \Magento\Framework\Model\AbstractModel implements ThemeInter
7879
*/
7980
protected $_customFactory;
8081

82+
/**
83+
* @var ThemeFactory
84+
*/
85+
private $themeModelFactory;
86+
8187
/**
8288
* @var ThemeInterface[]
8389
*/
@@ -96,7 +102,7 @@ class Theme extends \Magento\Framework\Model\AbstractModel implements ThemeInter
96102
* @param \Magento\Theme\Model\ResourceModel\Theme $resource
97103
* @param \Magento\Theme\Model\ResourceModel\Theme\Collection $resourceCollection
98104
* @param array $data
99-
*
105+
* @param ThemeFactory $themeModelFactory
100106
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
101107
*/
102108
public function __construct(
@@ -109,14 +115,16 @@ public function __construct(
109115
\Magento\Framework\View\Design\Theme\CustomizationFactory $customizationFactory,
110116
\Magento\Theme\Model\ResourceModel\Theme $resource = null,
111117
ThemeCollection $resourceCollection = null,
112-
array $data = []
118+
array $data = [],
119+
ThemeFactory $themeModelFactory = null
113120
) {
114121
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
115122
$this->_themeFactory = $themeFactory;
116123
$this->_domainFactory = $domainFactory;
117124
$this->_imageFactory = $imageFactory;
118125
$this->_validator = $validator;
119126
$this->_customFactory = $customizationFactory;
127+
$this->themeModelFactory = $themeModelFactory ?: ObjectManager::getInstance()->get(ThemeFactory::class);
120128
$this->addData(['type' => self::TYPE_VIRTUAL]);
121129
}
122130

@@ -377,52 +385,54 @@ public function getInheritedThemes()
377385
}
378386

379387
/**
380-
* {@inheritdoc}
388+
* @inheritdoc
381389
*/
382-
public function __sleep()
390+
public function toArray(array $keys = [])
383391
{
384-
$properties = parent::__sleep();
385-
$key = array_search('_logger', $properties);
386-
if (false !== $key) {
387-
unset($properties[$key]);
392+
$data = parent::toArray($keys);
393+
if (isset($data['parent_theme'])) {
394+
$data['parent_theme'] = $this->getParentTheme()->toArray();
388395
}
389-
return array_diff(
390-
$properties,
391-
[
392-
'_resource',
393-
'_resourceCollection',
394-
'_themeFactory',
395-
'_domainFactory',
396-
'_imageFactory',
397-
'_validator',
398-
'_customFactory'
399-
]
400-
);
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;
401404
}
402405

403406
/**
404-
* {@inheritdoc}
407+
* Populate Theme object from an array
408+
*
409+
* @param array $data
410+
* @return Theme
405411
*/
406-
public function __wakeup()
412+
public function populateFromArray(array $data)
407413
{
408-
parent::__wakeup();
409-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
410-
$this->_resource = $objectManager->get(\Magento\Theme\Model\ResourceModel\Theme::class);
411-
$this->_resourceCollection = $objectManager->get(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);
412-
$this->_themeFactory = $objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
413-
$this->_domainFactory = $objectManager->get(\Magento\Framework\View\Design\Theme\Domain\Factory::class);
414-
$this->_imageFactory = $objectManager->get(\Magento\Framework\View\Design\Theme\ImageFactory::class);
415-
$this->_validator = $objectManager->get(\Magento\Framework\View\Design\Theme\Validator::class);
416-
$this->_customFactory = $objectManager->get(\Magento\Framework\View\Design\Theme\CustomizationFactory::class);
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;
417427
}
418428

419429
/**
420-
* @param int $modelId
421-
* @param null $field
422-
* @return $this
430+
* Create Theme instance
431+
*
432+
* @return \Magento\Theme\Model\Theme
423433
*/
424-
public function load($modelId, $field = null)
434+
private function createThemeInstance()
425435
{
426-
return parent::load($modelId, $field); // TODO: Change the autogenerated stub
436+
return $this->themeModelFactory->create();
427437
}
428438
}

app/code/Magento/Theme/Model/Theme/ThemeProvider.php

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Theme\Model\Theme;
77

88
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\Serializer\Json;
910
use Magento\Framework\View\Design\Theme\ListInterface;
1011
use Magento\Framework\App\DeploymentConfig;
1112

@@ -44,25 +45,33 @@ class ThemeProvider implements \Magento\Framework\View\Design\Theme\ThemeProvide
4445
*/
4546
private $deploymentConfig;
4647

48+
/**
49+
* @var Json
50+
*/
51+
private $serializer;
52+
4753
/**
4854
* ThemeProvider constructor.
4955
*
5056
* @param \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory
5157
* @param \Magento\Theme\Model\ThemeFactory $themeFactory
5258
* @param \Magento\Framework\App\CacheInterface $cache
59+
* @param Json $serializer
5360
*/
5461
public function __construct(
5562
\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory,
5663
\Magento\Theme\Model\ThemeFactory $themeFactory,
57-
\Magento\Framework\App\CacheInterface $cache
64+
\Magento\Framework\App\CacheInterface $cache,
65+
Json $serializer = null
5866
) {
5967
$this->collectionFactory = $collectionFactory;
6068
$this->themeFactory = $themeFactory;
6169
$this->cache = $cache;
70+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
6271
}
6372

6473
/**
65-
* {@inheritdoc}
74+
* @inheritdoc
6675
*/
6776
public function getThemeByFullPath($fullPath)
6877
{
@@ -74,26 +83,24 @@ public function getThemeByFullPath($fullPath)
7483
return $this->getThemeList()->getThemeByFullPath($fullPath);
7584
}
7685

77-
/** @var $themeCollection \Magento\Theme\Model\ResourceModel\Theme\Collection */
78-
$theme = $this->cache->load('theme'. $fullPath);
86+
$theme = $this->loadThemeFromCache('theme' . $fullPath);
7987
if ($theme) {
80-
$this->themes[$fullPath] = unserialize($theme);
81-
return $this->themes[$fullPath];
88+
$this->themes[$fullPath] = $theme;
89+
return $theme;
8290
}
8391
$themeCollection = $this->collectionFactory->create();
84-
$item = $themeCollection->getThemeByFullPath($fullPath);
85-
if ($item->getId()) {
86-
$themeData = serialize($item);
87-
$this->cache->save($themeData, 'theme' . $fullPath);
88-
$this->cache->save($themeData, 'theme-by-id-' . $item->getId());
89-
$this->themes[$fullPath] = $item;
92+
$theme = $themeCollection->getThemeByFullPath($fullPath);
93+
if ($theme->getId()) {
94+
$this->saveThemeToCache($theme, 'theme' . $fullPath);
95+
$this->saveThemeToCache($theme, 'theme-by-id-' . $theme->getId());
96+
$this->themes[$fullPath] = $theme;
9097
}
9198

92-
return $item;
99+
return $theme;
93100
}
94101

95102
/**
96-
* {@inheritdoc}
103+
* @inheritdoc
97104
*/
98105
public function getThemeCustomizations(
99106
$area = \Magento\Framework\App\Area::AREA_FRONTEND,
@@ -106,26 +113,57 @@ public function getThemeCustomizations(
106113
}
107114

108115
/**
109-
* {@inheritdoc}
116+
* @inheritdoc
110117
*/
111118
public function getThemeById($themeId)
112119
{
113120
if (isset($this->themes[$themeId])) {
114121
return $this->themes[$themeId];
115122
}
116-
$theme = $this->cache->load('theme-by-id-' . $themeId);
123+
$theme = $this->loadThemeFromCache('theme-by-id-' . $themeId);
117124
if ($theme) {
118-
$this->themes[$themeId] = unserialize($theme);
119-
return $this->themes[$themeId];
125+
$this->themes[$themeId] = $theme;
126+
return $theme;
127+
}
128+
$theme = $this->themeFactory->create();
129+
$theme->load($themeId);
130+
if ($theme->getId()) {
131+
// We only cache by ID, as virtual themes may share the same path
132+
$this->saveThemeToCache($theme, 'theme-by-id-' . $themeId);
133+
$this->themes[$themeId] = $theme;
120134
}
121-
/** @var $themeModel \Magento\Framework\View\Design\ThemeInterface */
122-
$themeModel = $this->themeFactory->create();
123-
$themeModel->load($themeId);
124-
if ($themeModel->getId()) {
125-
$this->cache->save(serialize($themeModel), 'theme-by-id-' . $themeId);
126-
$this->themes[$themeId] = $themeModel;
135+
return $theme;
136+
}
137+
138+
/**
139+
* Load Theme model from cache
140+
*
141+
* @param string $cacheId
142+
* @return \Magento\Theme\Model\Theme|null
143+
*/
144+
private function loadThemeFromCache($cacheId)
145+
{
146+
$themeData = $this->cache->load($cacheId);
147+
if ($themeData) {
148+
$themeData = $this->serializer->unserialize($themeData);
149+
$theme = $this->themeFactory->create()->populateFromArray($themeData);
150+
return $theme;
127151
}
128-
return $themeModel;
152+
153+
return null;
154+
}
155+
156+
/**
157+
* Save Theme model to the cache
158+
*
159+
* @param \Magento\Theme\Model\Theme $theme
160+
* @param string $cacheId
161+
* @return void
162+
*/
163+
private function saveThemeToCache(\Magento\Theme\Model\Theme $theme, $cacheId)
164+
{
165+
$themeData = $this->serializer->serialize($theme->toArray());
166+
$this->cache->save($themeData, $cacheId);
129167
}
130168

131169
/**

0 commit comments

Comments
 (0)