Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Request.php
Go to the documentation of this file.
1 <?php
9 
17 
18 class Request extends HttpRequest implements RequestInterface
19 {
23  const REQUEST_PARAM_SERVICES = 'services';
24 
28  const ALL_SERVICES = 'all';
29 
39  public function __construct(
42  AreaList $areaList,
43  ScopeInterface $configScope,
44  $uri = null
45  ) {
46  parent::__construct($cookieReader, $converter, $uri);
47 
48  $pathInfo = $this->getRequestUri();
50  $areaFrontName = $areaList->getFrontName($configScope->getCurrentScope());
51  $pathInfo = preg_replace("#.*?/{$areaFrontName}/?#", '/', $pathInfo);
53  $pathInfo = preg_replace('#\?.*#', '', $pathInfo);
54  $this->setPathInfo($pathInfo);
55  }
56 
62  public function getHeader($header, $default = false)
63  {
64  $headerValue = parent::getHeader($header, $default);
65  if ($headerValue == false) {
67  $header = 'REDIRECT_HTTP_' . strtoupper(str_replace('-', '_', $header));
68  if (isset($_SERVER[$header])) {
69  $headerValue = $_SERVER[$header];
70  }
71  }
72  return $headerValue;
73  }
74 
82  public function getRequestedServices($default = null)
83  {
84  $param = $this->getParam(self::REQUEST_PARAM_SERVICES, $default);
85  return $this->_convertRequestParamToServiceArray($param);
86  }
87 
98  protected function _convertRequestParamToServiceArray($param)
99  {
100  $serviceSeparator = ',';
101  $serviceVerPattern = "[a-zA-Z\d]*V[\d]+";
102  $regexp = "/^({$serviceVerPattern})([{$serviceSeparator}]{$serviceVerPattern})*\$/";
103  if ($param == 'all') {
104  return $param;
105  }
106  //Check if the $param is of valid format
107  if (empty($param) || !preg_match($regexp, $param)) {
108  $message = new Phrase('Incorrect format of request URI or Requested services are missing.');
109  throw new \Magento\Framework\Webapi\Exception($message);
110  }
111  //Split the $param string to create an array of 'service' => 'version'
112  $serviceVersionArray = explode($serviceSeparator, $param);
113  $serviceArray = [];
114  foreach ($serviceVersionArray as $service) {
115  $serviceArray[] = $service;
116  }
117  return $serviceArray;
118  }
119 }
$message
getParam($key, $defaultValue=null)
__construct(CookieReaderInterface $cookieReader, StringUtils $converter, AreaList $areaList, ScopeInterface $configScope, $uri=null)
Definition: Request.php:39
_convertRequestParamToServiceArray($param)
Definition: Request.php:98
getRequestedServices($default=null)
Definition: Request.php:82
getHeader($header, $default=false)
Definition: Request.php:62
setPathInfo($pathInfo=null)
Definition: Http.php:175