16
16
use Magento \Framework \Serialize \SerializerInterface ;
17
17
use Magento \Store \Model \Config \Processor \Fallback ;
18
18
use Magento \Store \Model \ScopeInterface as StoreScope ;
19
+ use Magento \Framework \Encryption \Encryptor ;
19
20
20
21
/**
21
22
* System configuration type
@@ -70,6 +71,11 @@ class System implements ConfigTypeInterface
70
71
*/
71
72
private $ availableDataScopes ;
72
73
74
+ /**
75
+ * @var Encryptor
76
+ */
77
+ private $ encryptor ;
78
+
73
79
/**
74
80
* @param ConfigSourceInterface $source
75
81
* @param PostProcessorInterface $postProcessor
@@ -79,9 +85,11 @@ class System implements ConfigTypeInterface
79
85
* @param PreProcessorInterface $preProcessor
80
86
* @param int $cachingNestedLevel
81
87
* @param string $configType
82
- * @param Reader $reader
88
+ * @param Reader|null $reader
89
+ * @param Encryptor|null $encryptor
83
90
*
84
91
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
92
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
85
93
*/
86
94
public function __construct (
87
95
ConfigSourceInterface $ source ,
@@ -92,17 +100,19 @@ public function __construct(
92
100
PreProcessorInterface $ preProcessor ,
93
101
$ cachingNestedLevel = 1 ,
94
102
$ configType = self ::CONFIG_TYPE ,
95
- Reader $ reader = null
103
+ Reader $ reader = null ,
104
+ Encryptor $ encryptor = null
96
105
) {
97
106
$ this ->postProcessor = $ postProcessor ;
98
107
$ this ->cache = $ cache ;
99
108
$ this ->serializer = $ serializer ;
100
109
$ this ->configType = $ configType ;
101
110
$ this ->reader = $ reader ?: ObjectManager::getInstance ()->get (Reader::class);
111
+ $ this ->encryptor = $ encryptor ?: ObjectManager::getInstance ()->get (\Magento \Framework \Encryption \Encryptor::class);
102
112
}
103
113
104
114
/**
105
- * Get config value by path.
115
+ * Get configuration value by path
106
116
*
107
117
* System configuration is separated by scopes (default, websites, stores). Configuration of a scope is inherited
108
118
* from its parent scope (store inherits website).
@@ -124,40 +134,14 @@ public function __construct(
124
134
public function get ($ path = '' )
125
135
{
126
136
if ($ path === '' ) {
127
- $ this ->data = array_replace_recursive ($ this ->data , $ this ->loadAllData () );
137
+ $ this ->data = array_replace_recursive ($ this ->loadAllData () , $ this ->data );
128
138
129
139
return $ this ->data ;
130
140
}
131
141
132
142
return $ this ->getWithParts ($ path );
133
143
}
134
144
135
- /**
136
- * Merge newly loaded config data into already loaded.
137
- *
138
- * @param array $newData
139
- * @return void
140
- */
141
- private function mergeData (array $ newData ): void
142
- {
143
- if (array_key_exists (ScopeInterface::SCOPE_DEFAULT , $ newData )) {
144
- //Sometimes new data may contain links to arrays and we don't want that.
145
- $ this ->data [ScopeInterface::SCOPE_DEFAULT ] = (array )$ newData [ScopeInterface::SCOPE_DEFAULT ];
146
- unset($ newData [ScopeInterface::SCOPE_DEFAULT ]);
147
- }
148
- foreach ($ newData as $ scopeType => $ scopeTypeData ) {
149
- if (!array_key_exists ($ scopeType , $ this ->data )) {
150
- //Sometimes new data may contain links to arrays and we don't want that.
151
- $ this ->data [$ scopeType ] = (array )$ scopeTypeData ;
152
- } else {
153
- foreach ($ scopeTypeData as $ scopeId => $ scopeData ) {
154
- //Sometimes new data may contain links to arrays and we don't want that.
155
- $ this ->data [$ scopeType ][$ scopeId ] = (array )$ scopeData ;
156
- }
157
- }
158
- }
159
- }
160
-
161
145
/**
162
146
* Proceed with parts extraction from path.
163
147
*
@@ -170,10 +154,8 @@ private function getWithParts($path)
170
154
171
155
if (count ($ pathParts ) === 1 && $ pathParts [0 ] !== ScopeInterface::SCOPE_DEFAULT ) {
172
156
if (!isset ($ this ->data [$ pathParts [0 ]])) {
173
- //First filling data property with unprocessed data for post-processors to be able to use.
174
157
$ data = $ this ->readData ();
175
- //Post-processing only the data we know is not yet processed.
176
- $ this ->mergeData ($ this ->postProcessor ->process ($ data ));
158
+ $ this ->data = array_replace_recursive ($ data , $ this ->data );
177
159
}
178
160
179
161
return $ this ->data [$ pathParts [0 ]];
@@ -183,11 +165,7 @@ private function getWithParts($path)
183
165
184
166
if ($ scopeType === ScopeInterface::SCOPE_DEFAULT ) {
185
167
if (!isset ($ this ->data [$ scopeType ])) {
186
- //Adding unprocessed data to the data property so it can be used in post-processing.
187
- $ this ->mergeData ($ scopeData = $ this ->loadDefaultScopeData ($ scopeType ));
188
- //Only post-processing the data we know is raw.
189
- $ scopeData = $ this ->postProcessor ->process ($ scopeData );
190
- $ this ->mergeData ($ scopeData );
168
+ $ this ->data = array_replace_recursive ($ this ->loadDefaultScopeData ($ scopeType ), $ this ->data );
191
169
}
192
170
193
171
return $ this ->getDataByPathParts ($ this ->data [$ scopeType ], $ pathParts );
@@ -197,11 +175,10 @@ private function getWithParts($path)
197
175
198
176
if (!isset ($ this ->data [$ scopeType ][$ scopeId ])) {
199
177
$ scopeData = $ this ->loadScopeData ($ scopeType , $ scopeId );
200
- //Adding unprocessed data to the data property so it can be used in post-processing.
201
- $ this ->mergeData ($ scopeData );
202
- //Only post-processing the data we know is raw.
203
- $ scopeData = $ this ->postProcessor ->process ($ scopeData );
204
- $ this ->mergeData ($ scopeData );
178
+
179
+ if (!isset ($ this ->data [$ scopeType ][$ scopeId ])) {
180
+ $ this ->data = array_replace_recursive ($ scopeData , $ this ->data );
181
+ }
205
182
}
206
183
207
184
return isset ($ this ->data [$ scopeType ][$ scopeId ])
@@ -221,11 +198,10 @@ private function loadAllData()
221
198
if ($ cachedData === false ) {
222
199
$ data = $ this ->readData ();
223
200
} else {
224
- $ data = $ this ->serializer ->unserialize ($ cachedData );
225
- $ this ->data = $ data ;
201
+ $ data = $ this ->serializer ->unserialize ($ this ->encryptor ->decrypt ($ cachedData ));
226
202
}
227
203
228
- return $ this -> postProcessor -> process ( $ data) ;
204
+ return $ data ;
229
205
}
230
206
231
207
/**
@@ -242,7 +218,7 @@ private function loadDefaultScopeData($scopeType)
242
218
$ data = $ this ->readData ();
243
219
$ this ->cacheData ($ data );
244
220
} else {
245
- $ data = [$ scopeType => $ this ->serializer ->unserialize ($ cachedData )];
221
+ $ data = [$ scopeType => $ this ->serializer ->unserialize ($ this -> encryptor -> decrypt ( $ cachedData) )];
246
222
}
247
223
248
224
return $ data ;
@@ -263,7 +239,8 @@ private function loadScopeData($scopeType, $scopeId)
263
239
if ($ this ->availableDataScopes === null ) {
264
240
$ cachedScopeData = $ this ->cache ->load ($ this ->configType . '_scopes ' );
265
241
if ($ cachedScopeData !== false ) {
266
- $ this ->availableDataScopes = $ this ->serializer ->unserialize ($ cachedScopeData );
242
+ $ serializedCachedData = $ this ->encryptor ->decrypt ($ cachedScopeData );
243
+ $ this ->availableDataScopes = $ this ->serializer ->unserialize ($ serializedCachedData );
267
244
}
268
245
}
269
246
if (is_array ($ this ->availableDataScopes ) && !isset ($ this ->availableDataScopes [$ scopeType ][$ scopeId ])) {
@@ -272,14 +249,15 @@ private function loadScopeData($scopeType, $scopeId)
272
249
$ data = $ this ->readData ();
273
250
$ this ->cacheData ($ data );
274
251
} else {
275
- $ data = [$ scopeType => [$ scopeId => $ this ->serializer ->unserialize ($ cachedData )]];
252
+ $ serializedCachedData = $ this ->encryptor ->decrypt ($ cachedData );
253
+ $ data = [$ scopeType => [$ scopeId => $ this ->serializer ->unserialize ($ serializedCachedData )]];
276
254
}
277
255
278
256
return $ data ;
279
257
}
280
258
281
259
/**
282
- * Cache configuration data.
260
+ * Cache configuration data
283
261
*
284
262
* Caches data per scope to avoid reading data for all scopes on every request
285
263
*
@@ -289,12 +267,12 @@ private function loadScopeData($scopeType, $scopeId)
289
267
private function cacheData (array $ data )
290
268
{
291
269
$ this ->cache ->save (
292
- $ this ->serializer ->serialize ($ data ),
270
+ $ this ->encryptor -> encryptWithFastestAvailableAlgorithm ( $ this -> serializer ->serialize ($ data) ),
293
271
$ this ->configType ,
294
272
[self ::CACHE_TAG ]
295
273
);
296
274
$ this ->cache ->save (
297
- $ this ->serializer ->serialize ($ data ['default ' ]),
275
+ $ this ->encryptor -> encryptWithFastestAvailableAlgorithm ( $ this -> serializer ->serialize ($ data ['default ' ]) ),
298
276
$ this ->configType . '_default ' ,
299
277
[self ::CACHE_TAG ]
300
278
);
@@ -303,14 +281,14 @@ private function cacheData(array $data)
303
281
foreach ($ data [$ curScopeType ] ?? [] as $ curScopeId => $ curScopeData ) {
304
282
$ scopes [$ curScopeType ][$ curScopeId ] = 1 ;
305
283
$ this ->cache ->save (
306
- $ this ->serializer ->serialize ($ curScopeData ),
284
+ $ this ->encryptor -> encryptWithFastestAvailableAlgorithm ( $ this -> serializer ->serialize ($ curScopeData) ),
307
285
$ this ->configType . '_ ' . $ curScopeType . '_ ' . $ curScopeId ,
308
286
[self ::CACHE_TAG ]
309
287
);
310
288
}
311
289
}
312
290
$ this ->cache ->save (
313
- $ this ->serializer ->serialize ($ scopes ),
291
+ $ this ->encryptor -> encryptWithFastestAvailableAlgorithm ( $ this -> serializer ->serialize ($ scopes) ),
314
292
$ this ->configType . '_scopes ' ,
315
293
[self ::CACHE_TAG ]
316
294
);
@@ -346,6 +324,9 @@ private function getDataByPathParts($data, $pathParts)
346
324
private function readData (): array
347
325
{
348
326
$ this ->data = $ this ->reader ->read ();
327
+ $ this ->data = $ this ->postProcessor ->process (
328
+ $ this ->data
329
+ );
349
330
350
331
return $ this ->data ;
351
332
}
0 commit comments