6
6
namespace Magento \Theme \Model \Theme ;
7
7
8
8
use Magento \Framework \App \ObjectManager ;
9
+ use Magento \Framework \Serialize \Serializer \Json ;
9
10
use Magento \Framework \View \Design \Theme \ListInterface ;
10
11
use Magento \Framework \App \DeploymentConfig ;
11
12
@@ -44,25 +45,33 @@ class ThemeProvider implements \Magento\Framework\View\Design\Theme\ThemeProvide
44
45
*/
45
46
private $ deploymentConfig ;
46
47
48
+ /**
49
+ * @var Json
50
+ */
51
+ private $ serializer ;
52
+
47
53
/**
48
54
* ThemeProvider constructor.
49
55
*
50
56
* @param \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory
51
57
* @param \Magento\Theme\Model\ThemeFactory $themeFactory
52
58
* @param \Magento\Framework\App\CacheInterface $cache
59
+ * @param Json $serializer
53
60
*/
54
61
public function __construct (
55
62
\Magento \Theme \Model \ResourceModel \Theme \CollectionFactory $ collectionFactory ,
56
63
\Magento \Theme \Model \ThemeFactory $ themeFactory ,
57
- \Magento \Framework \App \CacheInterface $ cache
64
+ \Magento \Framework \App \CacheInterface $ cache ,
65
+ Json $ serializer = null
58
66
) {
59
67
$ this ->collectionFactory = $ collectionFactory ;
60
68
$ this ->themeFactory = $ themeFactory ;
61
69
$ this ->cache = $ cache ;
70
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
62
71
}
63
72
64
73
/**
65
- * { @inheritdoc}
74
+ * @inheritdoc
66
75
*/
67
76
public function getThemeByFullPath ($ fullPath )
68
77
{
@@ -74,26 +83,24 @@ public function getThemeByFullPath($fullPath)
74
83
return $ this ->getThemeList ()->getThemeByFullPath ($ fullPath );
75
84
}
76
85
77
- /** @var $themeCollection \Magento\Theme\Model\ResourceModel\Theme\Collection */
78
- $ theme = $ this ->cache ->load ('theme ' . $ fullPath );
86
+ $ theme = $ this ->loadThemeFromCache ('theme ' . $ fullPath );
79
87
if ($ theme ) {
80
- $ this ->themes [$ fullPath ] = unserialize ( $ theme) ;
81
- return $ this -> themes [ $ fullPath ] ;
88
+ $ this ->themes [$ fullPath ] = $ theme ;
89
+ return $ theme ;
82
90
}
83
91
$ 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 ;
90
97
}
91
98
92
- return $ item ;
99
+ return $ theme ;
93
100
}
94
101
95
102
/**
96
- * { @inheritdoc}
103
+ * @inheritdoc
97
104
*/
98
105
public function getThemeCustomizations (
99
106
$ area = \Magento \Framework \App \Area::AREA_FRONTEND ,
@@ -106,26 +113,57 @@ public function getThemeCustomizations(
106
113
}
107
114
108
115
/**
109
- * { @inheritdoc}
116
+ * @inheritdoc
110
117
*/
111
118
public function getThemeById ($ themeId )
112
119
{
113
120
if (isset ($ this ->themes [$ themeId ])) {
114
121
return $ this ->themes [$ themeId ];
115
122
}
116
- $ theme = $ this ->cache -> load ('theme-by-id- ' . $ themeId );
123
+ $ theme = $ this ->loadThemeFromCache ('theme-by-id- ' . $ themeId );
117
124
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 ;
120
134
}
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 ;
127
151
}
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 );
129
167
}
130
168
131
169
/**
0 commit comments