Config.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Application configuration object. Used to access configuration when application is initialized and installed.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App;
  9. use Magento\Framework\App\Config\ConfigTypeInterface;
  10. use Magento\Framework\App\Config\ScopeCodeResolver;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. /**
  13. * Class Config
  14. */
  15. class Config implements ScopeConfigInterface
  16. {
  17. /**
  18. * Config cache tag
  19. */
  20. const CACHE_TAG = 'CONFIG';
  21. /**
  22. * @var ScopeCodeResolver
  23. */
  24. private $scopeCodeResolver;
  25. /**
  26. * @var ConfigTypeInterface[]
  27. */
  28. private $types;
  29. /**
  30. * Config constructor.
  31. *
  32. * @param ScopeCodeResolver $scopeCodeResolver
  33. * @param array $types
  34. */
  35. public function __construct(
  36. ScopeCodeResolver $scopeCodeResolver,
  37. array $types = []
  38. ) {
  39. $this->scopeCodeResolver = $scopeCodeResolver;
  40. $this->types = $types;
  41. }
  42. /**
  43. * Retrieve config value by path and scope
  44. *
  45. * @param string $path
  46. * @param string $scope
  47. * @param null|string $scopeCode
  48. * @return mixed
  49. */
  50. public function getValue(
  51. $path = null,
  52. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  53. $scopeCode = null
  54. ) {
  55. if ($scope === 'store') {
  56. $scope = 'stores';
  57. } elseif ($scope === 'website') {
  58. $scope = 'websites';
  59. }
  60. $configPath = $scope;
  61. if ($scope !== 'default') {
  62. if (is_numeric($scopeCode) || $scopeCode === null) {
  63. $scopeCode = $this->scopeCodeResolver->resolve($scope, $scopeCode);
  64. } elseif ($scopeCode instanceof \Magento\Framework\App\ScopeInterface) {
  65. $scopeCode = $scopeCode->getCode();
  66. }
  67. if ($scopeCode) {
  68. $configPath .= '/' . $scopeCode;
  69. }
  70. }
  71. if ($path) {
  72. $configPath .= '/' . $path;
  73. }
  74. return $this->get('system', $configPath);
  75. }
  76. /**
  77. * Retrieve config flag
  78. *
  79. * @param string $path
  80. * @param string $scope
  81. * @param null|string $scopeCode
  82. * @return bool
  83. */
  84. public function isSetFlag($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
  85. {
  86. return !!$this->getValue($path, $scope, $scopeCode);
  87. }
  88. /**
  89. * Invalidate cache by type
  90. * Clean scopeCodeResolver
  91. *
  92. * @return void
  93. */
  94. public function clean()
  95. {
  96. foreach ($this->types as $type) {
  97. $type->clean();
  98. }
  99. $this->scopeCodeResolver->clean();
  100. }
  101. /**
  102. * Retrieve configuration.
  103. *
  104. * ('modules') - modules status configuration data
  105. * ('scopes', 'websites/base') - base website data
  106. * ('scopes', 'stores/default') - default store data
  107. *
  108. * ('system', 'default/web/seo/use_rewrites') - default system configuration data
  109. * ('system', 'websites/base/web/seo/use_rewrites') - 'base' website system configuration data
  110. *
  111. * ('i18n', 'default/en_US') - translations for default store and 'en_US' locale
  112. *
  113. * @param string $configType
  114. * @param string|null $path
  115. * @param mixed|null $default
  116. * @return array
  117. */
  118. public function get($configType, $path = '', $default = null)
  119. {
  120. $result = null;
  121. if (isset($this->types[$configType])) {
  122. $result = $this->types[$configType]->get($path);
  123. }
  124. return $result !== null ? $result : $default;
  125. }
  126. }