Request.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Web API request.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Webapi;
  9. use Magento\Framework\App\AreaList;
  10. use Magento\Framework\App\RequestInterface;
  11. use Magento\Framework\Config\ScopeInterface;
  12. use Magento\Framework\HTTP\PhpEnvironment\Request as HttpRequest;
  13. use Magento\Framework\Stdlib\Cookie\CookieReaderInterface;
  14. use Magento\Framework\Phrase;
  15. use Magento\Framework\Stdlib\StringUtils;
  16. class Request extends HttpRequest implements RequestInterface
  17. {
  18. /**
  19. * Name of query parameter to specify services for which to generate schema
  20. */
  21. const REQUEST_PARAM_SERVICES = 'services';
  22. /**
  23. * services parameter value to indicate that a schema for all services should be generated
  24. */
  25. const ALL_SERVICES = 'all';
  26. /**
  27. * Modify pathInfo: strip down the front name and query parameters.
  28. *
  29. * @param CookieReaderInterface $cookieReader
  30. * @param StringUtils $converter
  31. * @param AreaList $areaList
  32. * @param ScopeInterface $configScope
  33. * @param null|string $uri
  34. */
  35. public function __construct(
  36. CookieReaderInterface $cookieReader,
  37. StringUtils $converter,
  38. AreaList $areaList,
  39. ScopeInterface $configScope,
  40. $uri = null
  41. ) {
  42. parent::__construct($cookieReader, $converter, $uri);
  43. $pathInfo = $this->getRequestUri();
  44. /** Remove base url and area from path */
  45. $areaFrontName = $areaList->getFrontName($configScope->getCurrentScope());
  46. $pathInfo = preg_replace("#.*?/{$areaFrontName}/?#", '/', $pathInfo);
  47. /** Remove GET parameters from path */
  48. $pathInfo = preg_replace('#\?.*#', '', $pathInfo);
  49. $this->setPathInfo($pathInfo);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. *
  54. * Added CGI environment support.
  55. */
  56. public function getHeader($header, $default = false)
  57. {
  58. $headerValue = parent::getHeader($header, $default);
  59. if ($headerValue == false) {
  60. /** Workaround for hhvm environment */
  61. $header = 'REDIRECT_HTTP_' . strtoupper(str_replace('-', '_', $header));
  62. if (isset($_SERVER[$header])) {
  63. $headerValue = $_SERVER[$header];
  64. }
  65. }
  66. return $headerValue;
  67. }
  68. /**
  69. * Identify versions of resources that should be used for API configuration generation.
  70. *
  71. * @param string|null $default
  72. * @return array|string
  73. * @throws \Magento\Framework\Webapi\Exception When GET parameters are invalid
  74. */
  75. public function getRequestedServices($default = null)
  76. {
  77. $param = $this->getParam(self::REQUEST_PARAM_SERVICES, $default);
  78. return $this->_convertRequestParamToServiceArray($param);
  79. }
  80. /**
  81. * Extract the resources query param value and return associative array of the form 'resource' => 'version'
  82. *
  83. * @param string $param eg <pre> testModule1AllSoapAndRestV1,testModule2AllSoapNoRestV1 </pre>
  84. * @return string|array <pre> eg array (
  85. * 'testModule1AllSoapAndRestV1',
  86. * 'testModule2AllSoapNoRestV1',
  87. * )</pre>
  88. * @throws \Magento\Framework\Webapi\Exception
  89. */
  90. protected function _convertRequestParamToServiceArray($param)
  91. {
  92. $serviceSeparator = ',';
  93. $serviceVerPattern = "[a-zA-Z\d]*V[\d]+";
  94. $regexp = "/^({$serviceVerPattern})([{$serviceSeparator}]{$serviceVerPattern})*\$/";
  95. if ($param == 'all') {
  96. return $param;
  97. }
  98. //Check if the $param is of valid format
  99. if (empty($param) || !preg_match($regexp, $param)) {
  100. $message = new Phrase('Incorrect format of request URI or Requested services are missing.');
  101. throw new \Magento\Framework\Webapi\Exception($message);
  102. }
  103. //Split the $param string to create an array of 'service' => 'version'
  104. $serviceVersionArray = explode($serviceSeparator, $param);
  105. $serviceArray = [];
  106. foreach ($serviceVersionArray as $service) {
  107. $serviceArray[] = $service;
  108. }
  109. return $serviceArray;
  110. }
  111. }