Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Factory.php
Go to the documentation of this file.
1 <?php
11 
14 
18 class Factory
19 {
23  const DEFAULT_LIFETIME = 7200;
24 
28  const PARAM_CACHE_FORCED_OPTIONS = 'cache_options';
29 
33  private $_objectManager;
34 
38  private $_filesystem;
39 
45  private $_enforcedOptions = [];
46 
56  private $_decorators = [];
57 
63  protected $_defaultBackend = 'Cm_Cache_Backend_File';
64 
70  protected $_backendOptions = [
71  'hashed_directory_level' => 1,
72  'file_name_prefix' => 'mage',
73  ];
74 
80  protected $_resource;
81 
89  public function __construct(
93  array $enforcedOptions = [],
94  array $decorators = []
95  ) {
96  $this->_objectManager = $objectManager;
97  $this->_filesystem = $filesystem;
98  $this->_resource = $resource;
99  $this->_enforcedOptions = $enforcedOptions;
100  $this->_decorators = $decorators;
101  }
102 
109  public function create(array $options)
110  {
111  $options = $this->_getExpandedOptions($options);
112 
113  foreach (['backend_options', 'slow_backend_options'] as $section) {
114  if (!empty($options[$section]['cache_dir'])) {
115  $directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
116  $directory->create($options[$section]['cache_dir']);
117  $options[$section]['cache_dir'] = $directory->getAbsolutePath($options[$section]['cache_dir']);
118  }
119  }
120 
121  $idPrefix = isset($options['id_prefix']) ? $options['id_prefix'] : '';
122  if (!$idPrefix && isset($options['prefix'])) {
123  $idPrefix = $options['prefix'];
124  }
125  if (empty($idPrefix)) {
126  $configDirPath = $this->_filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath();
127  $idPrefix =
128  substr(md5($configDirPath), 0, 3) . '_';
129  }
130  $options['frontend_options']['cache_id_prefix'] = $idPrefix;
131 
132  $backend = $this->_getBackendOptions($options);
133  $frontend = $this->_getFrontendOptions($options);
134 
135  // Start profiling
136  $profilerTags = [
137  'group' => 'cache',
138  'operation' => 'cache:create',
139  'frontend_type' => $frontend['type'],
140  'backend_type' => $backend['type'],
141  ];
142  \Magento\Framework\Profiler::start('cache_frontend_create', $profilerTags);
143 
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  );
161  $result = $this->_applyDecorators($result);
162 
163  // stop profiling
164  \Magento\Framework\Profiler::stop('cache_frontend_create');
165  return $result;
166  }
167 
174  private function _getExpandedOptions(array $options)
175  {
176  return array_replace_recursive($options, $this->_enforcedOptions);
177  }
178 
187  private function _applyDecorators(\Magento\Framework\Cache\FrontendInterface $frontend)
188  {
189  foreach ($this->_decorators as $decoratorConfig) {
190  if (!isset($decoratorConfig['class'])) {
191  throw new \LogicException('Class has to be specified for a cache frontend decorator.');
192  }
193  $decoratorClass = $decoratorConfig['class'];
194  $decoratorParams = isset($decoratorConfig['parameters']) ? $decoratorConfig['parameters'] : [];
195  $decoratorParams['frontend'] = $frontend;
196  // conventionally, 'frontend' argument is a decoration subject
197  $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.');
200  }
201  }
202  return $frontend;
203  }
204 
213  protected function _getBackendOptions(array $cacheOptions)
214  {
215  $enableTwoLevels = false;
216  $type = isset($cacheOptions['backend']) ? $cacheOptions['backend'] : $this->_defaultBackend;
217  if (isset($cacheOptions['backend_options']) && is_array($cacheOptions['backend_options'])) {
218  $options = $cacheOptions['backend_options'];
219  } else {
220  $options = [];
221  }
222 
223  $backendType = false;
224  switch (strtolower($type)) {
225  case 'sqlite':
226  if (extension_loaded('sqlite') && isset($options['cache_db_complete_path'])) {
227  $backendType = 'Sqlite';
228  }
229  break;
230  case 'memcached':
231  if (extension_loaded('memcached')) {
232  if (isset($cacheOptions['memcached'])) {
233  $options = $cacheOptions['memcached'];
234  }
235  $enableTwoLevels = true;
236  $backendType = 'Libmemcached';
237  } elseif (extension_loaded('memcache')) {
238  if (isset($cacheOptions['memcached'])) {
239  $options = $cacheOptions['memcached'];
240  }
241  $enableTwoLevels = true;
242  $backendType = 'Memcached';
243  }
244  break;
245  case 'apc':
246  if (extension_loaded('apc') && ini_get('apc.enabled')) {
247  $enableTwoLevels = true;
248  $backendType = 'Apc';
249  }
250  break;
251  case 'xcache':
252  if (extension_loaded('xcache')) {
253  $enableTwoLevels = true;
254  $backendType = 'Xcache';
255  }
256  break;
257  case 'eaccelerator':
258  case 'varien_cache_backend_eaccelerator':
259  if (extension_loaded('eaccelerator') && ini_get('eaccelerator.enable')) {
260  $enableTwoLevels = true;
261  $backendType = \Magento\Framework\Cache\Backend\Eaccelerator::class;
262  }
263  break;
264  case 'database':
265  $backendType = \Magento\Framework\Cache\Backend\Database::class;
266  $options = $this->_getDbAdapterOptions();
267  break;
268  case 'remote_synchronized_cache':
269  $backendType = \Magento\Framework\Cache\Backend\RemoteSynchronizedCache::class;
270  $options['remote_backend'] = \Magento\Framework\Cache\Backend\Database::class;
271  $options['remote_backend_options'] = $this->_getDbAdapterOptions();
272  $options['local_backend'] = \Cm_Cache_Backend_File::class;
273  $cacheDir = $this->_filesystem->getDirectoryWrite(DirectoryList::CACHE);
274  $options['local_backend_options']['cache_dir'] = $cacheDir->getAbsolutePath();
275  $cacheDir->create();
276  break;
277  default:
278  if ($type != $this->_defaultBackend) {
279  try {
280  if (class_exists($type, true)) {
281  $implements = class_implements($type, true);
282  if (in_array('Zend_Cache_Backend_Interface', $implements)) {
283  $backendType = $type;
284  }
285  }
286  } catch (\Exception $e) {
287  }
288  }
289  }
290  if (!$backendType) {
291  $backendType = $this->_defaultBackend;
292  $cacheDir = $this->_filesystem->getDirectoryWrite(DirectoryList::CACHE);
293  $this->_backendOptions['cache_dir'] = $cacheDir->getAbsolutePath();
294  $cacheDir->create();
295  }
296  foreach ($this->_backendOptions as $option => $value) {
297  if (!array_key_exists($option, $options)) {
299  }
300  }
301 
302  $backendOptions = ['type' => $backendType, 'options' => $options];
303  if ($enableTwoLevels) {
304  $backendOptions = $this->_getTwoLevelsBackendOptions($backendOptions, $cacheOptions);
305  }
306  return $backendOptions;
307  }
308 
314  protected function _getDbAdapterOptions()
315  {
316  $options['adapter_callback'] = function () {
317  return $this->_resource->getConnection();
318  };
319  $options['data_table_callback'] = function () {
320  return $this->_resource->getTableName('cache');
321  };
322  $options['tags_table_callback'] = function () {
323  return $this->_resource->getTableName('cache_tag');
324  };
325  return $options;
326  }
327 
335  protected function _getTwoLevelsBackendOptions($fastOptions, $cacheOptions)
336  {
337  $options = [];
338  $options['fast_backend'] = $fastOptions['type'];
339  $options['fast_backend_options'] = $fastOptions['options'];
340  $options['fast_backend_custom_naming'] = true;
341  $options['fast_backend_autoload'] = true;
342  $options['slow_backend_custom_naming'] = true;
343  $options['slow_backend_autoload'] = true;
344 
345  if (isset($cacheOptions['auto_refresh_fast_cache'])) {
346  $options['auto_refresh_fast_cache'] = (bool)$cacheOptions['auto_refresh_fast_cache'];
347  } else {
348  $options['auto_refresh_fast_cache'] = false;
349  }
350  if (isset($cacheOptions['slow_backend'])) {
351  $options['slow_backend'] = $cacheOptions['slow_backend'];
352  } else {
353  $options['slow_backend'] = $this->_defaultBackend;
354  }
355  if (isset($cacheOptions['slow_backend_options'])) {
356  $options['slow_backend_options'] = $cacheOptions['slow_backend_options'];
357  } else {
358  $options['slow_backend_options'] = $this->_backendOptions;
359  }
360  if ($options['slow_backend'] == 'database') {
361  $options['slow_backend'] = \Magento\Framework\Cache\Backend\Database::class;
362  $options['slow_backend_options'] = $this->_getDbAdapterOptions();
363  if (isset($cacheOptions['slow_backend_store_data'])) {
364  $options['slow_backend_options']['store_data'] = (bool)$cacheOptions['slow_backend_store_data'];
365  } else {
366  $options['slow_backend_options']['store_data'] = false;
367  }
368  }
369 
370  $backend = ['type' => 'TwoLevels', 'options' => $options];
371  return $backend;
372  }
373 
381  protected function _getFrontendOptions(array $cacheOptions)
382  {
383  $options = isset($cacheOptions['frontend_options']) ? $cacheOptions['frontend_options'] : [];
384  if (!array_key_exists('caching', $options)) {
385  $options['caching'] = true;
386  }
387  if (!array_key_exists('lifetime', $options)) {
388  $options['lifetime'] = isset(
389  $cacheOptions['lifetime']
390  ) ? $cacheOptions['lifetime'] : self::DEFAULT_LIFETIME;
391  }
392  if (!array_key_exists('automatic_cleaning_factor', $options)) {
393  $options['automatic_cleaning_factor'] = 0;
394  }
395  $options['type'] =
396  isset($cacheOptions['frontend']) ? $cacheOptions['frontend'] : \Magento\Framework\Cache\Core::class;
397  return $options;
398  }
399 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, Filesystem $filesystem, \Magento\Framework\App\ResourceConnection $resource, array $enforcedOptions=[], array $decorators=[])
Definition: Factory.php:89
_getTwoLevelsBackendOptions($fastOptions, $cacheOptions)
Definition: Factory.php:335
$filesystem