Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FrontNameResolver.php
Go to the documentation of this file.
1 <?php
9 
15 
20 class FrontNameResolver implements \Magento\Framework\App\Area\FrontNameResolverInterface
21 {
22  const XML_PATH_USE_CUSTOM_ADMIN_PATH = 'admin/url/use_custom_path';
23 
24  const XML_PATH_CUSTOM_ADMIN_PATH = 'admin/url/custom_path';
25 
26  const XML_PATH_USE_CUSTOM_ADMIN_URL = 'admin/url/use_custom';
27 
28  const XML_PATH_CUSTOM_ADMIN_URL = 'admin/url/custom';
29 
33  const AREA_CODE = 'adminhtml';
34 
38  protected $standardPorts = ['http' => '80', 'https' => '443'];
39 
43  protected $defaultFrontName;
44 
48  protected $config;
49 
55  protected $deploymentConfig;
56 
60  private $scopeConfig;
61 
67  public function __construct(
68  \Magento\Backend\App\Config $config,
70  ScopeConfigInterface $scopeConfig
71  ) {
72  $this->config = $config;
74  $this->scopeConfig = $scopeConfig;
75  }
76 
83  public function getFrontName($checkHost = false)
84  {
85  if ($checkHost && !$this->isHostBackend()) {
86  return false;
87  }
88  $isCustomPathUsed = (bool)(string)$this->config->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_PATH);
89  if ($isCustomPathUsed) {
90  return (string)$this->config->getValue(self::XML_PATH_CUSTOM_ADMIN_PATH);
91  }
93  }
94 
100  public function isHostBackend()
101  {
102  if ($this->scopeConfig->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE)) {
103  $backendUrl = $this->scopeConfig->getValue(self::XML_PATH_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE);
104  } else {
105  $backendUrl = $this->scopeConfig->getValue(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE);
106  }
107  $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
108  return stripos($this->getHostWithPort($backendUrl), $host) !== false;
109  }
110 
117  private function getHostWithPort($url)
118  {
119  $scheme = parse_url(trim($url), PHP_URL_SCHEME);
120  $host = parse_url(trim($url), PHP_URL_HOST);
121  $port = parse_url(trim($url), PHP_URL_PORT);
122  if (!$port) {
123  $port = isset($this->standardPorts[$scheme]) ? $this->standardPorts[$scheme] : null;
124  }
125  return isset($port) ? $host . ':' . $port : $host;
126  }
127 }
const XML_PATH_UNSECURE_BASE_URL
Definition: Store.php:66
__construct(\Magento\Backend\App\Config $config, DeploymentConfig $deploymentConfig, ScopeConfigInterface $scopeConfig)