| @@ -1,70 +1,34 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp; |
| 4 | 4 | |
| 5 | -use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\CurlHandler; | |
| 6 | -use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\CurlMultiHandler; | |
| 7 | -use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\Proxy; | |
| 8 | -use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\StreamHandler; | |
| 9 | 5 | /** |
| 10 | - * Expands a URI template | |
| 11 | - * | |
| 12 | - * @param string $template URI template | |
| 13 | - * @param array $variables Template variables | |
| 14 | - * | |
| 15 | - * @return string | |
| 16 | - */ | |
| 17 | -function uri_template($template, array $variables) | |
| 18 | -{ | |
| 19 | - if (\extension_loaded('uri_template')) { | |
| 20 | - // @codeCoverageIgnoreStart | |
| 21 | - return \Dudlewebs\WPMCS\s3\uri_template($template, $variables); | |
| 22 | - // @codeCoverageIgnoreEnd | |
| 23 | - } | |
| 24 | - static $uriTemplate; | |
| 25 | - if (!$uriTemplate) { | |
| 26 | - $uriTemplate = new UriTemplate(); | |
| 27 | - } | |
| 28 | - return $uriTemplate->expand($template, $variables); | |
| 29 | -} | |
| 30 | -/** | |
| 31 | 6 | * Debug function used to describe the provided value type and class. |
| 32 | 7 | * |
| 33 | - * @param mixed $input | |
| 8 | + * @param mixed $input Any type of variable to describe the type of. This | |
| 9 | + * parameter misses a typehint because of that. | |
| 34 | 10 | * |
| 35 | 11 | * @return string Returns a string containing the type of the variable and |
| 36 | 12 | * if a class is provided, the class name. |
| 13 | + * | |
| 14 | + * @deprecated describe_type will be removed in guzzlehttp/guzzle:8.0. Use Utils::describeType instead. | |
| 37 | 15 | */ |
| 38 | -function describe_type($input) | |
| 16 | +function describe_type($input) : string | |
| 39 | 17 | { |
| 40 | - switch (\gettype($input)) { | |
| 41 | - case 'object': | |
| 42 | - return 'object(' . \get_class($input) . ')'; | |
| 43 | - case 'array': | |
| 44 | - return 'array(' . \count($input) . ')'; | |
| 45 | - default: | |
| 46 | - \ob_start(); | |
| 47 | - \var_dump($input); | |
| 48 | - // normalize float vs double | |
| 49 | - return \str_replace('double(', 'float(', \rtrim(\ob_get_clean())); | |
| 50 | - } | |
| 18 | + return Utils::describeType($input); | |
| 51 | 19 | } |
| 52 | 20 | /** |
| 53 | 21 | * Parses an array of header lines into an associative array of headers. |
| 54 | 22 | * |
| 55 | 23 | * @param iterable $lines Header lines array of strings in the following |
| 56 | - * format: "Name: Value" | |
| 57 | - * @return array | |
| 24 | + * format: "Name: Value" | |
| 25 | + * | |
| 26 | + * @deprecated headers_from_lines will be removed in guzzlehttp/guzzle:8.0. Use Utils::headersFromLines instead. | |
| 58 | 27 | */ |
| 59 | -function headers_from_lines($lines) | |
| 28 | +function headers_from_lines(iterable $lines) : array | |
| 60 | 29 | { |
| 61 | - $headers = []; | |
| 62 | - foreach ($lines as $line) { | |
| 63 | - $parts = \explode(':', $line, 2); | |
| 64 | - $headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null; | |
| 65 | - } | |
| 66 | - return $headers; | |
| 30 | + return Utils::headersFromLines($lines); | |
| 67 | 31 | } |
| 68 | 32 | /** |
| 69 | 33 | * Returns a debug stream based on the provided variable. |
| 70 | 34 | * |
| @@ -70,17 +34,14 @@ | ||
| 70 | 34 | * |
| 71 | 35 | * @param mixed $value Optional value |
| 72 | 36 | * |
| 73 | 37 | * @return resource |
| 38 | + * | |
| 39 | + * @deprecated debug_resource will be removed in guzzlehttp/guzzle:8.0. Use Utils::debugResource instead. | |
| 74 | 40 | */ |
| 75 | 41 | function debug_resource($value = null) |
| 76 | 42 | { |
| 77 | - if (\is_resource($value)) { | |
| 78 | - return $value; | |
| 79 | - } elseif (\defined('STDOUT')) { | |
| 80 | - return \STDOUT; | |
| 81 | - } | |
| 82 | - return \fopen('php://output', 'w'); | |
| 43 | + return Utils::debugResource($value); | |
| 83 | 44 | } |
| 84 | 45 | /** |
| 85 | 46 | * Chooses and creates a default handler to use based on the environment. |
| 86 | 47 | * |
| @@ -85,44 +46,26 @@ | ||
| 85 | 46 | * Chooses and creates a default handler to use based on the environment. |
| 86 | 47 | * |
| 87 | 48 | * The returned handler is not wrapped by any default middlewares. |
| 88 | 49 | * |
| 89 | - * @return callable Returns the best handler for the given system. | |
| 50 | + * @return callable(\Psr\Http\Message\RequestInterface, array): Promise\PromiseInterface Returns the best handler for the given system. | |
| 51 | + * | |
| 90 | 52 | * @throws \RuntimeException if no viable Handler is available. |
| 53 | + * | |
| 54 | + * @deprecated choose_handler will be removed in guzzlehttp/guzzle:8.0. Use Utils::chooseHandler instead. | |
| 91 | 55 | */ |
| 92 | -function choose_handler() | |
| 56 | +function choose_handler() : callable | |
| 93 | 57 | { |
| 94 | - $handler = null; | |
| 95 | - if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) { | |
| 96 | - $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); | |
| 97 | - } elseif (\function_exists('curl_exec')) { | |
| 98 | - $handler = new CurlHandler(); | |
| 99 | - } elseif (\function_exists('curl_multi_exec')) { | |
| 100 | - $handler = new CurlMultiHandler(); | |
| 101 | - } | |
| 102 | - if (\ini_get('allow_url_fopen')) { | |
| 103 | - $handler = $handler ? Proxy::wrapStreaming($handler, new StreamHandler()) : new StreamHandler(); | |
| 104 | - } elseif (!$handler) { | |
| 105 | - throw new \RuntimeException('GuzzleHttp requires cURL, the ' . 'allow_url_fopen ini setting, or a custom HTTP handler.'); | |
| 106 | - } | |
| 107 | - return $handler; | |
| 58 | + return Utils::chooseHandler(); | |
| 108 | 59 | } |
| 109 | 60 | /** |
| 110 | - * Get the default User-Agent string to use with Guzzle | |
| 61 | + * Get the default User-Agent string to use with Guzzle. | |
| 111 | 62 | * |
| 112 | - * @return string | |
| 63 | + * @deprecated default_user_agent will be removed in guzzlehttp/guzzle:8.0. Use Utils::defaultUserAgent instead. | |
| 113 | 64 | */ |
| 114 | -function default_user_agent() | |
| 65 | +function default_user_agent() : string | |
| 115 | 66 | { |
| 116 | - static $defaultAgent = ''; | |
| 117 | - if (!$defaultAgent) { | |
| 118 | - $defaultAgent = 'GuzzleHttp/' . Client::VERSION; | |
| 119 | - if (\extension_loaded('curl') && \function_exists('curl_version')) { | |
| 120 | - $defaultAgent .= ' curl/' . \curl_version()['version']; | |
| 121 | - } | |
| 122 | - $defaultAgent .= ' PHP/' . \PHP_VERSION; | |
| 123 | - } | |
| 124 | - return $defaultAgent; | |
| 67 | + return Utils::defaultUserAgent(); | |
| 125 | 68 | } |
| 126 | 69 | /** |
| 127 | 70 | * Returns the default cacert bundle for the current system. |
| 128 | 71 | * |
| @@ -133,76 +76,25 @@ | ||
| 133 | 76 | * disk, they will be utilized. |
| 134 | 77 | * |
| 135 | 78 | * Note: the result of this function is cached for subsequent calls. |
| 136 | 79 | * |
| 137 | - * @return string | |
| 138 | 80 | * @throws \RuntimeException if no bundle can be found. |
| 81 | + * | |
| 82 | + * @deprecated default_ca_bundle will be removed in guzzlehttp/guzzle:8.0. This function is not needed in PHP 5.6+. | |
| 139 | 83 | */ |
| 140 | -function default_ca_bundle() | |
| 84 | +function default_ca_bundle() : string | |
| 141 | 85 | { |
| 142 | - static $cached = null; | |
| 143 | - static $cafiles = [ | |
| 144 | - // Red Hat, CentOS, Fedora (provided by the ca-certificates package) | |
| 145 | - '/etc/pki/tls/certs/ca-bundle.crt', | |
| 146 | - // Ubuntu, Debian (provided by the ca-certificates package) | |
| 147 | - '/etc/ssl/certs/ca-certificates.crt', | |
| 148 | - // FreeBSD (provided by the ca_root_nss package) | |
| 149 | - '/usr/local/share/certs/ca-root-nss.crt', | |
| 150 | - // SLES 12 (provided by the ca-certificates package) | |
| 151 | - '/var/lib/ca-certificates/ca-bundle.pem', | |
| 152 | - // OS X provided by homebrew (using the default path) | |
| 153 | - '/usr/local/etc/openssl/cert.pem', | |
| 154 | - // Google app engine | |
| 155 | - '/etc/ca-certificates.crt', | |
| 156 | - // Windows? | |
| 157 | - 'C:\\windows\\system32\\curl-ca-bundle.crt', | |
| 158 | - 'C:\\windows\\curl-ca-bundle.crt', | |
| 159 | - ]; | |
| 160 | - if ($cached) { | |
| 161 | - return $cached; | |
| 162 | - } | |
| 163 | - if ($ca = \ini_get('openssl.cafile')) { | |
| 164 | - return $cached = $ca; | |
| 165 | - } | |
| 166 | - if ($ca = \ini_get('curl.cainfo')) { | |
| 167 | - return $cached = $ca; | |
| 168 | - } | |
| 169 | - foreach ($cafiles as $filename) { | |
| 170 | - if (\file_exists($filename)) { | |
| 171 | - return $cached = $filename; | |
| 172 | - } | |
| 173 | - } | |
| 174 | - throw new \RuntimeException(<<<EOT | |
| 175 | -No system CA bundle could be found in any of the the common system locations. | |
| 176 | -PHP versions earlier than 5.6 are not properly configured to use the system's | |
| 177 | -CA bundle by default. In order to verify peer certificates, you will need to | |
| 178 | -supply the path on disk to a certificate bundle to the 'verify' request | |
| 179 | -option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not | |
| 180 | -need a specific certificate bundle, then Mozilla provides a commonly used CA | |
| 181 | -bundle which can be downloaded here (provided by the maintainer of cURL): | |
| 182 | -https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once | |
| 183 | -you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP | |
| 184 | -ini setting to point to the path to the file, allowing you to omit the 'verify' | |
| 185 | -request option. See http://curl.haxx.se/docs/sslcerts.html for more | |
| 186 | -information. | |
| 187 | -EOT | |
| 188 | -); | |
| 86 | + return Utils::defaultCaBundle(); | |
| 189 | 87 | } |
| 190 | 88 | /** |
| 191 | 89 | * Creates an associative array of lowercase header names to the actual |
| 192 | 90 | * header casing. |
| 193 | 91 | * |
| 194 | - * @param array $headers | |
| 195 | - * | |
| 196 | - * @return array | |
| 92 | + * @deprecated normalize_header_keys will be removed in guzzlehttp/guzzle:8.0. Use Utils::normalizeHeaderKeys instead. | |
| 197 | 93 | */ |
| 198 | -function normalize_header_keys(array $headers) | |
| 94 | +function normalize_header_keys(array $headers) : array | |
| 199 | 95 | { |
| 200 | - $result = []; | |
| 201 | - foreach (\array_keys($headers) as $key) { | |
| 202 | - $result[\strtolower($key)] = $key; | |
| 203 | - } | |
| 204 | - return $result; | |
| 96 | + return Utils::normalizeHeaderKeys($headers); | |
| 205 | 97 | } |
| 206 | 98 | /** |
| 207 | 99 | * Returns true if the provided host matches any of the no proxy areas. |
| 208 | 100 | * |
| @@ -216,79 +108,51 @@ | ||
| 216 | 108 | * 2. An exact match. |
| 217 | 109 | * 3. The area starts with "." and the area is the last part of the host. e.g. |
| 218 | 110 | * '.mit.edu' will match any host that ends with '.mit.edu'. |
| 219 | 111 | * |
| 220 | - * @param string $host Host to check against the patterns. | |
| 221 | - * @param array $noProxyArray An array of host patterns. | |
| 112 | + * @param string $host Host to check against the patterns. | |
| 113 | + * @param string[] $noProxyArray An array of host patterns. | |
| 222 | 114 | * |
| 223 | - * @return bool | |
| 115 | + * @throws Exception\InvalidArgumentException | |
| 116 | + * | |
| 117 | + * @deprecated is_host_in_noproxy will be removed in guzzlehttp/guzzle:8.0. Use Utils::isHostInNoProxy instead. | |
| 224 | 118 | */ |
| 225 | -function is_host_in_noproxy($host, array $noProxyArray) | |
| 119 | +function is_host_in_noproxy(string $host, array $noProxyArray) : bool | |
| 226 | 120 | { |
| 227 | - if (\strlen($host) === 0) { | |
| 228 | - throw new \InvalidArgumentException('Empty host provided'); | |
| 229 | - } | |
| 230 | - // Strip port if present. | |
| 231 | - if (\strpos($host, ':')) { | |
| 232 | - $host = \explode($host, ':', 2)[0]; | |
| 233 | - } | |
| 234 | - foreach ($noProxyArray as $area) { | |
| 235 | - // Always match on wildcards. | |
| 236 | - if ($area === '*') { | |
| 237 | - return \true; | |
| 238 | - } elseif (empty($area)) { | |
| 239 | - // Don't match on empty values. | |
| 240 | - continue; | |
| 241 | - } elseif ($area === $host) { | |
| 242 | - // Exact matches. | |
| 243 | - return \true; | |
| 244 | - } else { | |
| 245 | - // Special match if the area when prefixed with ".". Remove any | |
| 246 | - // existing leading "." and add a new leading ".". | |
| 247 | - $area = '.' . \ltrim($area, '.'); | |
| 248 | - if (\substr($host, -\strlen($area)) === $area) { | |
| 249 | - return \true; | |
| 250 | - } | |
| 251 | - } | |
| 252 | - } | |
| 253 | - return \false; | |
| 121 | + return Utils::isHostInNoProxy($host, $noProxyArray); | |
| 254 | 122 | } |
| 255 | 123 | /** |
| 256 | 124 | * Wrapper for json_decode that throws when an error occurs. |
| 257 | 125 | * |
| 258 | 126 | * @param string $json JSON data to parse |
| 259 | - * @param bool $assoc When true, returned objects will be converted | |
| 127 | + * @param bool $assoc When true, returned objects will be converted | |
| 260 | 128 | * into associative arrays. |
| 261 | 129 | * @param int $depth User specified recursion depth. |
| 262 | 130 | * @param int $options Bitmask of JSON decode options. |
| 263 | 131 | * |
| 264 | - * @return mixed | |
| 132 | + * @return object|array|string|int|float|bool|null | |
| 133 | + * | |
| 265 | 134 | * @throws Exception\InvalidArgumentException if the JSON cannot be decoded. |
| 266 | - * @link http://www.php.net/manual/en/function.json-decode.php | |
| 135 | + * | |
| 136 | + * @see https://www.php.net/manual/en/function.json-decode.php | |
| 137 | + * @deprecated json_decode will be removed in guzzlehttp/guzzle:8.0. Use Utils::jsonDecode instead. | |
| 267 | 138 | */ |
| 268 | -function json_decode($json, $assoc = \false, $depth = 512, $options = 0) | |
| 139 | +function json_decode(string $json, bool $assoc = \false, int $depth = 512, int $options = 0) | |
| 269 | 140 | { |
| 270 | - $data = \json_decode($json, $assoc, $depth, $options); | |
| 271 | - if (\JSON_ERROR_NONE !== \json_last_error()) { | |
| 272 | - throw new Exception\InvalidArgumentException('json_decode error: ' . \json_last_error_msg()); | |
| 273 | - } | |
| 274 | - return $data; | |
| 141 | + return Utils::jsonDecode($json, $assoc, $depth, $options); | |
| 275 | 142 | } |
| 276 | 143 | /** |
| 277 | 144 | * Wrapper for JSON encoding that throws when an error occurs. |
| 278 | 145 | * |
| 279 | 146 | * @param mixed $value The value being encoded |
| 280 | - * @param int $options JSON encode option bitmask | |
| 281 | - * @param int $depth Set the maximum depth. Must be greater than zero. | |
| 147 | + * @param int $options JSON encode option bitmask | |
| 148 | + * @param int $depth Set the maximum depth. Must be greater than zero. | |
| 282 | 149 | * |
| 283 | - * @return string | |
| 284 | 150 | * @throws Exception\InvalidArgumentException if the JSON cannot be encoded. |
| 285 | - * @link http://www.php.net/manual/en/function.json-encode.php | |
| 151 | + * | |
| 152 | + * @see https://www.php.net/manual/en/function.json-encode.php | |
| 153 | + * @deprecated json_encode will be removed in guzzlehttp/guzzle:8.0. Use Utils::jsonEncode instead. | |
| 286 | 154 | */ |
| 287 | -function json_encode($value, $options = 0, $depth = 512) | |
| 155 | +function json_encode($value, int $options = 0, int $depth = 512) : string | |
| 288 | 156 | { |
| 289 | - $json = \json_encode($value, $options, $depth); | |
| 290 | - if (\JSON_ERROR_NONE !== \json_last_error()) { | |
| 291 | - throw new Exception\InvalidArgumentException('json_encode error: ' . \json_last_error_msg()); | |
| 292 | - } | |
| 293 | - return $json; | |
| 157 | + return Utils::jsonEncode($value, $options, $depth); | |
| 294 | 158 | } |