PreparedValueFactory.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model;
  7. use Magento\Config\Model\Config\BackendFactory;
  8. use Magento\Config\Model\Config\Structure;
  9. use Magento\Config\Model\Config\StructureFactory;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\Config\ValueInterface;
  12. use Magento\Framework\App\Config\Value;
  13. use Magento\Framework\App\ScopeInterface;
  14. use Magento\Store\Model\ScopeTypeNormalizer;
  15. use Magento\Framework\App\ScopeResolverPool;
  16. use Magento\Framework\Exception\RuntimeException;
  17. /**
  18. * Creates a prepared instance of Value.
  19. *
  20. * @see ValueInterface
  21. * @api
  22. * @since 101.0.0
  23. */
  24. class PreparedValueFactory
  25. {
  26. /**
  27. * The scope resolver pool.
  28. *
  29. * @var ScopeResolverPool
  30. */
  31. private $scopeResolverPool;
  32. /**
  33. * The manager for system configuration structure.
  34. *
  35. * @var StructureFactory
  36. */
  37. private $structureFactory;
  38. /**
  39. * The factory for configuration value objects.
  40. *
  41. * @see ValueInterface
  42. * @var BackendFactory
  43. */
  44. private $valueFactory;
  45. /**
  46. * The scope configuration.
  47. *
  48. * @var ScopeConfigInterface
  49. */
  50. private $config;
  51. /**
  52. * The scope type normalizer.
  53. *
  54. * @var ScopeTypeNormalizer
  55. */
  56. private $scopeTypeNormalizer;
  57. /**
  58. * @param ScopeResolverPool $scopeResolverPool The scope resolver pool
  59. * @param StructureFactory $structureFactory The manager for system configuration structure
  60. * @param BackendFactory $valueFactory The factory for configuration value objects
  61. * @param ScopeConfigInterface $config The scope configuration
  62. * @param ScopeTypeNormalizer $scopeTypeNormalizer The scope type normalizer
  63. */
  64. public function __construct(
  65. ScopeResolverPool $scopeResolverPool,
  66. StructureFactory $structureFactory,
  67. BackendFactory $valueFactory,
  68. ScopeConfigInterface $config,
  69. ScopeTypeNormalizer $scopeTypeNormalizer
  70. ) {
  71. $this->scopeResolverPool = $scopeResolverPool;
  72. $this->structureFactory = $structureFactory;
  73. $this->valueFactory = $valueFactory;
  74. $this->config = $config;
  75. $this->scopeTypeNormalizer = $scopeTypeNormalizer;
  76. }
  77. /**
  78. * Returns instance of Value with defined properties.
  79. *
  80. * @param string $path The configuration path in format section/group/field_name
  81. * @param string $value The configuration value
  82. * @param string $scope The configuration scope (default, website, or store)
  83. * @param string|int|null $scopeCode The scope code
  84. * @return ValueInterface
  85. * @throws RuntimeException If Value can not be created
  86. * @see ValueInterface
  87. * @since 101.0.0
  88. */
  89. public function create($path, $value, $scope, $scopeCode = null)
  90. {
  91. try {
  92. /** @var Structure $structure */
  93. $structure = $this->structureFactory->create();
  94. /** @var Structure\ElementInterface $field */
  95. $field = $structure->getElementByConfigPath($path);
  96. $configPath = $path;
  97. /** @var string $backendModelName */
  98. if ($field instanceof Structure\Element\Field && $field->hasBackendModel()) {
  99. $backendModelName = $field->getData()['backend_model'];
  100. $configPath = $field->getConfigPath() ?: $path;
  101. } else {
  102. $backendModelName = ValueInterface::class;
  103. }
  104. /** @var ValueInterface $backendModel */
  105. $backendModel = $this->valueFactory->create(
  106. $backendModelName,
  107. ['config' => $this->config]
  108. );
  109. if ($backendModel instanceof Value) {
  110. $scopeId = 0;
  111. $scope = $this->scopeTypeNormalizer->normalize($scope);
  112. if ($scope !== ScopeInterface::SCOPE_DEFAULT) {
  113. $scopeId = $this->scopeResolverPool->get($scope)
  114. ->getScope($scopeCode)
  115. ->getId();
  116. }
  117. if ($field instanceof Structure\Element\Field) {
  118. $groupPath = $field->getGroupPath();
  119. $group = $structure->getElement($groupPath);
  120. $backendModel->setField($field->getId());
  121. $backendModel->setGroupId($group->getId());
  122. $backendModel->setFieldConfig($field->getData());
  123. }
  124. $backendModel->setPath($configPath);
  125. $backendModel->setScope($scope);
  126. $backendModel->setScopeId($scopeId);
  127. $backendModel->setScopeCode($scopeCode);
  128. $backendModel->setValue($value);
  129. }
  130. return $backendModel;
  131. } catch (\Exception $exception) {
  132. throw new RuntimeException(__('%1', $exception->getMessage()), $exception);
  133. }
  134. }
  135. }