functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\MessageInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. use Psr\Http\Message\StreamInterface;
  6. use Psr\Http\Message\UriInterface;
  7. /**
  8. * Returns the string representation of an HTTP message.
  9. *
  10. * @param MessageInterface $message Message to convert to a string.
  11. *
  12. * @return string
  13. *
  14. * @deprecated str will be removed in guzzlehttp/psr7:2.0. Use Message::toString instead.
  15. */
  16. function str(MessageInterface $message)
  17. {
  18. return Message::toString($message);
  19. }
  20. /**
  21. * Returns a UriInterface for the given value.
  22. *
  23. * This function accepts a string or UriInterface and returns a
  24. * UriInterface for the given value. If the value is already a
  25. * UriInterface, it is returned as-is.
  26. *
  27. * @param string|UriInterface $uri
  28. *
  29. * @return UriInterface
  30. *
  31. * @throws \InvalidArgumentException
  32. *
  33. * @deprecated uri_for will be removed in guzzlehttp/psr7:2.0. Use Utils::uriFor instead.
  34. */
  35. function uri_for($uri)
  36. {
  37. return Utils::uriFor($uri);
  38. }
  39. /**
  40. * Create a new stream based on the input type.
  41. *
  42. * Options is an associative array that can contain the following keys:
  43. * - metadata: Array of custom metadata.
  44. * - size: Size of the stream.
  45. *
  46. * This method accepts the following `$resource` types:
  47. * - `Psr\Http\Message\StreamInterface`: Returns the value as-is.
  48. * - `string`: Creates a stream object that uses the given string as the contents.
  49. * - `resource`: Creates a stream object that wraps the given PHP stream resource.
  50. * - `Iterator`: If the provided value implements `Iterator`, then a read-only
  51. * stream object will be created that wraps the given iterable. Each time the
  52. * stream is read from, data from the iterator will fill a buffer and will be
  53. * continuously called until the buffer is equal to the requested read size.
  54. * Subsequent read calls will first read from the buffer and then call `next`
  55. * on the underlying iterator until it is exhausted.
  56. * - `object` with `__toString()`: If the object has the `__toString()` method,
  57. * the object will be cast to a string and then a stream will be returned that
  58. * uses the string value.
  59. * - `NULL`: When `null` is passed, an empty stream object is returned.
  60. * - `callable` When a callable is passed, a read-only stream object will be
  61. * created that invokes the given callable. The callable is invoked with the
  62. * number of suggested bytes to read. The callable can return any number of
  63. * bytes, but MUST return `false` when there is no more data to return. The
  64. * stream object that wraps the callable will invoke the callable until the
  65. * number of requested bytes are available. Any additional bytes will be
  66. * buffered and used in subsequent reads.
  67. *
  68. * @param resource|string|null|int|float|bool|StreamInterface|callable|\Iterator $resource Entity body data
  69. * @param array $options Additional options
  70. *
  71. * @return StreamInterface
  72. *
  73. * @throws \InvalidArgumentException if the $resource arg is not valid.
  74. *
  75. * @deprecated stream_for will be removed in guzzlehttp/psr7:2.0. Use Utils::streamFor instead.
  76. */
  77. function stream_for($resource = '', array $options = [])
  78. {
  79. return Utils::streamFor($resource, $options);
  80. }
  81. /**
  82. * Parse an array of header values containing ";" separated data into an
  83. * array of associative arrays representing the header key value pair data
  84. * of the header. When a parameter does not contain a value, but just
  85. * contains a key, this function will inject a key with a '' string value.
  86. *
  87. * @param string|array $header Header to parse into components.
  88. *
  89. * @return array Returns the parsed header values.
  90. *
  91. * @deprecated parse_header will be removed in guzzlehttp/psr7:2.0. Use Header::parse instead.
  92. */
  93. function parse_header($header)
  94. {
  95. return Header::parse($header);
  96. }
  97. /**
  98. * Converts an array of header values that may contain comma separated
  99. * headers into an array of headers with no comma separated values.
  100. *
  101. * @param string|array $header Header to normalize.
  102. *
  103. * @return array Returns the normalized header field values.
  104. *
  105. * @deprecated normalize_header will be removed in guzzlehttp/psr7:2.0. Use Header::normalize instead.
  106. */
  107. function normalize_header($header)
  108. {
  109. return Header::normalize($header);
  110. }
  111. /**
  112. * Clone and modify a request with the given changes.
  113. *
  114. * This method is useful for reducing the number of clones needed to mutate a
  115. * message.
  116. *
  117. * The changes can be one of:
  118. * - method: (string) Changes the HTTP method.
  119. * - set_headers: (array) Sets the given headers.
  120. * - remove_headers: (array) Remove the given headers.
  121. * - body: (mixed) Sets the given body.
  122. * - uri: (UriInterface) Set the URI.
  123. * - query: (string) Set the query string value of the URI.
  124. * - version: (string) Set the protocol version.
  125. *
  126. * @param RequestInterface $request Request to clone and modify.
  127. * @param array $changes Changes to apply.
  128. *
  129. * @return RequestInterface
  130. *
  131. * @deprecated modify_request will be removed in guzzlehttp/psr7:2.0. Use Utils::modifyRequest instead.
  132. */
  133. function modify_request(RequestInterface $request, array $changes)
  134. {
  135. return Utils::modifyRequest($request, $changes);
  136. }
  137. /**
  138. * Attempts to rewind a message body and throws an exception on failure.
  139. *
  140. * The body of the message will only be rewound if a call to `tell()` returns a
  141. * value other than `0`.
  142. *
  143. * @param MessageInterface $message Message to rewind
  144. *
  145. * @throws \RuntimeException
  146. *
  147. * @deprecated rewind_body will be removed in guzzlehttp/psr7:2.0. Use Message::rewindBody instead.
  148. */
  149. function rewind_body(MessageInterface $message)
  150. {
  151. Message::rewindBody($message);
  152. }
  153. /**
  154. * Safely opens a PHP stream resource using a filename.
  155. *
  156. * When fopen fails, PHP normally raises a warning. This function adds an
  157. * error handler that checks for errors and throws an exception instead.
  158. *
  159. * @param string $filename File to open
  160. * @param string $mode Mode used to open the file
  161. *
  162. * @return resource
  163. *
  164. * @throws \RuntimeException if the file cannot be opened
  165. *
  166. * @deprecated try_fopen will be removed in guzzlehttp/psr7:2.0. Use Utils::tryFopen instead.
  167. */
  168. function try_fopen($filename, $mode)
  169. {
  170. return Utils::tryFopen($filename, $mode);
  171. }
  172. /**
  173. * Copy the contents of a stream into a string until the given number of
  174. * bytes have been read.
  175. *
  176. * @param StreamInterface $stream Stream to read
  177. * @param int $maxLen Maximum number of bytes to read. Pass -1
  178. * to read the entire stream.
  179. * @return string
  180. *
  181. * @throws \RuntimeException on error.
  182. *
  183. * @deprecated copy_to_string will be removed in guzzlehttp/psr7:2.0. Use Utils::copyToString instead.
  184. */
  185. function copy_to_string(StreamInterface $stream, $maxLen = -1)
  186. {
  187. return Utils::copyToString($stream, $maxLen);
  188. }
  189. /**
  190. * Copy the contents of a stream into another stream until the given number
  191. * of bytes have been read.
  192. *
  193. * @param StreamInterface $source Stream to read from
  194. * @param StreamInterface $dest Stream to write to
  195. * @param int $maxLen Maximum number of bytes to read. Pass -1
  196. * to read the entire stream.
  197. *
  198. * @throws \RuntimeException on error.
  199. *
  200. * @deprecated copy_to_stream will be removed in guzzlehttp/psr7:2.0. Use Utils::copyToStream instead.
  201. */
  202. function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)
  203. {
  204. return Utils::copyToStream($source, $dest, $maxLen);
  205. }
  206. /**
  207. * Calculate a hash of a stream.
  208. *
  209. * This method reads the entire stream to calculate a rolling hash, based on
  210. * PHP's `hash_init` functions.
  211. *
  212. * @param StreamInterface $stream Stream to calculate the hash for
  213. * @param string $algo Hash algorithm (e.g. md5, crc32, etc)
  214. * @param bool $rawOutput Whether or not to use raw output
  215. *
  216. * @return string Returns the hash of the stream
  217. *
  218. * @throws \RuntimeException on error.
  219. *
  220. * @deprecated hash will be removed in guzzlehttp/psr7:2.0. Use Utils::hash instead.
  221. */
  222. function hash(StreamInterface $stream, $algo, $rawOutput = false)
  223. {
  224. return Utils::hash($stream, $algo, $rawOutput);
  225. }
  226. /**
  227. * Read a line from the stream up to the maximum allowed buffer length.
  228. *
  229. * @param StreamInterface $stream Stream to read from
  230. * @param int|null $maxLength Maximum buffer length
  231. *
  232. * @return string
  233. *
  234. * @deprecated readline will be removed in guzzlehttp/psr7:2.0. Use Utils::readLine instead.
  235. */
  236. function readline(StreamInterface $stream, $maxLength = null)
  237. {
  238. return Utils::readLine($stream, $maxLength);
  239. }
  240. /**
  241. * Parses a request message string into a request object.
  242. *
  243. * @param string $message Request message string.
  244. *
  245. * @return Request
  246. *
  247. * @deprecated parse_request will be removed in guzzlehttp/psr7:2.0. Use Message::parseRequest instead.
  248. */
  249. function parse_request($message)
  250. {
  251. return Message::parseRequest($message);
  252. }
  253. /**
  254. * Parses a response message string into a response object.
  255. *
  256. * @param string $message Response message string.
  257. *
  258. * @return Response
  259. *
  260. * @deprecated parse_response will be removed in guzzlehttp/psr7:2.0. Use Message::parseResponse instead.
  261. */
  262. function parse_response($message)
  263. {
  264. return Message::parseResponse($message);
  265. }
  266. /**
  267. * Parse a query string into an associative array.
  268. *
  269. * If multiple values are found for the same key, the value of that key value
  270. * pair will become an array. This function does not parse nested PHP style
  271. * arrays into an associative array (e.g., `foo[a]=1&foo[b]=2` will be parsed
  272. * into `['foo[a]' => '1', 'foo[b]' => '2'])`.
  273. *
  274. * @param string $str Query string to parse
  275. * @param int|bool $urlEncoding How the query string is encoded
  276. *
  277. * @return array
  278. *
  279. * @deprecated parse_query will be removed in guzzlehttp/psr7:2.0. Use Query::parse instead.
  280. */
  281. function parse_query($str, $urlEncoding = true)
  282. {
  283. return Query::parse($str, $urlEncoding);
  284. }
  285. /**
  286. * Build a query string from an array of key value pairs.
  287. *
  288. * This function can use the return value of `parse_query()` to build a query
  289. * string. This function does not modify the provided keys when an array is
  290. * encountered (like `http_build_query()` would).
  291. *
  292. * @param array $params Query string parameters.
  293. * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
  294. * to encode using RFC3986, or PHP_QUERY_RFC1738
  295. * to encode using RFC1738.
  296. * @return string
  297. *
  298. * @deprecated build_query will be removed in guzzlehttp/psr7:2.0. Use Query::build instead.
  299. */
  300. function build_query(array $params, $encoding = PHP_QUERY_RFC3986)
  301. {
  302. return Query::build($params, $encoding);
  303. }
  304. /**
  305. * Determines the mimetype of a file by looking at its extension.
  306. *
  307. * @param string $filename
  308. *
  309. * @return string|null
  310. *
  311. * @deprecated mimetype_from_filename will be removed in guzzlehttp/psr7:2.0. Use MimeType::fromFilename instead.
  312. */
  313. function mimetype_from_filename($filename)
  314. {
  315. return MimeType::fromFilename($filename);
  316. }
  317. /**
  318. * Maps a file extensions to a mimetype.
  319. *
  320. * @param $extension string The file extension.
  321. *
  322. * @return string|null
  323. *
  324. * @link http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
  325. * @deprecated mimetype_from_extension will be removed in guzzlehttp/psr7:2.0. Use MimeType::fromExtension instead.
  326. */
  327. function mimetype_from_extension($extension)
  328. {
  329. return MimeType::fromExtension($extension);
  330. }
  331. /**
  332. * Parses an HTTP message into an associative array.
  333. *
  334. * The array contains the "start-line" key containing the start line of
  335. * the message, "headers" key containing an associative array of header
  336. * array values, and a "body" key containing the body of the message.
  337. *
  338. * @param string $message HTTP request or response to parse.
  339. *
  340. * @return array
  341. *
  342. * @internal
  343. * @deprecated _parse_message will be removed in guzzlehttp/psr7:2.0. Use Message::parseMessage instead.
  344. */
  345. function _parse_message($message)
  346. {
  347. return Message::parseMessage($message);
  348. }
  349. /**
  350. * Constructs a URI for an HTTP request message.
  351. *
  352. * @param string $path Path from the start-line
  353. * @param array $headers Array of headers (each value an array).
  354. *
  355. * @return string
  356. *
  357. * @internal
  358. * @deprecated _parse_request_uri will be removed in guzzlehttp/psr7:2.0. Use Message::parseRequestUri instead.
  359. */
  360. function _parse_request_uri($path, array $headers)
  361. {
  362. return Message::parseRequestUri($path, $headers);
  363. }
  364. /**
  365. * Get a short summary of the message body.
  366. *
  367. * Will return `null` if the response is not printable.
  368. *
  369. * @param MessageInterface $message The message to get the body summary
  370. * @param int $truncateAt The maximum allowed size of the summary
  371. *
  372. * @return string|null
  373. *
  374. * @deprecated get_message_body_summary will be removed in guzzlehttp/psr7:2.0. Use Message::bodySummary instead.
  375. */
  376. function get_message_body_summary(MessageInterface $message, $truncateAt = 120)
  377. {
  378. return Message::bodySummary($message, $truncateAt);
  379. }
  380. /**
  381. * Remove the items given by the keys, case insensitively from the data.
  382. *
  383. * @param iterable<string> $keys
  384. *
  385. * @return array
  386. *
  387. * @internal
  388. * @deprecated _caseless_remove will be removed in guzzlehttp/psr7:2.0. Use Utils::caselessRemove instead.
  389. */
  390. function _caseless_remove($keys, array $data)
  391. {
  392. return Utils::caselessRemove($keys, $data);
  393. }