ValueProcessor.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Console\Command\ConfigShow;
  7. use Magento\Config\Model\Config\Backend\Encrypted;
  8. use Magento\Config\Model\Config\Structure;
  9. use Magento\Config\Model\Config\Structure\Element\Field;
  10. use Magento\Config\Model\Config\StructureFactory;
  11. use Magento\Framework\App\Area;
  12. use Magento\Framework\App\Config\Value;
  13. use Magento\Framework\App\Config\ValueFactory;
  14. use Magento\Framework\Config\ScopeInterface;
  15. use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
  16. /**
  17. * Class processes values using backend model which declared in system.xml.
  18. *
  19. * @api
  20. * @since 101.0.0
  21. */
  22. class ValueProcessor
  23. {
  24. /**
  25. * Placeholder for the output of sensitive data.
  26. */
  27. const SAFE_PLACEHOLDER = '******';
  28. /**
  29. * System configuration structure factory.
  30. *
  31. * @var StructureFactory
  32. */
  33. private $configStructureFactory;
  34. /**
  35. * Factory of object that implement \Magento\Framework\App\Config\ValueInterface.
  36. *
  37. * @var ValueFactory
  38. */
  39. private $configValueFactory;
  40. /**
  41. * Object for managing configuration scope.
  42. *
  43. * @var ScopeInterface
  44. */
  45. private $scope;
  46. /**
  47. * The json serializer.
  48. *
  49. * @var JsonSerializer
  50. */
  51. private $jsonSerializer;
  52. /**
  53. * @param ScopeInterface $scope The object for managing configuration scope
  54. * @param StructureFactory $structureFactory The system configuration structure factory.
  55. * @param ValueFactory $valueFactory The factory of object that
  56. * implement \Magento\Framework\App\Config\ValueInterface
  57. * @param JsonSerializer $jsonSerializer The json serializer
  58. */
  59. public function __construct(
  60. ScopeInterface $scope,
  61. StructureFactory $structureFactory,
  62. ValueFactory $valueFactory,
  63. JsonSerializer $jsonSerializer
  64. ) {
  65. $this->scope = $scope;
  66. $this->configStructureFactory = $structureFactory;
  67. $this->configValueFactory = $valueFactory;
  68. $this->jsonSerializer = $jsonSerializer;
  69. }
  70. /**
  71. * Processes value to display using backend model.
  72. *
  73. * @param string $scope The scope of configuration. E.g. 'default', 'website' or 'store'
  74. * @param string $scopeCode The scope code of configuration
  75. * @param string $value The value to process
  76. * @param string $path The configuration path for getting backend model. E.g. scope_id/group_id/field_id
  77. * @return string processed value result
  78. * @since 101.0.0
  79. */
  80. public function process($scope, $scopeCode, $value, $path)
  81. {
  82. $areaScope = $this->scope->getCurrentScope();
  83. $this->scope->setCurrentScope(Area::AREA_ADMINHTML);
  84. /** @var Structure $configStructure */
  85. $configStructure = $this->configStructureFactory->create();
  86. $this->scope->setCurrentScope($areaScope);
  87. /** @var Field $field */
  88. $field = $configStructure->getElementByConfigPath($path);
  89. /** @var Value $backendModel */
  90. $backendModel = $field instanceof Field && $field->hasBackendModel()
  91. ? $field->getBackendModel()
  92. : $this->configValueFactory->create();
  93. if ($backendModel instanceof Encrypted) {
  94. return $value ? self::SAFE_PLACEHOLDER : null;
  95. }
  96. $backendModel->setPath($path);
  97. $backendModel->setScope($scope);
  98. $backendModel->setScopeId($scopeCode);
  99. $backendModel->setValue($value);
  100. $backendModel->afterLoad();
  101. $processedValue = $backendModel->getValue();
  102. /**
  103. * If $processedValue is array it means that $value is json array (string).
  104. * It should be converted to string for displaying.
  105. */
  106. return is_array($processedValue) ? $this->jsonSerializer->serialize($processedValue) : $processedValue;
  107. }
  108. }