Pool.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Cache\Frontend;
  7. use Magento\Framework\App\Cache\Type\FrontendPool;
  8. use Magento\Framework\App\DeploymentConfig;
  9. /**
  10. * In-memory readonly pool of all cache front-end instances known to the system
  11. */
  12. class Pool implements \Iterator
  13. {
  14. /**
  15. * Frontend identifier associated with the default settings
  16. */
  17. const DEFAULT_FRONTEND_ID = 'default';
  18. /**
  19. * @var DeploymentConfig
  20. */
  21. private $deploymentConfig;
  22. /**
  23. * @var Factory
  24. */
  25. private $_factory;
  26. /**
  27. * @var \Magento\Framework\Cache\FrontendInterface[]
  28. */
  29. private $_instances;
  30. /**
  31. * @var array
  32. */
  33. private $_frontendSettings;
  34. /**
  35. * @param DeploymentConfig $deploymentConfig
  36. * @param Factory $frontendFactory
  37. * @param array $frontendSettings Format: array('<frontend_id>' => array(<cache_settings>), ...)
  38. */
  39. public function __construct(
  40. DeploymentConfig $deploymentConfig,
  41. Factory $frontendFactory,
  42. array $frontendSettings = []
  43. ) {
  44. $this->deploymentConfig = $deploymentConfig;
  45. $this->_factory = $frontendFactory;
  46. $this->_frontendSettings = $frontendSettings + [self::DEFAULT_FRONTEND_ID => []];
  47. }
  48. /**
  49. * Create instances of every cache frontend known to the system.
  50. * Method is to be used for delayed initialization of the iterator.
  51. *
  52. * @return void
  53. */
  54. protected function _initialize()
  55. {
  56. if ($this->_instances === null) {
  57. $this->_instances = [];
  58. foreach ($this->_getCacheSettings() as $frontendId => $frontendOptions) {
  59. $this->_instances[$frontendId] = $this->_factory->create($frontendOptions);
  60. }
  61. }
  62. }
  63. /**
  64. * Retrieve settings for all cache front-ends known to the system
  65. *
  66. * @return array Format: array('<frontend_id>' => array(<cache_settings>), ...)
  67. */
  68. protected function _getCacheSettings()
  69. {
  70. /*
  71. * Merging is intentionally implemented through array_merge() instead of array_replace_recursive()
  72. * to avoid "inheritance" of the default settings that become irrelevant as soon as cache storage type changes
  73. */
  74. $cacheInfo = $this->deploymentConfig->getConfigData(FrontendPool::KEY_CACHE);
  75. if (null !== $cacheInfo) {
  76. return array_merge($this->_frontendSettings, $cacheInfo[FrontendPool::KEY_FRONTEND_CACHE]);
  77. }
  78. return $this->_frontendSettings;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. *
  83. * @return \Magento\Framework\Cache\FrontendInterface
  84. */
  85. public function current()
  86. {
  87. $this->_initialize();
  88. return current($this->_instances);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function key()
  94. {
  95. $this->_initialize();
  96. return key($this->_instances);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function next()
  102. {
  103. $this->_initialize();
  104. next($this->_instances);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function rewind()
  110. {
  111. $this->_initialize();
  112. reset($this->_instances);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function valid()
  118. {
  119. $this->_initialize();
  120. return (bool)current($this->_instances);
  121. }
  122. /**
  123. * Retrieve frontend instance by its unique identifier
  124. *
  125. * @param string $identifier Cache frontend identifier
  126. * @return \Magento\Framework\Cache\FrontendInterface Cache frontend instance
  127. * @throws \InvalidArgumentException
  128. */
  129. public function get($identifier)
  130. {
  131. $this->_initialize();
  132. if (isset($this->_instances[$identifier])) {
  133. return $this->_instances[$identifier];
  134. }
  135. throw new \InvalidArgumentException("Cache frontend '{$identifier}' is not recognized.");
  136. }
  137. }