SettingChecker.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Reader\Source\Deployed;
  7. use Magento\Config\Model\Config\Reader;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\DeploymentConfig;
  10. use Magento\Config\Model\Placeholder\PlaceholderInterface;
  11. use Magento\Config\Model\Placeholder\PlaceholderFactory;
  12. use Magento\Framework\App\Config\ScopeCodeResolver;
  13. /**
  14. * Class for checking settings that defined in config file
  15. * @api
  16. * @since 100.1.2
  17. */
  18. class SettingChecker
  19. {
  20. /**
  21. * @var DeploymentConfig
  22. */
  23. private $config;
  24. /**
  25. * @var PlaceholderInterface
  26. */
  27. private $placeholder;
  28. /**
  29. * @var ScopeCodeResolver
  30. */
  31. private $scopeCodeResolver;
  32. /**
  33. * @param DeploymentConfig $config
  34. * @param PlaceholderFactory $placeholderFactory
  35. * @param ScopeCodeResolver $scopeCodeResolver
  36. */
  37. public function __construct(
  38. DeploymentConfig $config,
  39. PlaceholderFactory $placeholderFactory,
  40. ScopeCodeResolver $scopeCodeResolver
  41. ) {
  42. $this->config = $config;
  43. $this->scopeCodeResolver = $scopeCodeResolver;
  44. $this->placeholder = $placeholderFactory->create(PlaceholderFactory::TYPE_ENVIRONMENT);
  45. }
  46. /**
  47. * Check that setting defined in deployed configuration
  48. *
  49. * @param string $path
  50. * @param string $scope
  51. * @param string|null $scopeCode
  52. * @return boolean
  53. * @since 100.1.2
  54. */
  55. public function isReadOnly($path, $scope, $scopeCode = null)
  56. {
  57. $config = $this->getEnvValue(
  58. $this->placeholder->generate($path, $scope, $scopeCode)
  59. );
  60. if (null === $config) {
  61. $config = $this->config->get($this->resolvePath($scope, $scopeCode) . "/" . $path);
  62. }
  63. return $config !== null;
  64. }
  65. /**
  66. * Check that there is value for generated placeholder
  67. *
  68. * Placeholder is generated from values of $path, $scope and $scopeCode
  69. *
  70. * @param string $path
  71. * @param string $scope
  72. * @param string $scopeCode
  73. * @param string|null $scopeCode
  74. * @return string|null
  75. * @since 100.1.2
  76. */
  77. public function getPlaceholderValue($path, $scope, $scopeCode = null)
  78. {
  79. return $this->getEnvValue($this->placeholder->generate($path, $scope, $scopeCode));
  80. }
  81. /**
  82. * Retrieve value of environment variable by placeholder
  83. *
  84. * @param string $placeholder
  85. * @return string|null
  86. * @since 100.1.2
  87. */
  88. public function getEnvValue($placeholder)
  89. {
  90. if ($this->placeholder->isApplicable($placeholder) && isset($_ENV[$placeholder])) {
  91. return $_ENV[$placeholder];
  92. }
  93. return null;
  94. }
  95. /**
  96. * Resolve path by scope and scope code
  97. *
  98. * @param string $scope
  99. * @param string $scopeCode
  100. * @return string
  101. */
  102. private function resolvePath($scope, $scopeCode)
  103. {
  104. $scopePath = 'system/' . $scope;
  105. if ($scope != ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  106. $scopePath .= '/' . $this->scopeCodeResolver->resolve($scope, $scopeCode);
  107. }
  108. return $scopePath;
  109. }
  110. }