Placeholder.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\Config;
  7. /**
  8. * Placeholder configuration values processor. Replace placeholders in configuration with config values
  9. */
  10. class Placeholder
  11. {
  12. /**
  13. * @var \Magento\Framework\App\RequestInterface
  14. */
  15. protected $request;
  16. /**
  17. * @var string[]
  18. */
  19. protected $urlPaths;
  20. /**
  21. * @var string
  22. */
  23. protected $urlPlaceholder;
  24. /**
  25. * @param \Magento\Framework\App\RequestInterface $request
  26. * @param string[] $urlPaths
  27. * @param string $urlPlaceholder
  28. */
  29. public function __construct(\Magento\Framework\App\RequestInterface $request, $urlPaths, $urlPlaceholder)
  30. {
  31. $this->request = $request;
  32. $this->urlPaths = $urlPaths;
  33. $this->urlPlaceholder = $urlPlaceholder;
  34. }
  35. /**
  36. * Replace placeholders with config values
  37. *
  38. * @param array $data
  39. * @return array
  40. */
  41. public function process(array $data = [])
  42. {
  43. foreach (array_keys($data) as $key) {
  44. $this->_processData($data, $key);
  45. }
  46. return $data;
  47. }
  48. /**
  49. * Process array data recursively
  50. *
  51. * @param array &$data
  52. * @param string $path
  53. * @return void
  54. */
  55. protected function _processData(&$data, $path)
  56. {
  57. $configValue = $this->_getValue($path, $data);
  58. if (is_array($configValue)) {
  59. foreach (array_keys($configValue) as $key) {
  60. $this->_processData($data, $path . '/' . $key);
  61. }
  62. } else {
  63. $this->_setValue($data, $path, $this->_processPlaceholders($configValue, $data));
  64. }
  65. }
  66. /**
  67. * Replace placeholders with config values
  68. *
  69. * @param string $value
  70. * @param array $data
  71. * @return string
  72. */
  73. protected function _processPlaceholders($value, $data)
  74. {
  75. $placeholder = $this->_getPlaceholder($value);
  76. if ($placeholder) {
  77. $url = false;
  78. if ($placeholder == 'unsecure_base_url') {
  79. $url = $this->_getValue($this->urlPaths['unsecureBaseUrl'], $data);
  80. } elseif ($placeholder == 'secure_base_url') {
  81. $url = $this->_getValue($this->urlPaths['secureBaseUrl'], $data);
  82. }
  83. if ($url) {
  84. $value = str_replace('{{' . $placeholder . '}}', $url, $value);
  85. } elseif (strpos($value, $this->urlPlaceholder) !== false) {
  86. $distroBaseUrl = $this->request->getDistroBaseUrl();
  87. $value = str_replace($this->urlPlaceholder, $distroBaseUrl, $value);
  88. }
  89. if (null !== $this->_getPlaceholder($value)) {
  90. $value = $this->_processPlaceholders($value, $data);
  91. }
  92. }
  93. return $value;
  94. }
  95. /**
  96. * Get placeholder from value
  97. *
  98. * @param string $value
  99. * @return string|null
  100. */
  101. protected function _getPlaceholder($value)
  102. {
  103. if (is_string($value) && preg_match('/{{(.*)}}.*/', $value, $matches)) {
  104. $placeholder = $matches[1];
  105. if ($placeholder == 'unsecure_base_url' || $placeholder == 'secure_base_url' || strpos(
  106. $value,
  107. $this->urlPlaceholder
  108. ) !== false
  109. ) {
  110. return $placeholder;
  111. }
  112. }
  113. return null;
  114. }
  115. /**
  116. * Get array value by path
  117. *
  118. * @param string $path
  119. * @param array $data
  120. * @return array|null
  121. */
  122. protected function _getValue($path, array $data)
  123. {
  124. $keys = explode('/', $path);
  125. foreach ($keys as $key) {
  126. if (is_array($data) && (isset($data[$key]) || array_key_exists($key, $data))) {
  127. $data = $data[$key];
  128. } else {
  129. return null;
  130. }
  131. }
  132. return $data;
  133. }
  134. /**
  135. * Set array value by path
  136. *
  137. * @param array &$container
  138. * @param string $path
  139. * @param string $value
  140. * @return void
  141. */
  142. protected function _setValue(array &$container, $path, $value)
  143. {
  144. $segments = explode('/', $path);
  145. $currentPointer = & $container;
  146. foreach ($segments as $segment) {
  147. if (!isset($currentPointer[$segment])) {
  148. $currentPointer[$segment] = [];
  149. }
  150. $currentPointer = & $currentPointer[$segment];
  151. }
  152. $currentPointer = $value;
  153. }
  154. }