Scoped.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Data;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. /**
  10. * Provides scoped configuration
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Scoped extends \Magento\Framework\Config\Data
  15. {
  16. /**
  17. * Configuration scope resolver
  18. *
  19. * @var \Magento\Framework\Config\ScopeInterface
  20. */
  21. protected $_configScope;
  22. /**
  23. * Configuration reader
  24. *
  25. * @var \Magento\Framework\Config\ReaderInterface
  26. */
  27. protected $_reader;
  28. /**
  29. * Configuration cache
  30. *
  31. * @var \Magento\Framework\Config\CacheInterface
  32. */
  33. protected $_cache;
  34. /**
  35. * Cache tag
  36. *
  37. * @var string
  38. */
  39. protected $_cacheId;
  40. /**
  41. * Scope priority loading scheme
  42. *
  43. * @var string[]
  44. */
  45. protected $_scopePriorityScheme = [];
  46. /**
  47. * Loaded scopes
  48. *
  49. * @var array
  50. */
  51. protected $_loadedScopes = [];
  52. /**
  53. * @var SerializerInterface
  54. */
  55. private $serializer;
  56. /**
  57. * Constructor
  58. *
  59. * @param \Magento\Framework\Config\ReaderInterface $reader
  60. * @param \Magento\Framework\Config\ScopeInterface $configScope
  61. * @param \Magento\Framework\Config\CacheInterface $cache
  62. * @param string $cacheId
  63. * @param SerializerInterface|null $serializer
  64. */
  65. public function __construct(
  66. \Magento\Framework\Config\ReaderInterface $reader,
  67. \Magento\Framework\Config\ScopeInterface $configScope,
  68. \Magento\Framework\Config\CacheInterface $cache,
  69. $cacheId,
  70. SerializerInterface $serializer = null
  71. ) {
  72. $this->_reader = $reader;
  73. $this->_configScope = $configScope;
  74. $this->_cache = $cache;
  75. $this->_cacheId = $cacheId;
  76. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  77. }
  78. /**
  79. * Get config value by key
  80. *
  81. * @param string $path
  82. * @param mixed $default
  83. * @return array|mixed|null
  84. */
  85. public function get($path = null, $default = null)
  86. {
  87. $this->_loadScopedData();
  88. return parent::get($path, $default);
  89. }
  90. /**
  91. * Load data for current scope
  92. *
  93. * @return void
  94. */
  95. protected function _loadScopedData()
  96. {
  97. $scope = $this->_configScope->getCurrentScope();
  98. if (false == isset($this->_loadedScopes[$scope])) {
  99. if (false == in_array($scope, $this->_scopePriorityScheme)) {
  100. $this->_scopePriorityScheme[] = $scope;
  101. }
  102. foreach ($this->_scopePriorityScheme as $scopeCode) {
  103. if (false == isset($this->_loadedScopes[$scopeCode])) {
  104. if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId))
  105. ) {
  106. $data = $this->serializer->unserialize($data);
  107. } else {
  108. $data = $this->_reader->read($scopeCode);
  109. if ($scopeCode !== 'primary') {
  110. $this->_cache->save(
  111. $this->serializer->serialize($data),
  112. $scopeCode . '::' . $this->_cacheId
  113. );
  114. }
  115. }
  116. $this->merge($data);
  117. $this->_loadedScopes[$scopeCode] = true;
  118. }
  119. if ($scopeCode == $scope) {
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. }