vendor/guzzlehttp/guzzle/src/functions.php
268 lines · 10287 bytes
1<?php
2
3namespace GuzzleHttp;
4
5/**
6 * Debug function used to describe the provided value type and class.
7 *
8 * @param mixed $input Any type of variable to describe the type of. This
9 * parameter misses a typehint because of that.
10 *
11 * @return string Returns a string containing the type of the variable and
12 * if a class is provided, the class name.
13 *
14 * @deprecated describe_type will be removed in guzzlehttp/guzzle:8.0. Use get_debug_type() instead.
15 */
16function describe_type($input): string
17{
18 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use get_debug_type() instead.', __FUNCTION__);
19
20 switch (\gettype($input)) {
21 case 'object':
22 return 'object('.\get_class($input).')';
23 case 'array':
24 return 'array('.\count($input).')';
25 default:
26 \ob_start();
27 \var_dump($input);
28 // normalize float vs double
29 /** @var string $varDumpContent */
30 $varDumpContent = \ob_get_clean();
31
32 return \str_replace('double(', 'float(', \rtrim($varDumpContent, " \n\r\t\0\x0B"));
33 }
34}
35
36/**
37 * Parses an array of header lines into an associative array of headers.
38 *
39 * @param iterable $lines Header lines array of strings in the following
40 * format: "Name: Value"
41 *
42 * @deprecated headers_from_lines will be removed in guzzlehttp/guzzle:8.0. Use Utils::headersFromLines instead.
43 */
44function headers_from_lines(iterable $lines): array
45{
46 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use Utils::headersFromLines() instead.', __FUNCTION__);
47
48 return Utils::headersFromLines($lines);
49}
50
51/**
52 * Returns a debug stream based on the provided variable.
53 *
54 * @param mixed $value Optional value
55 *
56 * @return resource
57 *
58 * @deprecated debug_resource will be removed in guzzlehttp/guzzle:8.0. Use Utils::debugResource instead.
59 */
60function debug_resource($value = null)
61{
62 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use Utils::debugResource() instead.', __FUNCTION__);
63
64 return Utils::debugResource($value);
65}
66
67/**
68 * Chooses and creates a default handler to use based on the environment.
69 *
70 * The returned handler is not wrapped by any default middlewares.
71 *
72 * @return callable(\Psr\Http\Message\RequestInterface, array): Promise\PromiseInterface Returns the best handler for the given system.
73 *
74 * @throws \RuntimeException if no viable Handler is available.
75 *
76 * @deprecated choose_handler will be removed in guzzlehttp/guzzle:8.0. Use Utils::chooseHandler instead.
77 */
78function choose_handler(): callable
79{
80 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use Utils::chooseHandler() instead.', __FUNCTION__);
81
82 return Utils::chooseHandler();
83}
84
85/**
86 * Get the default User-Agent string to use with Guzzle.
87 *
88 * @deprecated default_user_agent will be removed in guzzlehttp/guzzle:8.0. Use Utils::defaultUserAgent instead.
89 */
90function default_user_agent(): string
91{
92 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use Utils::defaultUserAgent() instead.', __FUNCTION__);
93
94 return Utils::defaultUserAgent();
95}
96
97/**
98 * Returns the default cacert bundle for the current system.
99 *
100 * First, the openssl.cafile and curl.cainfo php.ini settings are checked.
101 * If those settings are not configured, then the common locations for
102 * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X
103 * and Windows are checked. If any of these file locations are found on
104 * disk, they will be utilized.
105 *
106 * Note: the result of this function is cached for subsequent calls.
107 *
108 * @throws \RuntimeException if no bundle can be found.
109 *
110 * @deprecated default_ca_bundle will be removed in guzzlehttp/guzzle:8.0. This function is not needed in PHP 5.6+.
111 */
112function default_ca_bundle(): string
113{
114 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. This function is not needed in PHP 5.6+.', __FUNCTION__);
115
116 static $cached = null;
117 static $cafiles = [
118 // Red Hat, CentOS, Fedora (provided by the ca-certificates package)
119 '/etc/pki/tls/certs/ca-bundle.crt',
120 // Ubuntu, Debian (provided by the ca-certificates package)
121 '/etc/ssl/certs/ca-certificates.crt',
122 // FreeBSD (provided by the ca_root_nss package)
123 '/usr/local/share/certs/ca-root-nss.crt',
124 // SLES 12 (provided by the ca-certificates package)
125 '/var/lib/ca-certificates/ca-bundle.pem',
126 // OS X provided by homebrew (using the default path)
127 '/usr/local/etc/openssl/cert.pem',
128 // Google app engine
129 '/etc/ca-certificates.crt',
130 // Windows?
131 'C:\\windows\\system32\\curl-ca-bundle.crt',
132 'C:\\windows\\curl-ca-bundle.crt',
133 ];
134
135 if ($cached) {
136 return $cached;
137 }
138
139 if ($ca = \ini_get('openssl.cafile')) {
140 return $cached = $ca;
141 }
142
143 if ($ca = \ini_get('curl.cainfo')) {
144 return $cached = $ca;
145 }
146
147 foreach ($cafiles as $filename) {
148 if (\file_exists($filename)) {
149 return $cached = $filename;
150 }
151 }
152
153 throw new \RuntimeException(
154 <<< EOT
155No system CA bundle could be found in any of the the common system locations.
156PHP versions earlier than 5.6 are not properly configured to use the system's
157CA bundle by default. In order to verify peer certificates, you will need to
158supply the path on disk to a certificate bundle to the 'verify' request option:
159https://github.com/guzzle/guzzle/blob/7.16/docs/request-options.md#verify. If
160you do not need a specific certificate bundle, then Mozilla provides a commonly
161used CA bundle which can be downloaded here (provided by the maintainer of
162cURL): https://curl.se/ca/cacert.pem. Once you have a CA bundle available on
163disk, you can set the 'openssl.cafile' PHP ini setting to point to the path to
164the file, allowing you to omit the 'verify' request option. See
165https://curl.se/docs/sslcerts.html for more information.
166EOT
167 );
168}
169
170/**
171 * Creates an associative array of lowercase header names to the actual
172 * header casing.
173 *
174 * @deprecated normalize_header_keys will be removed in guzzlehttp/guzzle:8.0. Use Utils::normalizeHeaderKeys instead.
175 */
176function normalize_header_keys(array $headers): array
177{
178 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use Utils::normalizeHeaderKeys() instead.', __FUNCTION__);
179
180 return Utils::normalizeHeaderKeys($headers);
181}
182
183/**
184 * Returns true if the provided host matches any of the no proxy areas.
185 *
186 * This method will strip a port from the host if it is present. Each pattern
187 * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a
188 * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" ==
189 * "baz.foo.com", but ".foo.com" != "foo.com").
190 *
191 * Areas are matched in the following cases:
192 * 1. "*" (without quotes) always matches any hosts.
193 * 2. An exact match.
194 * 3. The area starts with "." and the area is the last part of the host. e.g.
195 * '.mit.edu' will match any host that ends with '.mit.edu'.
196 *
197 * @param string $host Host to check against the patterns.
198 * @param string[] $noProxyArray An array of host patterns.
199 *
200 * @throws Exception\InvalidArgumentException
201 *
202 * @deprecated is_host_in_noproxy will be removed in guzzlehttp/guzzle:8.0. Use Utils::isHostInNoProxy instead.
203 */
204function is_host_in_noproxy(string $host, array $noProxyArray): bool
205{
206 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use Utils::isHostInNoProxy() instead.', __FUNCTION__);
207
208 return Utils::isHostInNoProxy($host, $noProxyArray);
209}
210
211/**
212 * Wrapper for json_decode that throws when an error occurs.
213 *
214 * @param string $json JSON data to parse
215 * @param bool $assoc When true, returned objects will be converted
216 * into associative arrays.
217 * @param int $depth User specified recursion depth.
218 * @param int $options Bitmask of JSON decode options.
219 *
220 * @return object|array|string|int|float|bool|null
221 *
222 * @throws Exception\InvalidArgumentException if the JSON cannot be decoded.
223 *
224 * @see https://www.php.net/manual/en/function.json-decode.php
225 * @deprecated json_decode will be removed in guzzlehttp/guzzle:8.0. Use PHP's json_decode() instead.
226 */
227function json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
228{
229 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use PHP\'s json_decode() instead.', __FUNCTION__);
230
231 if ($depth < 1) {
232 throw new Exception\InvalidArgumentException('json_decode error: Maximum stack depth exceeded');
233 }
234
235 $data = \json_decode($json, $assoc, $depth, $options);
236 if (\JSON_ERROR_NONE !== \json_last_error()) {
237 throw new Exception\InvalidArgumentException('json_decode error: '.\json_last_error_msg());
238 }
239
240 /** @var object|array|string|int|float|bool|null $data */
241 return $data;
242}
243
244/**
245 * Wrapper for JSON encoding that throws when an error occurs.
246 *
247 * @param mixed $value The value being encoded
248 * @param int $options JSON encode option bitmask
249 * @param int $depth Set the maximum depth. Must be greater than zero.
250 *
251 * @throws Exception\InvalidArgumentException if the JSON cannot be encoded.
252 *
253 * @see https://www.php.net/manual/en/function.json-encode.php
254 * @deprecated json_encode will be removed in guzzlehttp/guzzle:8.0. Use PHP's json_encode() instead.
255 */
256function json_encode($value, int $options = 0, int $depth = 512): string
257{
258 \trigger_deprecation('guzzlehttp/guzzle', '7.1', '%s() is deprecated and will be removed in 8.0. Use PHP\'s json_encode() instead.', __FUNCTION__);
259
260 /** @var positive-int $depth */
261 $json = \json_encode($value, $options, $depth);
262 if (\JSON_ERROR_NONE !== \json_last_error()) {
263 throw new Exception\InvalidArgumentException('json_encode error: '.\json_last_error_msg());
264 }
265
266 /** @var non-empty-string $json */
267 return $json;
268}