GuzzleHttpPsr7ServerRequest

Server-side HTTP request.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Psr7/ServerRequest.php  
  1. class ServerRequest extends Request implements ServerRequestInterface 
  2. /** 
  3. * @var array 
  4. */ 
  5. private $attributes = []; 
  6.  
  7. /** 
  8. * @var array 
  9. */ 
  10. private $cookieParams = []; 
  11.  
  12. /** 
  13. * @var null|array|object 
  14. */ 
  15. private $parsedBody; 
  16.  
  17. /** 
  18. * @var array 
  19. */ 
  20. private $queryParams = []; 
  21.  
  22. /** 
  23. * @var array 
  24. */ 
  25. private $serverParams; 
  26.  
  27. /** 
  28. * @var array 
  29. */ 
  30. private $uploadedFiles = []; 
  31.  
  32. /** 
  33. * @param string $method HTTP method 
  34. * @param string|UriInterface $uri URI 
  35. * @param array $headers Request headers 
  36. * @param string|null|resource|StreamInterface $body Request body 
  37. * @param string $version Protocol version 
  38. * @param array $serverParams Typically the $_SERVER superglobal 
  39. */ 
  40. public function __construct( 
  41. $method,  
  42. $uri,  
  43. array $headers = [],  
  44. $body = null,  
  45. $version = '1.1',  
  46. array $serverParams = [] 
  47. ) { 
  48. $this->serverParams = $serverParams; 
  49.  
  50. parent::__construct($method, $uri, $headers, $body, $version); 
  51.  
  52. /** 
  53. * Return an UploadedFile instance array. 
  54. * @param array $files A array which respect $_FILES structure 
  55. * @throws InvalidArgumentException for unrecognized values 
  56. * @return array 
  57. */ 
  58. public static function normalizeFiles(array $files) 
  59. $normalized = []; 
  60.  
  61. foreach ($files as $key => $value) { 
  62. if ($value instanceof UploadedFileInterface) { 
  63. $normalized[$key] = $value; 
  64. } elseif (is_array($value) && isset($value['tmp_name'])) { 
  65. $normalized[$key] = self::createUploadedFileFromSpec($value); 
  66. } elseif (is_array($value)) { 
  67. $normalized[$key] = self::normalizeFiles($value); 
  68. continue; 
  69. } else { 
  70. throw new InvalidArgumentException('Invalid value in files specification'); 
  71.  
  72. return $normalized; 
  73.  
  74. /** 
  75. * Create and return an UploadedFile instance from a $_FILES specification. 
  76. * If the specification represents an array of values, this method will 
  77. * delegate to normalizeNestedFileSpec() and return that return value. 
  78. * @param array $value $_FILES struct 
  79. * @return array|UploadedFileInterface 
  80. */ 
  81. private static function createUploadedFileFromSpec(array $value) 
  82. if (is_array($value['tmp_name'])) { 
  83. return self::normalizeNestedFileSpec($value); 
  84.  
  85. return new UploadedFile( 
  86. $value['tmp_name'],  
  87. (int) $value['size'],  
  88. (int) $value['error'],  
  89. $value['name'],  
  90. $value['type'] 
  91. ); 
  92.  
  93. /** 
  94. * Normalize an array of file specifications. 
  95. * Loops through all nested files and returns a normalized array of 
  96. * UploadedFileInterface instances. 
  97. * @param array $files 
  98. * @return UploadedFileInterface[] 
  99. */ 
  100. private static function normalizeNestedFileSpec(array $files = []) 
  101. $normalizedFiles = []; 
  102.  
  103. foreach (array_keys($files['tmp_name']) as $key) { 
  104. $spec = [ 
  105. 'tmp_name' => $files['tmp_name'][$key],  
  106. 'size' => $files['size'][$key],  
  107. 'error' => $files['error'][$key],  
  108. 'name' => $files['name'][$key],  
  109. 'type' => $files['type'][$key],  
  110. ]; 
  111. $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec); 
  112.  
  113. return $normalizedFiles; 
  114.  
  115. /** 
  116. * Return a ServerRequest populated with superglobals: 
  117. * $_GET 
  118. * $_POST 
  119. * $_COOKIE 
  120. * $_FILES 
  121. * $_SERVER 
  122. * @return ServerRequestInterface 
  123. */ 
  124. public static function fromGlobals() 
  125. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; 
  126. $headers = function_exists('getallheaders') ? getallheaders() : []; 
  127. $uri = self::getUriFromGlobals(); 
  128. $body = new LazyOpenStream('php://input', 'r+'); 
  129. $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1'; 
  130.  
  131. $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER); 
  132.  
  133. return $serverRequest 
  134. ->withCookieParams($_COOKIE) 
  135. ->withQueryParams($_GET) 
  136. ->withParsedBody($_POST) 
  137. ->withUploadedFiles(self::normalizeFiles($_FILES)); 
  138.  
  139. /** 
  140. * Get a Uri populated with values from $_SERVER. 
  141. * @return UriInterface 
  142. */ 
  143. public static function getUriFromGlobals() { 
  144. $uri = new Uri(''); 
  145.  
  146. if (isset($_SERVER['HTTPS'])) { 
  147. $uri = $uri->withScheme($_SERVER['HTTPS'] == 'on' ? 'https' : 'http'); 
  148.  
  149. if (isset($_SERVER['HTTP_HOST'])) { 
  150. $uri = $uri->withHost($_SERVER['HTTP_HOST']); 
  151. } elseif (isset($_SERVER['SERVER_NAME'])) { 
  152. $uri = $uri->withHost($_SERVER['SERVER_NAME']); 
  153.  
  154. if (isset($_SERVER['SERVER_PORT'])) { 
  155. $uri = $uri->withPort($_SERVER['SERVER_PORT']); 
  156.  
  157. if (isset($_SERVER['REQUEST_URI'])) { 
  158. $uri = $uri->withPath(current(explode('?', $_SERVER['REQUEST_URI']))); 
  159.  
  160. if (isset($_SERVER['QUERY_STRING'])) { 
  161. $uri = $uri->withQuery($_SERVER['QUERY_STRING']); 
  162.  
  163. return $uri; 
  164.  
  165.  
  166. /** 
  167. * {@inheritdoc} 
  168. */ 
  169. public function getServerParams() 
  170. return $this->serverParams; 
  171.  
  172. /** 
  173. * {@inheritdoc} 
  174. */ 
  175. public function getUploadedFiles() 
  176. return $this->uploadedFiles; 
  177.  
  178. /** 
  179. * {@inheritdoc} 
  180. */ 
  181. public function withUploadedFiles(array $uploadedFiles) 
  182. $new = clone $this; 
  183. $new->uploadedFiles = $uploadedFiles; 
  184.  
  185. return $new; 
  186.  
  187. /** 
  188. * {@inheritdoc} 
  189. */ 
  190. public function getCookieParams() 
  191. return $this->cookieParams; 
  192.  
  193. /** 
  194. * {@inheritdoc} 
  195. */ 
  196. public function withCookieParams(array $cookies) 
  197. $new = clone $this; 
  198. $new->cookieParams = $cookies; 
  199.  
  200. return $new; 
  201.  
  202. /** 
  203. * {@inheritdoc} 
  204. */ 
  205. public function getQueryParams() 
  206. return $this->queryParams; 
  207.  
  208. /** 
  209. * {@inheritdoc} 
  210. */ 
  211. public function withQueryParams(array $query) 
  212. $new = clone $this; 
  213. $new->queryParams = $query; 
  214.  
  215. return $new; 
  216.  
  217. /** 
  218. * {@inheritdoc} 
  219. */ 
  220. public function getParsedBody() 
  221. return $this->parsedBody; 
  222.  
  223. /** 
  224. * {@inheritdoc} 
  225. */ 
  226. public function withParsedBody($data) 
  227. $new = clone $this; 
  228. $new->parsedBody = $data; 
  229.  
  230. return $new; 
  231.  
  232. /** 
  233. * {@inheritdoc} 
  234. */ 
  235. public function getAttributes() 
  236. return $this->attributes; 
  237.  
  238. /** 
  239. * {@inheritdoc} 
  240. */ 
  241. public function getAttribute($attribute, $default = null) 
  242. if (false === array_key_exists($attribute, $this->attributes)) { 
  243. return $default; 
  244.  
  245. return $this->attributes[$attribute]; 
  246.  
  247. /** 
  248. * {@inheritdoc} 
  249. */ 
  250. public function withAttribute($attribute, $value) 
  251. $new = clone $this; 
  252. $new->attributes[$attribute] = $value; 
  253.  
  254. return $new; 
  255.  
  256. /** 
  257. * {@inheritdoc} 
  258. */ 
  259. public function withoutAttribute($attribute) 
  260. if (false === array_key_exists($attribute, $this->attributes)) { 
  261. return $this; 
  262.  
  263. $new = clone $this; 
  264. unset($new->attributes[$attribute]); 
  265.  
  266. return $new;