Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
12 
14 {
20  protected $_cache;
21 
27  protected $_definitions;
28 
34  protected $_currentCacheKey;
35 
41  protected $_preferences = [];
42 
48  protected $_virtualTypes = [];
49 
55  protected $_arguments = [];
56 
62  protected $_nonShared = [];
63 
69  protected $_relations;
70 
76  protected $_mergedArguments;
77 
81  private $serializer;
82 
87  public function __construct(RelationsInterface $relations = null, DefinitionInterface $definitions = null)
88  {
89  $this->_relations = $relations ?: new \Magento\Framework\ObjectManager\Relations\Runtime();
90  $this->_definitions = $definitions ?: new \Magento\Framework\ObjectManager\Definition\Runtime();
91  }
92 
99  public function setRelations(RelationsInterface $relations)
100  {
101  $this->_relations = $relations;
102  }
103 
111  {
112  $this->_cache = $cache;
113  }
114 
121  public function getArguments($type)
122  {
123  if (isset($this->_mergedArguments[$type])) {
124  return $this->_mergedArguments[$type];
125  }
126  return $this->_collectConfiguration($type);
127  }
128 
135  public function isShared($type)
136  {
137  return !isset($this->_nonShared[$type]);
138  }
139 
146  public function getInstanceType($instanceName)
147  {
148  while (isset($this->_virtualTypes[$instanceName])) {
149  $instanceName = $this->_virtualTypes[$instanceName];
150  }
151  return $instanceName;
152  }
153 
161  public function getPreference($type)
162  {
163  $type = ltrim($type, '\\');
164  $preferencePath = [];
165  while (isset($this->_preferences[$type])) {
166  if (isset($preferencePath[$this->_preferences[$type]])) {
167  throw new \LogicException(
168  'Circular type preference: ' .
169  $type .
170  ' relates to ' .
171  $this->_preferences[$type] .
172  ' and viceversa.'
173  );
174  }
175  $type = $this->_preferences[$type];
176  $preferencePath[$type] = 1;
177  }
178  return $type;
179  }
180 
188  protected function _collectConfiguration($type)
189  {
190  if (!isset($this->_mergedArguments[$type])) {
191  if (isset($this->_virtualTypes[$type])) {
192  $arguments = $this->_collectConfiguration($this->_virtualTypes[$type]);
193  } elseif ($this->_relations->has($type)) {
194  $relations = $this->_relations->getParents($type);
195  $arguments = [];
196  foreach ($relations as $relation) {
197  if ($relation) {
198  $relationArguments = $this->_collectConfiguration($relation);
199  if ($relationArguments) {
200  $arguments = array_replace($arguments, $relationArguments);
201  }
202  }
203  }
204  } else {
205  $arguments = [];
206  }
207 
208  if (isset($this->_arguments[$type])) {
209  if ($arguments && count($arguments)) {
210  $arguments = array_replace_recursive($arguments, $this->_arguments[$type]);
211  } else {
212  $arguments = $this->_arguments[$type];
213  }
214  }
215  $this->_mergedArguments[$type] = $arguments;
216  return $arguments;
217  }
218  return $this->_mergedArguments[$type];
219  }
220 
228  protected function _mergeConfiguration(array $configuration)
229  {
230  foreach ($configuration as $key => $curConfig) {
231  switch ($key) {
232  case 'preferences':
233  foreach ($curConfig as $for => $to) {
234  $this->_preferences[ltrim($for, '\\')] = ltrim($to, '\\');
235  }
236  break;
237 
238  default:
239  $key = ltrim($key, '\\');
240  if (isset($curConfig['type'])) {
241  $this->_virtualTypes[$key] = ltrim($curConfig['type'], '\\');
242  }
243  if (isset($curConfig['arguments'])) {
244  if (!empty($this->_mergedArguments)) {
245  $this->_mergedArguments = [];
246  }
247  if (isset($this->_arguments[$key])) {
248  $this->_arguments[$key] = array_replace($this->_arguments[$key], $curConfig['arguments']);
249  } else {
250  $this->_arguments[$key] = $curConfig['arguments'];
251  }
252  }
253  if (isset($curConfig['shared'])) {
254  if (!$curConfig['shared']) {
255  $this->_nonShared[$key] = 1;
256  } else {
257  unset($this->_nonShared[$key]);
258  }
259  }
260  break;
261  }
262  }
263  }
264 
271  public function extend(array $configuration)
272  {
273  if ($this->_cache) {
274  if (!$this->_currentCacheKey) {
275  $this->_currentCacheKey = md5(
276  $this->getSerializer()->serialize(
277  [$this->_arguments, $this->_nonShared, $this->_preferences, $this->_virtualTypes]
278  )
279  );
280  }
281  $key = md5($this->_currentCacheKey . $this->getSerializer()->serialize($configuration));
282  $cached = $this->_cache->get($key);
283  if ($cached) {
284  list(
285  $this->_arguments,
286  $this->_nonShared,
287  $this->_preferences,
288  $this->_virtualTypes,
289  $this->_mergedArguments
290  ) = $cached;
291  } else {
292  $this->_mergeConfiguration($configuration);
293  if (!$this->_mergedArguments) {
294  foreach ($this->_definitions->getClasses() as $class) {
296  }
297  }
298  $this->_cache->save(
299  [
300  $this->_arguments,
301  $this->_nonShared,
302  $this->_preferences,
303  $this->_virtualTypes,
304  $this->_mergedArguments,
305  ],
306  $key
307  );
308  }
309  $this->_currentCacheKey = $key;
310  } else {
311  $this->_mergeConfiguration($configuration);
312  }
313  }
314 
320  public function getVirtualTypes()
321  {
322  return $this->_virtualTypes;
323  }
324 
330  public function getPreferences()
331  {
332  return $this->_preferences;
333  }
334 
341  private function getSerializer()
342  {
343  if ($this->serializer === null) {
345  ->get(SerializerInterface::class);
346  }
347  return $this->serializer;
348  }
349 }
_mergeConfiguration(array $configuration)
Definition: Config.php:228
setCache(ConfigCacheInterface $cache)
Definition: Config.php:110
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$configuration
Definition: index.php:33
setRelations(RelationsInterface $relations)
Definition: Config.php:99
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
__construct(RelationsInterface $relations=null, DefinitionInterface $definitions=null)
Definition: Config.php:87
$arguments