Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PathInfo.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 class PathInfo
14 {
22  public function getPathInfo(string $requestUri, string $baseUrl) : string
23  {
24  if ($requestUri === '/') {
25  return '';
26  }
27 
28  $requestUri = $this->removeRepeatedSlashes($requestUri);
29  $parsedRequestUri = explode('?', $requestUri, 2);
30  $pathInfo = (string)substr(current($parsedRequestUri), (int)strlen($baseUrl));
31 
32  if ($this->isNoRouteUri($baseUrl, $pathInfo)) {
34  }
35  return $pathInfo;
36  }
37 
44  public function getQueryString(string $requestUri) : string
45  {
46  $requestUri = $this->removeRepeatedSlashes($requestUri);
47  $parsedRequestUri = explode('?', $requestUri, 2);
48  $queryString = !isset($parsedRequestUri[1]) ? '' : '?' . $parsedRequestUri[1];
49  return $queryString;
50  }
51 
58  private function removeRepeatedSlashes($pathInfo) : string
59  {
60  $firstChar = (string)substr($pathInfo, 0, 1);
61  if ($firstChar == '/') {
62  $pathInfo = '/' . ltrim($pathInfo, '/');
63  }
64 
65  return $pathInfo;
66  }
67 
75  private function isNoRouteUri($baseUrl, $pathInfo) : bool
76  {
77  $firstChar = (string)substr($pathInfo, 0, 1);
78  return $baseUrl !== '' && !in_array($firstChar, ['/', '']);
79  }
80 }
getQueryString(string $requestUri)
Definition: PathInfo.php:44
getPathInfo(string $requestUri, string $baseUrl)
Definition: PathInfo.php:22