9
9
*/
10
10
namespace Magento \Framework \App \Cache \Frontend ;
11
11
12
+ use Cm_Cache_Backend_File ;
13
+ use Exception ;
14
+ use LogicException ;
12
15
use Magento \Framework \App \Filesystem \DirectoryList ;
16
+ use Magento \Framework \App \ResourceConnection ;
17
+ use Magento \Framework \Cache \Backend \Database ;
18
+ use Magento \Framework \Cache \Backend \Eaccelerator ;
19
+ use Magento \Framework \Cache \Backend \RemoteSynchronizedCache ;
20
+ use Magento \Framework \Cache \Core ;
21
+ use Magento \Framework \Cache \Frontend \Adapter \Zend ;
22
+ use Magento \Framework \Cache \FrontendInterface ;
13
23
use Magento \Framework \Filesystem ;
24
+ use Magento \Framework \ObjectManagerInterface ;
25
+ use Magento \Framework \Profiler ;
26
+ use UnexpectedValueException ;
27
+ use Zend_Cache ;
14
28
15
29
/**
16
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -28,7 +42,7 @@ class Factory
28
42
const PARAM_CACHE_FORCED_OPTIONS = 'cache_options ' ;
29
43
30
44
/**
31
- * @var \Magento\Framework\ ObjectManagerInterface
45
+ * @var ObjectManagerInterface
32
46
*/
33
47
private $ _objectManager ;
34
48
@@ -75,21 +89,21 @@ class Factory
75
89
/**
76
90
* Resource
77
91
*
78
- * @var \Magento\Framework\App\ ResourceConnection
92
+ * @var ResourceConnection
79
93
*/
80
94
protected $ _resource ;
81
95
82
96
/**
83
- * @param \Magento\Framework\ ObjectManagerInterface $objectManager
97
+ * @param ObjectManagerInterface $objectManager
84
98
* @param Filesystem $filesystem
85
- * @param \Magento\Framework\App\ ResourceConnection $resource
99
+ * @param ResourceConnection $resource
86
100
* @param array $enforcedOptions
87
101
* @param array $decorators
88
102
*/
89
103
public function __construct (
90
- \ Magento \ Framework \ ObjectManagerInterface $ objectManager ,
104
+ ObjectManagerInterface $ objectManager ,
91
105
Filesystem $ filesystem ,
92
- \ Magento \ Framework \ App \ ResourceConnection $ resource ,
106
+ ResourceConnection $ resource ,
93
107
array $ enforcedOptions = [],
94
108
array $ decorators = []
95
109
) {
@@ -104,7 +118,7 @@ public function __construct(
104
118
* Return newly created cache frontend instance
105
119
*
106
120
* @param array $options
107
- * @return \Magento\Framework\Cache\ FrontendInterface
121
+ * @return FrontendInterface
108
122
*/
109
123
public function create (array $ options )
110
124
{
@@ -139,29 +153,32 @@ public function create(array $options)
139
153
'frontend_type ' => $ frontend ['type ' ],
140
154
'backend_type ' => $ backend ['type ' ],
141
155
];
142
- \ Magento \ Framework \ Profiler::start ('cache_frontend_create ' , $ profilerTags );
156
+ Profiler::start ('cache_frontend_create ' , $ profilerTags );
143
157
144
- /** @var $result \Magento\Framework\Cache\Frontend\Adapter\Zend */
145
- $ result = $ this ->_objectManager ->create (
146
- \Magento \Framework \Cache \Frontend \Adapter \Zend::class,
147
- [
148
- 'frontendFactory ' => function () use ($ frontend , $ backend ) {
149
- return \Zend_Cache::factory (
150
- $ frontend ['type ' ],
151
- $ backend ['type ' ],
152
- $ frontend ,
153
- $ backend ['options ' ],
154
- true ,
155
- true ,
156
- true
157
- );
158
- }
159
- ]
160
- );
158
+ try {
159
+ $ result = $ this ->_objectManager ->create (
160
+ Zend::class,
161
+ [
162
+ 'frontendFactory ' => function () use ($ frontend , $ backend ) {
163
+ return Zend_Cache::factory (
164
+ $ frontend ['type ' ],
165
+ $ backend ['type ' ],
166
+ $ frontend ,
167
+ $ backend ['options ' ],
168
+ true ,
169
+ true ,
170
+ true
171
+ );
172
+ },
173
+ ]
174
+ );
175
+ } catch (\Exception $ e ) {
176
+ $ result = $ this ->createCacheWithDefaultOptions ($ options );
177
+ }
161
178
$ result = $ this ->_applyDecorators ($ result );
162
179
163
180
// stop profiling
164
- \ Magento \ Framework \ Profiler::stop ('cache_frontend_create ' );
181
+ Profiler::stop ('cache_frontend_create ' );
165
182
return $ result ;
166
183
}
167
184
@@ -179,24 +196,24 @@ private function _getExpandedOptions(array $options)
179
196
/**
180
197
* Apply decorators to a cache frontend instance and return the topmost one
181
198
*
182
- * @param \Magento\Framework\Cache\ FrontendInterface $frontend
183
- * @return \Magento\Framework\Cache\ FrontendInterface
184
- * @throws \ LogicException
185
- * @throws \ UnexpectedValueException
199
+ * @param FrontendInterface $frontend
200
+ * @return FrontendInterface
201
+ * @throws LogicException
202
+ * @throws UnexpectedValueException
186
203
*/
187
- private function _applyDecorators (\ Magento \ Framework \ Cache \ FrontendInterface $ frontend )
204
+ private function _applyDecorators (FrontendInterface $ frontend )
188
205
{
189
206
foreach ($ this ->_decorators as $ decoratorConfig ) {
190
207
if (!isset ($ decoratorConfig ['class ' ])) {
191
- throw new \ LogicException ('Class has to be specified for a cache frontend decorator. ' );
208
+ throw new LogicException ('Class has to be specified for a cache frontend decorator. ' );
192
209
}
193
210
$ decoratorClass = $ decoratorConfig ['class ' ];
194
211
$ decoratorParams = isset ($ decoratorConfig ['parameters ' ]) ? $ decoratorConfig ['parameters ' ] : [];
195
212
$ decoratorParams ['frontend ' ] = $ frontend ;
196
213
// conventionally, 'frontend' argument is a decoration subject
197
214
$ frontend = $ this ->_objectManager ->create ($ decoratorClass , $ decoratorParams );
198
- if (!$ frontend instanceof \ Magento \ Framework \ Cache \ FrontendInterface) {
199
- throw new \ UnexpectedValueException ('Decorator has to implement the cache frontend interface. ' );
215
+ if (!$ frontend instanceof FrontendInterface) {
216
+ throw new UnexpectedValueException ('Decorator has to implement the cache frontend interface. ' );
200
217
}
201
218
}
202
219
return $ frontend ;
@@ -258,18 +275,18 @@ protected function _getBackendOptions(array $cacheOptions)
258
275
case 'varien_cache_backend_eaccelerator ' :
259
276
if (extension_loaded ('eaccelerator ' ) && ini_get ('eaccelerator.enable ' )) {
260
277
$ enableTwoLevels = true ;
261
- $ backendType = \ Magento \ Framework \ Cache \ Backend \ Eaccelerator::class;
278
+ $ backendType = Eaccelerator::class;
262
279
}
263
280
break ;
264
281
case 'database ' :
265
- $ backendType = \ Magento \ Framework \ Cache \ Backend \ Database::class;
282
+ $ backendType = Database::class;
266
283
$ options = $ this ->_getDbAdapterOptions ();
267
284
break ;
268
285
case 'remote_synchronized_cache ' :
269
- $ backendType = \ Magento \ Framework \ Cache \ Backend \ RemoteSynchronizedCache::class;
270
- $ options ['remote_backend ' ] = \ Magento \ Framework \ Cache \ Backend \ Database::class;
286
+ $ backendType = RemoteSynchronizedCache::class;
287
+ $ options ['remote_backend ' ] = Database::class;
271
288
$ options ['remote_backend_options ' ] = $ this ->_getDbAdapterOptions ();
272
- $ options ['local_backend ' ] = \ Cm_Cache_Backend_File::class;
289
+ $ options ['local_backend ' ] = Cm_Cache_Backend_File::class;
273
290
$ cacheDir = $ this ->_filesystem ->getDirectoryWrite (DirectoryList::CACHE );
274
291
$ options ['local_backend_options ' ]['cache_dir ' ] = $ cacheDir ->getAbsolutePath ();
275
292
$ cacheDir ->create ();
@@ -283,7 +300,7 @@ protected function _getBackendOptions(array $cacheOptions)
283
300
$ backendType = $ type ;
284
301
}
285
302
}
286
- } catch (\ Exception $ e ) {
303
+ } catch (Exception $ e ) {
287
304
}
288
305
}
289
306
}
@@ -358,7 +375,7 @@ protected function _getTwoLevelsBackendOptions($fastOptions, $cacheOptions)
358
375
$ options ['slow_backend_options ' ] = $ this ->_backendOptions ;
359
376
}
360
377
if ($ options ['slow_backend ' ] == 'database ' ) {
361
- $ options ['slow_backend ' ] = \ Magento \ Framework \ Cache \ Backend \ Database::class;
378
+ $ options ['slow_backend ' ] = Database::class;
362
379
$ options ['slow_backend_options ' ] = $ this ->_getDbAdapterOptions ();
363
380
if (isset ($ cacheOptions ['slow_backend_store_data ' ])) {
364
381
$ options ['slow_backend_options ' ]['store_data ' ] = (bool )$ cacheOptions ['slow_backend_store_data ' ];
@@ -392,8 +409,38 @@ protected function _getFrontendOptions(array $cacheOptions)
392
409
if (!array_key_exists ('automatic_cleaning_factor ' , $ options )) {
393
410
$ options ['automatic_cleaning_factor ' ] = 0 ;
394
411
}
395
- $ options ['type ' ] =
396
- isset ($ cacheOptions ['frontend ' ]) ? $ cacheOptions ['frontend ' ] : \Magento \Framework \Cache \Core::class;
412
+ $ options ['type ' ] = isset ($ cacheOptions ['frontend ' ]) ? $ cacheOptions ['frontend ' ] : Core::class;
397
413
return $ options ;
398
414
}
415
+
416
+ /**
417
+ * Create frontend cache with default options.
418
+ *
419
+ * @param array $options
420
+ * @return Zend
421
+ */
422
+ private function createCacheWithDefaultOptions (array $ options ): Zend
423
+ {
424
+ unset($ options ['backend ' ]);
425
+ unset($ options ['frontend ' ]);
426
+ $ backend = $ this ->_getBackendOptions ($ options );
427
+ $ frontend = $ this ->_getFrontendOptions ($ options );
428
+
429
+ return $ this ->_objectManager ->create (
430
+ Zend::class,
431
+ [
432
+ 'frontendFactory ' => function () use ($ frontend , $ backend ) {
433
+ return Zend_Cache::factory (
434
+ $ frontend ['type ' ],
435
+ $ backend ['type ' ],
436
+ $ frontend ,
437
+ $ backend ['options ' ],
438
+ true ,
439
+ true ,
440
+ true
441
+ );
442
+ },
443
+ ]
444
+ );
445
+ }
399
446
}
0 commit comments