Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Placeholder.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $request;
17 
21  protected $urlPaths;
22 
26  protected $urlPlaceholder;
27 
33  public function __construct(\Magento\Framework\App\RequestInterface $request, $urlPaths, $urlPlaceholder)
34  {
35  $this->request = $request;
36  $this->urlPaths = $urlPaths;
37  $this->urlPlaceholder = $urlPlaceholder;
38  }
39 
46  public function process(array $data = [])
47  {
48  foreach (array_keys($data) as $key) {
49  $this->_processData($data, $key);
50  }
51  return $data;
52  }
53 
61  protected function _processData(&$data, $path)
62  {
63  $configValue = $this->_getValue($path, $data);
64  if (is_array($configValue)) {
65  foreach (array_keys($configValue) as $key) {
66  $this->_processData($data, $path . '/' . $key);
67  }
68  } else {
69  $this->_setValue($data, $path, $this->_processPlaceholders($configValue, $data));
70  }
71  }
72 
80  protected function _processPlaceholders($value, $data)
81  {
82  $placeholder = $this->_getPlaceholder($value);
83  if ($placeholder) {
84  $url = false;
85  if ($placeholder == 'unsecure_base_url') {
86  $url = $this->_getValue($this->urlPaths['unsecureBaseUrl'], $data);
87  } elseif ($placeholder == 'secure_base_url') {
88  $url = $this->_getValue($this->urlPaths['secureBaseUrl'], $data);
89  }
90 
91  if ($url) {
92  $value = str_replace('{{' . $placeholder . '}}', $url, $value);
93  } elseif (strpos($value, $this->urlPlaceholder) !== false) {
94  $distroBaseUrl = $this->request->getDistroBaseUrl();
95 
96  $value = str_replace($this->urlPlaceholder, $distroBaseUrl, $value);
97  }
98 
99  if (null !== $this->_getPlaceholder($value)) {
101  }
102  }
103  return $value;
104  }
105 
112  protected function _getPlaceholder($value)
113  {
114  if (is_string($value) && preg_match('/{{(.*)}}.*/', $value, $matches)) {
115  $placeholder = $matches[1];
116  if ($placeholder == 'unsecure_base_url' || $placeholder == 'secure_base_url' || strpos(
117  $value,
118  $this->urlPlaceholder
119  ) !== false
120  ) {
121  return $placeholder;
122  }
123  }
124  return null;
125  }
126 
134  protected function _getValue($path, array $data)
135  {
136  $keys = explode('/', $path);
137  foreach ($keys as $key) {
138  if (is_array($data) && (isset($data[$key]) || array_key_exists($key, $data))) {
139  $data = $data[$key];
140  } else {
141  return null;
142  }
143  }
144  return $data;
145  }
146 
155  protected function _setValue(array &$container, $path, $value)
156  {
157  $segments = explode('/', $path);
158  $currentPointer = & $container;
159  foreach ($segments as $segment) {
160  if (!isset($currentPointer[$segment])) {
161  $currentPointer[$segment] = [];
162  }
163  $currentPointer = & $currentPointer[$segment];
164  }
165  $currentPointer = $value;
166  }
167 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_setValue(array &$container, $path, $value)
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\App\RequestInterface $request, $urlPaths, $urlPlaceholder)
Definition: Placeholder.php:33