Config.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\TestFramework\App;
  9. use Magento\Framework\App\Config\ScopeCodeResolver;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\DataObject;
  12. use Magento\TestFramework\ObjectManager;
  13. /**
  14. * @inheritdoc
  15. */
  16. class Config extends \Magento\Framework\App\Config
  17. {
  18. /**
  19. * @var DataObject[]
  20. */
  21. private $data;
  22. /**
  23. * @var ScopeCodeResolver
  24. */
  25. private $scopeCodeResolver;
  26. /**
  27. * Initialize data object with all settings data
  28. *
  29. * @param array $data
  30. * @param string $configType
  31. * @return void
  32. */
  33. private function setData(array $data, $configType)
  34. {
  35. $this->data[$configType] = new DataObject($data);
  36. }
  37. /**
  38. * Retrieve Scope Code Resolver
  39. *
  40. * @return ScopeCodeResolver
  41. */
  42. private function getScopeCodeResolver()
  43. {
  44. if (!$this->scopeCodeResolver) {
  45. $this->scopeCodeResolver = ObjectManager::getInstance()->get(ScopeCodeResolver::class);
  46. }
  47. return $this->scopeCodeResolver;
  48. }
  49. /**
  50. * Set config value in the corresponding config scope
  51. *
  52. * @param string $path
  53. * @param mixed $value
  54. * @param string $scope
  55. * @param null|string $scopeCode
  56. * @return void
  57. */
  58. public function setValue(
  59. $path,
  60. $value,
  61. $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  62. $scopeCode = null
  63. ) {
  64. $result = $this->get('system');
  65. if ($scope === 'store') {
  66. $scope = 'stores';
  67. } elseif ($scope === 'website') {
  68. $scope = 'websites';
  69. }
  70. if (empty($scopeCode)) {
  71. $scopeCode = $this->getScopeCodeResolver()->resolve($scope, $scopeCode);
  72. }
  73. $keys = explode('/', $path);
  74. if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  75. $searchKeys = array_merge([$scope, $scopeCode], $keys);
  76. } else {
  77. $searchKeys = array_merge([$scope], $keys);
  78. }
  79. $this->updateResult($searchKeys, $result, $value);
  80. $this->setData($result, 'system');
  81. }
  82. /**
  83. * Recursively update results in global variable, which hold configs
  84. *
  85. * @param array $keys
  86. * @param array $result
  87. * @param mixed $value
  88. * @return void
  89. */
  90. private function updateResult(array $keys, & $result, $value)
  91. {
  92. $key = array_shift($keys);
  93. if (empty($keys)) {
  94. $result[$key] = $value;
  95. } else {
  96. $this->updateResult($keys, $result[$key], $value);
  97. }
  98. }
  99. /**
  100. * Flush all muted settings
  101. *
  102. * @return void
  103. */
  104. public function clean()
  105. {
  106. $this->data = null;
  107. $this->scopeCodeResolver = null;
  108. parent::clean();
  109. }
  110. /**
  111. * @inheritdoc
  112. */
  113. public function get($configType, $path = null, $default = null)
  114. {
  115. $path = $path === null ? '' : $path;
  116. if (!isset($this->data[$configType]) || $this->data[$configType]->getData($path) === null) {
  117. return parent::get($configType, $path, $default);
  118. }
  119. return $this->data[$configType]->getData($path);
  120. }
  121. }