Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Baseurl.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\Validator\Url as UrlValidator;
10 
15 class Baseurl extends \Magento\Framework\App\Config\Value
16 {
20  protected $_mergeService;
21 
25  private $urlValidator;
26 
37  public function __construct(
38  \Magento\Framework\Model\Context $context,
39  \Magento\Framework\Registry $registry,
41  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
42  \Magento\Framework\View\Asset\MergeService $mergeService,
43  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
44  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
45  array $data = []
46  ) {
47  $this->_mergeService = $mergeService;
48  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
49  }
50 
57  public function beforeSave()
58  {
59  $value = $this->getValue();
60  try {
61  if (!$this->_validateUnsecure($value) && !$this->_validateSecure($value)) {
62  $this->_validateFullyQualifiedUrl($value);
63  }
64  } catch (\Magento\Framework\Exception\LocalizedException $e) {
65  $field = $this->getFieldConfig();
66  $label = $field && is_array($field) ? $field['label'] : 'value';
67  $msg = __('Invalid %1. %2', $label, $e->getMessage());
68  $error = new \Magento\Framework\Exception\LocalizedException($msg, $e);
69  throw $error;
70  }
71  }
72 
79  private function _validateUnsecure($value)
80  {
81  $placeholders = ['{{unsecure_base_url}}'];
82  switch ($this->getPath()) {
83  case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL:
84  $this->_assertValuesOrUrl(['{{base_url}}'], $value);
85  break;
86  case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL:
87  $this->_assertStartsWithValuesOrUrl($placeholders, $value);
88  break;
89  case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_STATIC_URL:
90  case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL:
91  $this->_assertStartsWithValuesOrUrlOrEmpty($placeholders, $value);
92  break;
93  default:
94  return false;
95  }
96  return true;
97  }
98 
105  private function _validateSecure($value)
106  {
107  $placeholders = ['{{unsecure_base_url}}', '{{secure_base_url}}'];
108  switch ($this->getPath()) {
109  case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL:
110  $this->_assertValuesOrUrl(['{{base_url}}', '{{unsecure_base_url}}'], $value);
111  break;
112  case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL:
113  $this->_assertStartsWithValuesOrUrl($placeholders, $value);
114  break;
115  case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_STATIC_URL:
116  case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL:
117  $this->_assertStartsWithValuesOrUrlOrEmpty($placeholders, $value);
118  break;
119  default:
120  return false;
121  }
122  return true;
123  }
124 
133  private function _assertValuesOrUrl(array $values, $value)
134  {
135  if (!in_array($value, $values) && !$this->_isFullyQualifiedUrl($value)) {
136  throw new \Magento\Framework\Exception\LocalizedException(
137  __('Value must be a URL or one of placeholders: %1', implode(',', $values))
138  );
139  }
140  }
141 
150  private function _assertStartsWithValuesOrUrl(array $values, $value)
151  {
152  $quoted = array_map('preg_quote', $values, array_fill(0, count($values), '/'));
153  if (!preg_match('/^(' . implode('|', $quoted) . ')(.+\/)?$/', $value) && !$this->_isFullyQualifiedUrl($value)
154  ) {
155  throw new \Magento\Framework\Exception\LocalizedException(
156  __(
157  'Specify a URL or path that starts with placeholder(s): %1, and ends with "/".',
158  implode(', ', $values)
159  )
160  );
161  }
162  }
163 
172  private function _assertStartsWithValuesOrUrlOrEmpty(array $values, $value)
173  {
174  if (empty($value)) {
175  return;
176  }
177  try {
178  $this->_assertStartsWithValuesOrUrl($values, $value);
179  } catch (\Magento\Framework\Exception\LocalizedException $e) {
180  $msg = __('%1 An empty value is allowed as well.', $e->getMessage());
181  $error = new \Magento\Framework\Exception\LocalizedException($msg, $e);
182  throw $error;
183  }
184  }
185 
193  private function _validateFullyQualifiedUrl($value)
194  {
195  if (!$this->_isFullyQualifiedUrl($value)) {
196  throw new \Magento\Framework\Exception\LocalizedException(__('Specify a fully qualified URL.'));
197  }
198  }
199 
206  private function _isFullyQualifiedUrl($value)
207  {
208  return preg_match('/\/$/', $value) && $this->getUrlValidator()->isValid($value, ['http', 'https']);
209  }
210 
216  public function afterSave()
217  {
218  if ($this->isValueChanged()) {
219  switch ($this->getPath()) {
220  case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL:
221  case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL:
222  case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL:
223  case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL:
224  $this->_mergeService->cleanMergedJsCss();
225  break;
226  }
227  }
228  return parent::afterSave();
229  }
230 
237  private function getUrlValidator()
238  {
239  if (!$this->urlValidator) {
240  $this->urlValidator = ObjectManager::getInstance()->get(UrlValidator::class);
241  }
242  return $this->urlValidator;
243  }
244 }
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\View\Asset\MergeService $mergeService, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Baseurl.php:37