Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pool.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class Pool implements \Iterator
15 {
19  const DEFAULT_FRONTEND_ID = 'default';
20 
24  private $deploymentConfig;
25 
29  private $_factory;
30 
34  private $_instances;
35 
39  private $_frontendSettings;
40 
46  public function __construct(
47  DeploymentConfig $deploymentConfig,
48  Factory $frontendFactory,
49  array $frontendSettings = []
50  ) {
51  $this->deploymentConfig = $deploymentConfig;
52  $this->_factory = $frontendFactory;
53  $this->_frontendSettings = $frontendSettings + [self::DEFAULT_FRONTEND_ID => []];
54  }
55 
62  protected function _initialize()
63  {
64  if ($this->_instances === null) {
65  $this->_instances = [];
66  foreach ($this->_getCacheSettings() as $frontendId => $frontendOptions) {
67  $this->_instances[$frontendId] = $this->_factory->create($frontendOptions);
68  }
69  }
70  }
71 
77  protected function _getCacheSettings()
78  {
79  /*
80  * Merging is intentionally implemented through array_merge() instead of array_replace_recursive()
81  * to avoid "inheritance" of the default settings that become irrelevant as soon as cache storage type changes
82  */
83  $cacheInfo = $this->deploymentConfig->getConfigData(FrontendPool::KEY_CACHE);
84  if (null !== $cacheInfo) {
85  return array_merge($this->_frontendSettings, $cacheInfo[FrontendPool::KEY_FRONTEND_CACHE]);
86  }
87  return $this->_frontendSettings;
88  }
89 
95  public function current()
96  {
97  $this->_initialize();
98  return current($this->_instances);
99  }
100 
104  public function key()
105  {
106  $this->_initialize();
107  return key($this->_instances);
108  }
109 
113  public function next()
114  {
115  $this->_initialize();
116  next($this->_instances);
117  }
118 
122  public function rewind()
123  {
124  $this->_initialize();
125  reset($this->_instances);
126  }
127 
131  public function valid()
132  {
133  $this->_initialize();
134  return (bool)current($this->_instances);
135  }
136 
144  public function get($identifier)
145  {
146  $this->_initialize();
147  if (isset($this->_instances[$identifier])) {
148  return $this->_instances[$identifier];
149  }
150  throw new \InvalidArgumentException("Cache frontend '{$identifier}' is not recognized.");
151  }
152 }
__construct(DeploymentConfig $deploymentConfig, Factory $frontendFactory, array $frontendSettings=[])
Definition: Pool.php:46
$deploymentConfig