@@ -9,15 +9,15 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface DiscoveryStrategy |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Find a resource of a specific type. |
|
| 14 | - * |
|
| 15 | - * @param string $type |
|
| 16 | - * |
|
| 17 | - * @return array The return value is always an array with zero or more elements. Each |
|
| 18 | - * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
| 19 | - * |
|
| 20 | - * @throws StrategyUnavailableException if we cannot use this strategy |
|
| 21 | - */ |
|
| 22 | - public static function getCandidates($type); |
|
| 12 | + /** |
|
| 13 | + * Find a resource of a specific type. |
|
| 14 | + * |
|
| 15 | + * @param string $type |
|
| 16 | + * |
|
| 17 | + * @return array The return value is always an array with zero or more elements. Each |
|
| 18 | + * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
| 19 | + * |
|
| 20 | + * @throws StrategyUnavailableException if we cannot use this strategy |
|
| 21 | + */ |
|
| 22 | + public static function getCandidates($type); |
|
| 23 | 23 | } |
@@ -4,5 +4,5 @@ |
||
| 4 | 4 | |
| 5 | 5 | // Don't redefine the functions if included multiple times. |
| 6 | 6 | if (!\function_exists('OCA\\FullTextSearch_Elasticsearch\\Vendor\\GuzzleHttp\\describe_type')) { |
| 7 | - require __DIR__ . '/functions.php'; |
|
| 7 | + require __DIR__ . '/functions.php'; |
|
| 8 | 8 | } |
@@ -18,145 +18,145 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class RedirectMiddleware |
| 20 | 20 | { |
| 21 | - public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
|
| 22 | - public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
|
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - public static $defaultSettings = ['max' => 5, 'protocols' => ['http', 'https'], 'strict' => \false, 'referer' => \false, 'track_redirects' => \false]; |
|
| 27 | - /** |
|
| 28 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 29 | - */ |
|
| 30 | - private $nextHandler; |
|
| 31 | - /** |
|
| 32 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 33 | - */ |
|
| 34 | - public function __construct(callable $nextHandler) |
|
| 35 | - { |
|
| 36 | - $this->nextHandler = $nextHandler; |
|
| 37 | - } |
|
| 38 | - public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
| 39 | - { |
|
| 40 | - $fn = $this->nextHandler; |
|
| 41 | - if (empty($options['allow_redirects'])) { |
|
| 42 | - return $fn($request, $options); |
|
| 43 | - } |
|
| 44 | - if ($options['allow_redirects'] === \true) { |
|
| 45 | - $options['allow_redirects'] = self::$defaultSettings; |
|
| 46 | - } elseif (!\is_array($options['allow_redirects'])) { |
|
| 47 | - throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); |
|
| 48 | - } else { |
|
| 49 | - // Merge the default settings with the provided settings |
|
| 50 | - $options['allow_redirects'] += self::$defaultSettings; |
|
| 51 | - } |
|
| 52 | - if (empty($options['allow_redirects']['max'])) { |
|
| 53 | - return $fn($request, $options); |
|
| 54 | - } |
|
| 55 | - return $fn($request, $options)->then(function (ResponseInterface $response) use($request, $options) { |
|
| 56 | - return $this->checkRedirect($request, $options, $response); |
|
| 57 | - }); |
|
| 58 | - } |
|
| 59 | - /** |
|
| 60 | - * @return ResponseInterface|PromiseInterface |
|
| 61 | - */ |
|
| 62 | - public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
|
| 63 | - { |
|
| 64 | - if (\strpos((string) $response->getStatusCode(), '3') !== 0 || !$response->hasHeader('Location')) { |
|
| 65 | - return $response; |
|
| 66 | - } |
|
| 67 | - $this->guardMax($request, $response, $options); |
|
| 68 | - $nextRequest = $this->modifyRequest($request, $options, $response); |
|
| 69 | - // If authorization is handled by curl, unset it if URI is cross-origin. |
|
| 70 | - if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $nextRequest->getUri()) && \defined('\\CURLOPT_HTTPAUTH')) { |
|
| 71 | - unset($options['curl'][\CURLOPT_HTTPAUTH], $options['curl'][\CURLOPT_USERPWD]); |
|
| 72 | - } |
|
| 73 | - if (isset($options['allow_redirects']['on_redirect'])) { |
|
| 74 | - $options['allow_redirects']['on_redirect']($request, $response, $nextRequest->getUri()); |
|
| 75 | - } |
|
| 76 | - $promise = $this($nextRequest, $options); |
|
| 77 | - // Add headers to be able to track history of redirects. |
|
| 78 | - if (!empty($options['allow_redirects']['track_redirects'])) { |
|
| 79 | - return $this->withTracking($promise, (string) $nextRequest->getUri(), $response->getStatusCode()); |
|
| 80 | - } |
|
| 81 | - return $promise; |
|
| 82 | - } |
|
| 83 | - /** |
|
| 84 | - * Enable tracking on promise. |
|
| 85 | - */ |
|
| 86 | - private function withTracking(PromiseInterface $promise, string $uri, int $statusCode) : PromiseInterface |
|
| 87 | - { |
|
| 88 | - return $promise->then(static function (ResponseInterface $response) use($uri, $statusCode) { |
|
| 89 | - // Note that we are pushing to the front of the list as this |
|
| 90 | - // would be an earlier response than what is currently present |
|
| 91 | - // in the history header. |
|
| 92 | - $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
|
| 93 | - $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
|
| 94 | - \array_unshift($historyHeader, $uri); |
|
| 95 | - \array_unshift($statusHeader, (string) $statusCode); |
|
| 96 | - return $response->withHeader(self::HISTORY_HEADER, $historyHeader)->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
|
| 97 | - }); |
|
| 98 | - } |
|
| 99 | - /** |
|
| 100 | - * Check for too many redirects. |
|
| 101 | - * |
|
| 102 | - * @throws TooManyRedirectsException Too many redirects. |
|
| 103 | - */ |
|
| 104 | - private function guardMax(RequestInterface $request, ResponseInterface $response, array &$options) : void |
|
| 105 | - { |
|
| 106 | - $current = $options['__redirect_count'] ?? 0; |
|
| 107 | - $options['__redirect_count'] = $current + 1; |
|
| 108 | - $max = $options['allow_redirects']['max']; |
|
| 109 | - if ($options['__redirect_count'] > $max) { |
|
| 110 | - throw new TooManyRedirectsException("Will not follow more than {$max} redirects", $request, $response); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response) : RequestInterface |
|
| 114 | - { |
|
| 115 | - // Request modifications to apply. |
|
| 116 | - $modify = []; |
|
| 117 | - $protocols = $options['allow_redirects']['protocols']; |
|
| 118 | - // Use a GET request if this is an entity enclosing request and we are |
|
| 119 | - // not forcing RFC compliance, but rather emulating what all browsers |
|
| 120 | - // would do. |
|
| 121 | - $statusCode = $response->getStatusCode(); |
|
| 122 | - if ($statusCode == 303 || $statusCode <= 302 && !$options['allow_redirects']['strict']) { |
|
| 123 | - $safeMethods = ['GET', 'HEAD', 'OPTIONS']; |
|
| 124 | - $requestMethod = $request->getMethod(); |
|
| 125 | - $modify['method'] = \in_array($requestMethod, $safeMethods) ? $requestMethod : 'GET'; |
|
| 126 | - $modify['body'] = ''; |
|
| 127 | - } |
|
| 128 | - $uri = self::redirectUri($request, $response, $protocols); |
|
| 129 | - if (isset($options['idn_conversion']) && $options['idn_conversion'] !== \false) { |
|
| 130 | - $idnOptions = $options['idn_conversion'] === \true ? \IDNA_DEFAULT : $options['idn_conversion']; |
|
| 131 | - $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 132 | - } |
|
| 133 | - $modify['uri'] = $uri; |
|
| 134 | - Psr7\Message::rewindBody($request); |
|
| 135 | - // Add the Referer header if it is told to do so and only |
|
| 136 | - // add the header if we are not redirecting from https to http. |
|
| 137 | - if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) { |
|
| 138 | - $uri = $request->getUri()->withUserInfo(''); |
|
| 139 | - $modify['set_headers']['Referer'] = (string) $uri; |
|
| 140 | - } else { |
|
| 141 | - $modify['remove_headers'][] = 'Referer'; |
|
| 142 | - } |
|
| 143 | - // Remove Authorization and Cookie headers if URI is cross-origin. |
|
| 144 | - if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $modify['uri'])) { |
|
| 145 | - $modify['remove_headers'][] = 'Authorization'; |
|
| 146 | - $modify['remove_headers'][] = 'Cookie'; |
|
| 147 | - } |
|
| 148 | - return Psr7\Utils::modifyRequest($request, $modify); |
|
| 149 | - } |
|
| 150 | - /** |
|
| 151 | - * Set the appropriate URL on the request based on the location header. |
|
| 152 | - */ |
|
| 153 | - private static function redirectUri(RequestInterface $request, ResponseInterface $response, array $protocols) : UriInterface |
|
| 154 | - { |
|
| 155 | - $location = Psr7\UriResolver::resolve($request->getUri(), new Psr7\Uri($response->getHeaderLine('Location'))); |
|
| 156 | - // Ensure that the redirect URI is allowed based on the protocols. |
|
| 157 | - if (!\in_array($location->getScheme(), $protocols)) { |
|
| 158 | - throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols)), $request, $response); |
|
| 159 | - } |
|
| 160 | - return $location; |
|
| 161 | - } |
|
| 21 | + public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
|
| 22 | + public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + public static $defaultSettings = ['max' => 5, 'protocols' => ['http', 'https'], 'strict' => \false, 'referer' => \false, 'track_redirects' => \false]; |
|
| 27 | + /** |
|
| 28 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 29 | + */ |
|
| 30 | + private $nextHandler; |
|
| 31 | + /** |
|
| 32 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 33 | + */ |
|
| 34 | + public function __construct(callable $nextHandler) |
|
| 35 | + { |
|
| 36 | + $this->nextHandler = $nextHandler; |
|
| 37 | + } |
|
| 38 | + public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
| 39 | + { |
|
| 40 | + $fn = $this->nextHandler; |
|
| 41 | + if (empty($options['allow_redirects'])) { |
|
| 42 | + return $fn($request, $options); |
|
| 43 | + } |
|
| 44 | + if ($options['allow_redirects'] === \true) { |
|
| 45 | + $options['allow_redirects'] = self::$defaultSettings; |
|
| 46 | + } elseif (!\is_array($options['allow_redirects'])) { |
|
| 47 | + throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); |
|
| 48 | + } else { |
|
| 49 | + // Merge the default settings with the provided settings |
|
| 50 | + $options['allow_redirects'] += self::$defaultSettings; |
|
| 51 | + } |
|
| 52 | + if (empty($options['allow_redirects']['max'])) { |
|
| 53 | + return $fn($request, $options); |
|
| 54 | + } |
|
| 55 | + return $fn($request, $options)->then(function (ResponseInterface $response) use($request, $options) { |
|
| 56 | + return $this->checkRedirect($request, $options, $response); |
|
| 57 | + }); |
|
| 58 | + } |
|
| 59 | + /** |
|
| 60 | + * @return ResponseInterface|PromiseInterface |
|
| 61 | + */ |
|
| 62 | + public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
|
| 63 | + { |
|
| 64 | + if (\strpos((string) $response->getStatusCode(), '3') !== 0 || !$response->hasHeader('Location')) { |
|
| 65 | + return $response; |
|
| 66 | + } |
|
| 67 | + $this->guardMax($request, $response, $options); |
|
| 68 | + $nextRequest = $this->modifyRequest($request, $options, $response); |
|
| 69 | + // If authorization is handled by curl, unset it if URI is cross-origin. |
|
| 70 | + if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $nextRequest->getUri()) && \defined('\\CURLOPT_HTTPAUTH')) { |
|
| 71 | + unset($options['curl'][\CURLOPT_HTTPAUTH], $options['curl'][\CURLOPT_USERPWD]); |
|
| 72 | + } |
|
| 73 | + if (isset($options['allow_redirects']['on_redirect'])) { |
|
| 74 | + $options['allow_redirects']['on_redirect']($request, $response, $nextRequest->getUri()); |
|
| 75 | + } |
|
| 76 | + $promise = $this($nextRequest, $options); |
|
| 77 | + // Add headers to be able to track history of redirects. |
|
| 78 | + if (!empty($options['allow_redirects']['track_redirects'])) { |
|
| 79 | + return $this->withTracking($promise, (string) $nextRequest->getUri(), $response->getStatusCode()); |
|
| 80 | + } |
|
| 81 | + return $promise; |
|
| 82 | + } |
|
| 83 | + /** |
|
| 84 | + * Enable tracking on promise. |
|
| 85 | + */ |
|
| 86 | + private function withTracking(PromiseInterface $promise, string $uri, int $statusCode) : PromiseInterface |
|
| 87 | + { |
|
| 88 | + return $promise->then(static function (ResponseInterface $response) use($uri, $statusCode) { |
|
| 89 | + // Note that we are pushing to the front of the list as this |
|
| 90 | + // would be an earlier response than what is currently present |
|
| 91 | + // in the history header. |
|
| 92 | + $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
|
| 93 | + $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
|
| 94 | + \array_unshift($historyHeader, $uri); |
|
| 95 | + \array_unshift($statusHeader, (string) $statusCode); |
|
| 96 | + return $response->withHeader(self::HISTORY_HEADER, $historyHeader)->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
|
| 97 | + }); |
|
| 98 | + } |
|
| 99 | + /** |
|
| 100 | + * Check for too many redirects. |
|
| 101 | + * |
|
| 102 | + * @throws TooManyRedirectsException Too many redirects. |
|
| 103 | + */ |
|
| 104 | + private function guardMax(RequestInterface $request, ResponseInterface $response, array &$options) : void |
|
| 105 | + { |
|
| 106 | + $current = $options['__redirect_count'] ?? 0; |
|
| 107 | + $options['__redirect_count'] = $current + 1; |
|
| 108 | + $max = $options['allow_redirects']['max']; |
|
| 109 | + if ($options['__redirect_count'] > $max) { |
|
| 110 | + throw new TooManyRedirectsException("Will not follow more than {$max} redirects", $request, $response); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response) : RequestInterface |
|
| 114 | + { |
|
| 115 | + // Request modifications to apply. |
|
| 116 | + $modify = []; |
|
| 117 | + $protocols = $options['allow_redirects']['protocols']; |
|
| 118 | + // Use a GET request if this is an entity enclosing request and we are |
|
| 119 | + // not forcing RFC compliance, but rather emulating what all browsers |
|
| 120 | + // would do. |
|
| 121 | + $statusCode = $response->getStatusCode(); |
|
| 122 | + if ($statusCode == 303 || $statusCode <= 302 && !$options['allow_redirects']['strict']) { |
|
| 123 | + $safeMethods = ['GET', 'HEAD', 'OPTIONS']; |
|
| 124 | + $requestMethod = $request->getMethod(); |
|
| 125 | + $modify['method'] = \in_array($requestMethod, $safeMethods) ? $requestMethod : 'GET'; |
|
| 126 | + $modify['body'] = ''; |
|
| 127 | + } |
|
| 128 | + $uri = self::redirectUri($request, $response, $protocols); |
|
| 129 | + if (isset($options['idn_conversion']) && $options['idn_conversion'] !== \false) { |
|
| 130 | + $idnOptions = $options['idn_conversion'] === \true ? \IDNA_DEFAULT : $options['idn_conversion']; |
|
| 131 | + $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 132 | + } |
|
| 133 | + $modify['uri'] = $uri; |
|
| 134 | + Psr7\Message::rewindBody($request); |
|
| 135 | + // Add the Referer header if it is told to do so and only |
|
| 136 | + // add the header if we are not redirecting from https to http. |
|
| 137 | + if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) { |
|
| 138 | + $uri = $request->getUri()->withUserInfo(''); |
|
| 139 | + $modify['set_headers']['Referer'] = (string) $uri; |
|
| 140 | + } else { |
|
| 141 | + $modify['remove_headers'][] = 'Referer'; |
|
| 142 | + } |
|
| 143 | + // Remove Authorization and Cookie headers if URI is cross-origin. |
|
| 144 | + if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $modify['uri'])) { |
|
| 145 | + $modify['remove_headers'][] = 'Authorization'; |
|
| 146 | + $modify['remove_headers'][] = 'Cookie'; |
|
| 147 | + } |
|
| 148 | + return Psr7\Utils::modifyRequest($request, $modify); |
|
| 149 | + } |
|
| 150 | + /** |
|
| 151 | + * Set the appropriate URL on the request based on the location header. |
|
| 152 | + */ |
|
| 153 | + private static function redirectUri(RequestInterface $request, ResponseInterface $response, array $protocols) : UriInterface |
|
| 154 | + { |
|
| 155 | + $location = Psr7\UriResolver::resolve($request->getUri(), new Psr7\Uri($response->getHeaderLine('Location'))); |
|
| 156 | + // Ensure that the redirect URI is allowed based on the protocols. |
|
| 157 | + if (!\in_array($location->getScheme(), $protocols)) { |
|
| 158 | + throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols)), $request, $response); |
|
| 159 | + } |
|
| 160 | + return $location; |
|
| 161 | + } |
|
| 162 | 162 | } |
@@ -5,8 +5,8 @@ |
||
| 5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
| 6 | 6 | interface BodySummarizerInterface |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Returns a summarized message body. |
|
| 10 | - */ |
|
| 11 | - public function summarize(MessageInterface $message) : ?string; |
|
| 8 | + /** |
|
| 9 | + * Returns a summarized message body. |
|
| 10 | + */ |
|
| 11 | + public function summarize(MessageInterface $message) : ?string; |
|
| 12 | 12 | } |
@@ -5,113 +5,113 @@ |
||
| 5 | 5 | |
| 6 | 6 | final class Header |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Parse an array of header values containing ";" separated data into an |
|
| 10 | - * array of associative arrays representing the header key value pair data |
|
| 11 | - * of the header. When a parameter does not contain a value, but just |
|
| 12 | - * contains a key, this function will inject a key with a '' string value. |
|
| 13 | - * |
|
| 14 | - * @param string|array $header Header to parse into components. |
|
| 15 | - */ |
|
| 16 | - public static function parse($header) : array |
|
| 17 | - { |
|
| 18 | - static $trimmed = "\"' \n\t\r"; |
|
| 19 | - $params = $matches = []; |
|
| 20 | - foreach ((array) $header as $value) { |
|
| 21 | - foreach (self::splitList($value) as $val) { |
|
| 22 | - $part = []; |
|
| 23 | - foreach (\preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) ?: [] as $kvp) { |
|
| 24 | - if (\preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) { |
|
| 25 | - $m = $matches[0]; |
|
| 26 | - if (isset($m[1])) { |
|
| 27 | - $part[\trim($m[0], $trimmed)] = \trim($m[1], $trimmed); |
|
| 28 | - } else { |
|
| 29 | - $part[] = \trim($m[0], $trimmed); |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - } |
|
| 33 | - if ($part) { |
|
| 34 | - $params[] = $part; |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - return $params; |
|
| 39 | - } |
|
| 40 | - /** |
|
| 41 | - * Converts an array of header values that may contain comma separated |
|
| 42 | - * headers into an array of headers with no comma separated values. |
|
| 43 | - * |
|
| 44 | - * @param string|array $header Header to normalize. |
|
| 45 | - * |
|
| 46 | - * @deprecated Use self::splitList() instead. |
|
| 47 | - */ |
|
| 48 | - public static function normalize($header) : array |
|
| 49 | - { |
|
| 50 | - $result = []; |
|
| 51 | - foreach ((array) $header as $value) { |
|
| 52 | - foreach (self::splitList($value) as $parsed) { |
|
| 53 | - $result[] = $parsed; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - return $result; |
|
| 57 | - } |
|
| 58 | - /** |
|
| 59 | - * Splits a HTTP header defined to contain a comma-separated list into |
|
| 60 | - * each individual value. Empty values will be removed. |
|
| 61 | - * |
|
| 62 | - * Example headers include 'accept', 'cache-control' and 'if-none-match'. |
|
| 63 | - * |
|
| 64 | - * This method must not be used to parse headers that are not defined as |
|
| 65 | - * a list, such as 'user-agent' or 'set-cookie'. |
|
| 66 | - * |
|
| 67 | - * @param string|string[] $values Header value as returned by MessageInterface::getHeader() |
|
| 68 | - * |
|
| 69 | - * @return string[] |
|
| 70 | - */ |
|
| 71 | - public static function splitList($values) : array |
|
| 72 | - { |
|
| 73 | - if (!\is_array($values)) { |
|
| 74 | - $values = [$values]; |
|
| 75 | - } |
|
| 76 | - $result = []; |
|
| 77 | - foreach ($values as $value) { |
|
| 78 | - if (!\is_string($value)) { |
|
| 79 | - throw new \TypeError('$header must either be a string or an array containing strings.'); |
|
| 80 | - } |
|
| 81 | - $v = ''; |
|
| 82 | - $isQuoted = \false; |
|
| 83 | - $isEscaped = \false; |
|
| 84 | - for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
|
| 85 | - if ($isEscaped) { |
|
| 86 | - $v .= $value[$i]; |
|
| 87 | - $isEscaped = \false; |
|
| 88 | - continue; |
|
| 89 | - } |
|
| 90 | - if (!$isQuoted && $value[$i] === ',') { |
|
| 91 | - $v = \trim($v); |
|
| 92 | - if ($v !== '') { |
|
| 93 | - $result[] = $v; |
|
| 94 | - } |
|
| 95 | - $v = ''; |
|
| 96 | - continue; |
|
| 97 | - } |
|
| 98 | - if ($isQuoted && $value[$i] === '\\') { |
|
| 99 | - $isEscaped = \true; |
|
| 100 | - $v .= $value[$i]; |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - if ($value[$i] === '"') { |
|
| 104 | - $isQuoted = !$isQuoted; |
|
| 105 | - $v .= $value[$i]; |
|
| 106 | - continue; |
|
| 107 | - } |
|
| 108 | - $v .= $value[$i]; |
|
| 109 | - } |
|
| 110 | - $v = \trim($v); |
|
| 111 | - if ($v !== '') { |
|
| 112 | - $result[] = $v; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - return $result; |
|
| 116 | - } |
|
| 8 | + /** |
|
| 9 | + * Parse an array of header values containing ";" separated data into an |
|
| 10 | + * array of associative arrays representing the header key value pair data |
|
| 11 | + * of the header. When a parameter does not contain a value, but just |
|
| 12 | + * contains a key, this function will inject a key with a '' string value. |
|
| 13 | + * |
|
| 14 | + * @param string|array $header Header to parse into components. |
|
| 15 | + */ |
|
| 16 | + public static function parse($header) : array |
|
| 17 | + { |
|
| 18 | + static $trimmed = "\"' \n\t\r"; |
|
| 19 | + $params = $matches = []; |
|
| 20 | + foreach ((array) $header as $value) { |
|
| 21 | + foreach (self::splitList($value) as $val) { |
|
| 22 | + $part = []; |
|
| 23 | + foreach (\preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) ?: [] as $kvp) { |
|
| 24 | + if (\preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) { |
|
| 25 | + $m = $matches[0]; |
|
| 26 | + if (isset($m[1])) { |
|
| 27 | + $part[\trim($m[0], $trimmed)] = \trim($m[1], $trimmed); |
|
| 28 | + } else { |
|
| 29 | + $part[] = \trim($m[0], $trimmed); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | + if ($part) { |
|
| 34 | + $params[] = $part; |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + return $params; |
|
| 39 | + } |
|
| 40 | + /** |
|
| 41 | + * Converts an array of header values that may contain comma separated |
|
| 42 | + * headers into an array of headers with no comma separated values. |
|
| 43 | + * |
|
| 44 | + * @param string|array $header Header to normalize. |
|
| 45 | + * |
|
| 46 | + * @deprecated Use self::splitList() instead. |
|
| 47 | + */ |
|
| 48 | + public static function normalize($header) : array |
|
| 49 | + { |
|
| 50 | + $result = []; |
|
| 51 | + foreach ((array) $header as $value) { |
|
| 52 | + foreach (self::splitList($value) as $parsed) { |
|
| 53 | + $result[] = $parsed; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + return $result; |
|
| 57 | + } |
|
| 58 | + /** |
|
| 59 | + * Splits a HTTP header defined to contain a comma-separated list into |
|
| 60 | + * each individual value. Empty values will be removed. |
|
| 61 | + * |
|
| 62 | + * Example headers include 'accept', 'cache-control' and 'if-none-match'. |
|
| 63 | + * |
|
| 64 | + * This method must not be used to parse headers that are not defined as |
|
| 65 | + * a list, such as 'user-agent' or 'set-cookie'. |
|
| 66 | + * |
|
| 67 | + * @param string|string[] $values Header value as returned by MessageInterface::getHeader() |
|
| 68 | + * |
|
| 69 | + * @return string[] |
|
| 70 | + */ |
|
| 71 | + public static function splitList($values) : array |
|
| 72 | + { |
|
| 73 | + if (!\is_array($values)) { |
|
| 74 | + $values = [$values]; |
|
| 75 | + } |
|
| 76 | + $result = []; |
|
| 77 | + foreach ($values as $value) { |
|
| 78 | + if (!\is_string($value)) { |
|
| 79 | + throw new \TypeError('$header must either be a string or an array containing strings.'); |
|
| 80 | + } |
|
| 81 | + $v = ''; |
|
| 82 | + $isQuoted = \false; |
|
| 83 | + $isEscaped = \false; |
|
| 84 | + for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
|
| 85 | + if ($isEscaped) { |
|
| 86 | + $v .= $value[$i]; |
|
| 87 | + $isEscaped = \false; |
|
| 88 | + continue; |
|
| 89 | + } |
|
| 90 | + if (!$isQuoted && $value[$i] === ',') { |
|
| 91 | + $v = \trim($v); |
|
| 92 | + if ($v !== '') { |
|
| 93 | + $result[] = $v; |
|
| 94 | + } |
|
| 95 | + $v = ''; |
|
| 96 | + continue; |
|
| 97 | + } |
|
| 98 | + if ($isQuoted && $value[$i] === '\\') { |
|
| 99 | + $isEscaped = \true; |
|
| 100 | + $v .= $value[$i]; |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + if ($value[$i] === '"') { |
|
| 104 | + $isQuoted = !$isQuoted; |
|
| 105 | + $v .= $value[$i]; |
|
| 106 | + continue; |
|
| 107 | + } |
|
| 108 | + $v .= $value[$i]; |
|
| 109 | + } |
|
| 110 | + $v = \trim($v); |
|
| 111 | + if ($v !== '') { |
|
| 112 | + $result[] = $v; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + return $result; |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -16,134 +16,134 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class PumpStream implements StreamInterface |
| 18 | 18 | { |
| 19 | - /** @var callable(int): (string|false|null)|null */ |
|
| 20 | - private $source; |
|
| 21 | - /** @var int|null */ |
|
| 22 | - private $size; |
|
| 23 | - /** @var int */ |
|
| 24 | - private $tellPos = 0; |
|
| 25 | - /** @var array */ |
|
| 26 | - private $metadata; |
|
| 27 | - /** @var BufferStream */ |
|
| 28 | - private $buffer; |
|
| 29 | - /** |
|
| 30 | - * @param callable(int): (string|false|null) $source Source of the stream data. The callable MAY |
|
| 31 | - * accept an integer argument used to control the |
|
| 32 | - * amount of data to return. The callable MUST |
|
| 33 | - * return a string when called, or false|null on error |
|
| 34 | - * or EOF. |
|
| 35 | - * @param array{size?: int, metadata?: array} $options Stream options: |
|
| 36 | - * - metadata: Hash of metadata to use with stream. |
|
| 37 | - * - size: Size of the stream, if known. |
|
| 38 | - */ |
|
| 39 | - public function __construct(callable $source, array $options = []) |
|
| 40 | - { |
|
| 41 | - $this->source = $source; |
|
| 42 | - $this->size = $options['size'] ?? null; |
|
| 43 | - $this->metadata = $options['metadata'] ?? []; |
|
| 44 | - $this->buffer = new BufferStream(); |
|
| 45 | - } |
|
| 46 | - public function __toString() : string |
|
| 47 | - { |
|
| 48 | - try { |
|
| 49 | - return Utils::copyToString($this); |
|
| 50 | - } catch (\Throwable $e) { |
|
| 51 | - if (\PHP_VERSION_ID >= 70400) { |
|
| 52 | - throw $e; |
|
| 53 | - } |
|
| 54 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 55 | - return ''; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - public function close() : void |
|
| 59 | - { |
|
| 60 | - $this->detach(); |
|
| 61 | - } |
|
| 62 | - public function detach() |
|
| 63 | - { |
|
| 64 | - $this->tellPos = 0; |
|
| 65 | - $this->source = null; |
|
| 66 | - return null; |
|
| 67 | - } |
|
| 68 | - public function getSize() : ?int |
|
| 69 | - { |
|
| 70 | - return $this->size; |
|
| 71 | - } |
|
| 72 | - public function tell() : int |
|
| 73 | - { |
|
| 74 | - return $this->tellPos; |
|
| 75 | - } |
|
| 76 | - public function eof() : bool |
|
| 77 | - { |
|
| 78 | - return $this->source === null; |
|
| 79 | - } |
|
| 80 | - public function isSeekable() : bool |
|
| 81 | - { |
|
| 82 | - return \false; |
|
| 83 | - } |
|
| 84 | - public function rewind() : void |
|
| 85 | - { |
|
| 86 | - $this->seek(0); |
|
| 87 | - } |
|
| 88 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
| 89 | - { |
|
| 90 | - throw new \RuntimeException('Cannot seek a PumpStream'); |
|
| 91 | - } |
|
| 92 | - public function isWritable() : bool |
|
| 93 | - { |
|
| 94 | - return \false; |
|
| 95 | - } |
|
| 96 | - public function write($string) : int |
|
| 97 | - { |
|
| 98 | - throw new \RuntimeException('Cannot write to a PumpStream'); |
|
| 99 | - } |
|
| 100 | - public function isReadable() : bool |
|
| 101 | - { |
|
| 102 | - return \true; |
|
| 103 | - } |
|
| 104 | - public function read($length) : string |
|
| 105 | - { |
|
| 106 | - $data = $this->buffer->read($length); |
|
| 107 | - $readLen = \strlen($data); |
|
| 108 | - $this->tellPos += $readLen; |
|
| 109 | - $remaining = $length - $readLen; |
|
| 110 | - if ($remaining) { |
|
| 111 | - $this->pump($remaining); |
|
| 112 | - $data .= $this->buffer->read($remaining); |
|
| 113 | - $this->tellPos += \strlen($data) - $readLen; |
|
| 114 | - } |
|
| 115 | - return $data; |
|
| 116 | - } |
|
| 117 | - public function getContents() : string |
|
| 118 | - { |
|
| 119 | - $result = ''; |
|
| 120 | - while (!$this->eof()) { |
|
| 121 | - $result .= $this->read(1000000); |
|
| 122 | - } |
|
| 123 | - return $result; |
|
| 124 | - } |
|
| 125 | - /** |
|
| 126 | - * @return mixed |
|
| 127 | - */ |
|
| 128 | - public function getMetadata($key = null) |
|
| 129 | - { |
|
| 130 | - if (!$key) { |
|
| 131 | - return $this->metadata; |
|
| 132 | - } |
|
| 133 | - return $this->metadata[$key] ?? null; |
|
| 134 | - } |
|
| 135 | - private function pump(int $length) : void |
|
| 136 | - { |
|
| 137 | - if ($this->source !== null) { |
|
| 138 | - do { |
|
| 139 | - $data = ($this->source)($length); |
|
| 140 | - if ($data === \false || $data === null) { |
|
| 141 | - $this->source = null; |
|
| 142 | - return; |
|
| 143 | - } |
|
| 144 | - $this->buffer->write($data); |
|
| 145 | - $length -= \strlen($data); |
|
| 146 | - } while ($length > 0); |
|
| 147 | - } |
|
| 148 | - } |
|
| 19 | + /** @var callable(int): (string|false|null)|null */ |
|
| 20 | + private $source; |
|
| 21 | + /** @var int|null */ |
|
| 22 | + private $size; |
|
| 23 | + /** @var int */ |
|
| 24 | + private $tellPos = 0; |
|
| 25 | + /** @var array */ |
|
| 26 | + private $metadata; |
|
| 27 | + /** @var BufferStream */ |
|
| 28 | + private $buffer; |
|
| 29 | + /** |
|
| 30 | + * @param callable(int): (string|false|null) $source Source of the stream data. The callable MAY |
|
| 31 | + * accept an integer argument used to control the |
|
| 32 | + * amount of data to return. The callable MUST |
|
| 33 | + * return a string when called, or false|null on error |
|
| 34 | + * or EOF. |
|
| 35 | + * @param array{size?: int, metadata?: array} $options Stream options: |
|
| 36 | + * - metadata: Hash of metadata to use with stream. |
|
| 37 | + * - size: Size of the stream, if known. |
|
| 38 | + */ |
|
| 39 | + public function __construct(callable $source, array $options = []) |
|
| 40 | + { |
|
| 41 | + $this->source = $source; |
|
| 42 | + $this->size = $options['size'] ?? null; |
|
| 43 | + $this->metadata = $options['metadata'] ?? []; |
|
| 44 | + $this->buffer = new BufferStream(); |
|
| 45 | + } |
|
| 46 | + public function __toString() : string |
|
| 47 | + { |
|
| 48 | + try { |
|
| 49 | + return Utils::copyToString($this); |
|
| 50 | + } catch (\Throwable $e) { |
|
| 51 | + if (\PHP_VERSION_ID >= 70400) { |
|
| 52 | + throw $e; |
|
| 53 | + } |
|
| 54 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 55 | + return ''; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + public function close() : void |
|
| 59 | + { |
|
| 60 | + $this->detach(); |
|
| 61 | + } |
|
| 62 | + public function detach() |
|
| 63 | + { |
|
| 64 | + $this->tellPos = 0; |
|
| 65 | + $this->source = null; |
|
| 66 | + return null; |
|
| 67 | + } |
|
| 68 | + public function getSize() : ?int |
|
| 69 | + { |
|
| 70 | + return $this->size; |
|
| 71 | + } |
|
| 72 | + public function tell() : int |
|
| 73 | + { |
|
| 74 | + return $this->tellPos; |
|
| 75 | + } |
|
| 76 | + public function eof() : bool |
|
| 77 | + { |
|
| 78 | + return $this->source === null; |
|
| 79 | + } |
|
| 80 | + public function isSeekable() : bool |
|
| 81 | + { |
|
| 82 | + return \false; |
|
| 83 | + } |
|
| 84 | + public function rewind() : void |
|
| 85 | + { |
|
| 86 | + $this->seek(0); |
|
| 87 | + } |
|
| 88 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
| 89 | + { |
|
| 90 | + throw new \RuntimeException('Cannot seek a PumpStream'); |
|
| 91 | + } |
|
| 92 | + public function isWritable() : bool |
|
| 93 | + { |
|
| 94 | + return \false; |
|
| 95 | + } |
|
| 96 | + public function write($string) : int |
|
| 97 | + { |
|
| 98 | + throw new \RuntimeException('Cannot write to a PumpStream'); |
|
| 99 | + } |
|
| 100 | + public function isReadable() : bool |
|
| 101 | + { |
|
| 102 | + return \true; |
|
| 103 | + } |
|
| 104 | + public function read($length) : string |
|
| 105 | + { |
|
| 106 | + $data = $this->buffer->read($length); |
|
| 107 | + $readLen = \strlen($data); |
|
| 108 | + $this->tellPos += $readLen; |
|
| 109 | + $remaining = $length - $readLen; |
|
| 110 | + if ($remaining) { |
|
| 111 | + $this->pump($remaining); |
|
| 112 | + $data .= $this->buffer->read($remaining); |
|
| 113 | + $this->tellPos += \strlen($data) - $readLen; |
|
| 114 | + } |
|
| 115 | + return $data; |
|
| 116 | + } |
|
| 117 | + public function getContents() : string |
|
| 118 | + { |
|
| 119 | + $result = ''; |
|
| 120 | + while (!$this->eof()) { |
|
| 121 | + $result .= $this->read(1000000); |
|
| 122 | + } |
|
| 123 | + return $result; |
|
| 124 | + } |
|
| 125 | + /** |
|
| 126 | + * @return mixed |
|
| 127 | + */ |
|
| 128 | + public function getMetadata($key = null) |
|
| 129 | + { |
|
| 130 | + if (!$key) { |
|
| 131 | + return $this->metadata; |
|
| 132 | + } |
|
| 133 | + return $this->metadata[$key] ?? null; |
|
| 134 | + } |
|
| 135 | + private function pump(int $length) : void |
|
| 136 | + { |
|
| 137 | + if ($this->source !== null) { |
|
| 138 | + do { |
|
| 139 | + $data = ($this->source)($length); |
|
| 140 | + if ($data === \false || $data === null) { |
|
| 141 | + $this->source = null; |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | + $this->buffer->write($data); |
|
| 145 | + $length -= \strlen($data); |
|
| 146 | + } while ($length > 0); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -8,15 +8,15 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | final class Rfc7230 |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Header related regular expressions (based on amphp/http package) |
|
| 13 | - * |
|
| 14 | - * Note: header delimiter (\r\n) is modified to \r?\n to accept line feed only delimiters for BC reasons. |
|
| 15 | - * |
|
| 16 | - * @see https://github.com/amphp/http/blob/v1.0.1/src/Rfc7230.php#L12-L15 |
|
| 17 | - * |
|
| 18 | - * @license https://github.com/amphp/http/blob/v1.0.1/LICENSE |
|
| 19 | - */ |
|
| 20 | - public const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\\]?={}\x01- ]++):[ \t]*+((?:[ \t]*+[!-~\x80-\xff]++)*+)[ \t]*+\r?\n)m"; |
|
| 21 | - public const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)"; |
|
| 11 | + /** |
|
| 12 | + * Header related regular expressions (based on amphp/http package) |
|
| 13 | + * |
|
| 14 | + * Note: header delimiter (\r\n) is modified to \r?\n to accept line feed only delimiters for BC reasons. |
|
| 15 | + * |
|
| 16 | + * @see https://github.com/amphp/http/blob/v1.0.1/src/Rfc7230.php#L12-L15 |
|
| 17 | + * |
|
| 18 | + * @license https://github.com/amphp/http/blob/v1.0.1/LICENSE |
|
| 19 | + */ |
|
| 20 | + public const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\\]?={}\x01- ]++):[ \t]*+((?:[ \t]*+[!-~\x80-\xff]++)*+)[ \t]*+\r?\n)m"; |
|
| 21 | + public const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)"; |
|
| 22 | 22 | } |
@@ -13,137 +13,137 @@ |
||
| 13 | 13 | #[\AllowDynamicProperties] |
| 14 | 14 | final class FnStream implements StreamInterface |
| 15 | 15 | { |
| 16 | - private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; |
|
| 17 | - /** @var array<string, callable> */ |
|
| 18 | - private $methods; |
|
| 19 | - /** |
|
| 20 | - * @param array<string, callable> $methods Hash of method name to a callable. |
|
| 21 | - */ |
|
| 22 | - public function __construct(array $methods) |
|
| 23 | - { |
|
| 24 | - $this->methods = $methods; |
|
| 25 | - // Create the functions on the class |
|
| 26 | - foreach ($methods as $name => $fn) { |
|
| 27 | - $this->{'_fn_' . $name} = $fn; |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - /** |
|
| 31 | - * Lazily determine which methods are not implemented. |
|
| 32 | - * |
|
| 33 | - * @throws \BadMethodCallException |
|
| 34 | - */ |
|
| 35 | - public function __get(string $name) : void |
|
| 36 | - { |
|
| 37 | - throw new \BadMethodCallException(\str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); |
|
| 38 | - } |
|
| 39 | - /** |
|
| 40 | - * The close method is called on the underlying stream only if possible. |
|
| 41 | - */ |
|
| 42 | - public function __destruct() |
|
| 43 | - { |
|
| 44 | - if (isset($this->_fn_close)) { |
|
| 45 | - ($this->_fn_close)(); |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - /** |
|
| 49 | - * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
| 50 | - * |
|
| 51 | - * @throws \LogicException |
|
| 52 | - */ |
|
| 53 | - public function __wakeup() : void |
|
| 54 | - { |
|
| 55 | - throw new \LogicException('FnStream should never be unserialized'); |
|
| 56 | - } |
|
| 57 | - /** |
|
| 58 | - * Adds custom functionality to an underlying stream by intercepting |
|
| 59 | - * specific method calls. |
|
| 60 | - * |
|
| 61 | - * @param StreamInterface $stream Stream to decorate |
|
| 62 | - * @param array<string, callable> $methods Hash of method name to a closure |
|
| 63 | - * |
|
| 64 | - * @return FnStream |
|
| 65 | - */ |
|
| 66 | - public static function decorate(StreamInterface $stream, array $methods) |
|
| 67 | - { |
|
| 68 | - // If any of the required methods were not provided, then simply |
|
| 69 | - // proxy to the decorated stream. |
|
| 70 | - foreach (\array_diff(self::SLOTS, \array_keys($methods)) as $diff) { |
|
| 71 | - /** @var callable $callable */ |
|
| 72 | - $callable = [$stream, $diff]; |
|
| 73 | - $methods[$diff] = $callable; |
|
| 74 | - } |
|
| 75 | - return new self($methods); |
|
| 76 | - } |
|
| 77 | - public function __toString() : string |
|
| 78 | - { |
|
| 79 | - try { |
|
| 80 | - /** @var string */ |
|
| 81 | - return ($this->_fn___toString)(); |
|
| 82 | - } catch (\Throwable $e) { |
|
| 83 | - if (\PHP_VERSION_ID >= 70400) { |
|
| 84 | - throw $e; |
|
| 85 | - } |
|
| 86 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 87 | - return ''; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - public function close() : void |
|
| 91 | - { |
|
| 92 | - ($this->_fn_close)(); |
|
| 93 | - } |
|
| 94 | - public function detach() |
|
| 95 | - { |
|
| 96 | - return ($this->_fn_detach)(); |
|
| 97 | - } |
|
| 98 | - public function getSize() : ?int |
|
| 99 | - { |
|
| 100 | - return ($this->_fn_getSize)(); |
|
| 101 | - } |
|
| 102 | - public function tell() : int |
|
| 103 | - { |
|
| 104 | - return ($this->_fn_tell)(); |
|
| 105 | - } |
|
| 106 | - public function eof() : bool |
|
| 107 | - { |
|
| 108 | - return ($this->_fn_eof)(); |
|
| 109 | - } |
|
| 110 | - public function isSeekable() : bool |
|
| 111 | - { |
|
| 112 | - return ($this->_fn_isSeekable)(); |
|
| 113 | - } |
|
| 114 | - public function rewind() : void |
|
| 115 | - { |
|
| 116 | - ($this->_fn_rewind)(); |
|
| 117 | - } |
|
| 118 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
| 119 | - { |
|
| 120 | - ($this->_fn_seek)($offset, $whence); |
|
| 121 | - } |
|
| 122 | - public function isWritable() : bool |
|
| 123 | - { |
|
| 124 | - return ($this->_fn_isWritable)(); |
|
| 125 | - } |
|
| 126 | - public function write($string) : int |
|
| 127 | - { |
|
| 128 | - return ($this->_fn_write)($string); |
|
| 129 | - } |
|
| 130 | - public function isReadable() : bool |
|
| 131 | - { |
|
| 132 | - return ($this->_fn_isReadable)(); |
|
| 133 | - } |
|
| 134 | - public function read($length) : string |
|
| 135 | - { |
|
| 136 | - return ($this->_fn_read)($length); |
|
| 137 | - } |
|
| 138 | - public function getContents() : string |
|
| 139 | - { |
|
| 140 | - return ($this->_fn_getContents)(); |
|
| 141 | - } |
|
| 142 | - /** |
|
| 143 | - * @return mixed |
|
| 144 | - */ |
|
| 145 | - public function getMetadata($key = null) |
|
| 146 | - { |
|
| 147 | - return ($this->_fn_getMetadata)($key); |
|
| 148 | - } |
|
| 16 | + private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; |
|
| 17 | + /** @var array<string, callable> */ |
|
| 18 | + private $methods; |
|
| 19 | + /** |
|
| 20 | + * @param array<string, callable> $methods Hash of method name to a callable. |
|
| 21 | + */ |
|
| 22 | + public function __construct(array $methods) |
|
| 23 | + { |
|
| 24 | + $this->methods = $methods; |
|
| 25 | + // Create the functions on the class |
|
| 26 | + foreach ($methods as $name => $fn) { |
|
| 27 | + $this->{'_fn_' . $name} = $fn; |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + /** |
|
| 31 | + * Lazily determine which methods are not implemented. |
|
| 32 | + * |
|
| 33 | + * @throws \BadMethodCallException |
|
| 34 | + */ |
|
| 35 | + public function __get(string $name) : void |
|
| 36 | + { |
|
| 37 | + throw new \BadMethodCallException(\str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); |
|
| 38 | + } |
|
| 39 | + /** |
|
| 40 | + * The close method is called on the underlying stream only if possible. |
|
| 41 | + */ |
|
| 42 | + public function __destruct() |
|
| 43 | + { |
|
| 44 | + if (isset($this->_fn_close)) { |
|
| 45 | + ($this->_fn_close)(); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + /** |
|
| 49 | + * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
| 50 | + * |
|
| 51 | + * @throws \LogicException |
|
| 52 | + */ |
|
| 53 | + public function __wakeup() : void |
|
| 54 | + { |
|
| 55 | + throw new \LogicException('FnStream should never be unserialized'); |
|
| 56 | + } |
|
| 57 | + /** |
|
| 58 | + * Adds custom functionality to an underlying stream by intercepting |
|
| 59 | + * specific method calls. |
|
| 60 | + * |
|
| 61 | + * @param StreamInterface $stream Stream to decorate |
|
| 62 | + * @param array<string, callable> $methods Hash of method name to a closure |
|
| 63 | + * |
|
| 64 | + * @return FnStream |
|
| 65 | + */ |
|
| 66 | + public static function decorate(StreamInterface $stream, array $methods) |
|
| 67 | + { |
|
| 68 | + // If any of the required methods were not provided, then simply |
|
| 69 | + // proxy to the decorated stream. |
|
| 70 | + foreach (\array_diff(self::SLOTS, \array_keys($methods)) as $diff) { |
|
| 71 | + /** @var callable $callable */ |
|
| 72 | + $callable = [$stream, $diff]; |
|
| 73 | + $methods[$diff] = $callable; |
|
| 74 | + } |
|
| 75 | + return new self($methods); |
|
| 76 | + } |
|
| 77 | + public function __toString() : string |
|
| 78 | + { |
|
| 79 | + try { |
|
| 80 | + /** @var string */ |
|
| 81 | + return ($this->_fn___toString)(); |
|
| 82 | + } catch (\Throwable $e) { |
|
| 83 | + if (\PHP_VERSION_ID >= 70400) { |
|
| 84 | + throw $e; |
|
| 85 | + } |
|
| 86 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); |
|
| 87 | + return ''; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + public function close() : void |
|
| 91 | + { |
|
| 92 | + ($this->_fn_close)(); |
|
| 93 | + } |
|
| 94 | + public function detach() |
|
| 95 | + { |
|
| 96 | + return ($this->_fn_detach)(); |
|
| 97 | + } |
|
| 98 | + public function getSize() : ?int |
|
| 99 | + { |
|
| 100 | + return ($this->_fn_getSize)(); |
|
| 101 | + } |
|
| 102 | + public function tell() : int |
|
| 103 | + { |
|
| 104 | + return ($this->_fn_tell)(); |
|
| 105 | + } |
|
| 106 | + public function eof() : bool |
|
| 107 | + { |
|
| 108 | + return ($this->_fn_eof)(); |
|
| 109 | + } |
|
| 110 | + public function isSeekable() : bool |
|
| 111 | + { |
|
| 112 | + return ($this->_fn_isSeekable)(); |
|
| 113 | + } |
|
| 114 | + public function rewind() : void |
|
| 115 | + { |
|
| 116 | + ($this->_fn_rewind)(); |
|
| 117 | + } |
|
| 118 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
| 119 | + { |
|
| 120 | + ($this->_fn_seek)($offset, $whence); |
|
| 121 | + } |
|
| 122 | + public function isWritable() : bool |
|
| 123 | + { |
|
| 124 | + return ($this->_fn_isWritable)(); |
|
| 125 | + } |
|
| 126 | + public function write($string) : int |
|
| 127 | + { |
|
| 128 | + return ($this->_fn_write)($string); |
|
| 129 | + } |
|
| 130 | + public function isReadable() : bool |
|
| 131 | + { |
|
| 132 | + return ($this->_fn_isReadable)(); |
|
| 133 | + } |
|
| 134 | + public function read($length) : string |
|
| 135 | + { |
|
| 136 | + return ($this->_fn_read)($length); |
|
| 137 | + } |
|
| 138 | + public function getContents() : string |
|
| 139 | + { |
|
| 140 | + return ($this->_fn_getContents)(); |
|
| 141 | + } |
|
| 142 | + /** |
|
| 143 | + * @return mixed |
|
| 144 | + */ |
|
| 145 | + public function getMetadata($key = null) |
|
| 146 | + { |
|
| 147 | + return ($this->_fn_getMetadata)($key); |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -9,15 +9,15 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | final class NoSeekStream implements StreamInterface |
| 11 | 11 | { |
| 12 | - use StreamDecoratorTrait; |
|
| 13 | - /** @var StreamInterface */ |
|
| 14 | - private $stream; |
|
| 15 | - public function seek($offset, $whence = \SEEK_SET) : void |
|
| 16 | - { |
|
| 17 | - throw new \RuntimeException('Cannot seek a NoSeekStream'); |
|
| 18 | - } |
|
| 19 | - public function isSeekable() : bool |
|
| 20 | - { |
|
| 21 | - return \false; |
|
| 22 | - } |
|
| 12 | + use StreamDecoratorTrait; |
|
| 13 | + /** @var StreamInterface */ |
|
| 14 | + private $stream; |
|
| 15 | + public function seek($offset, $whence = \SEEK_SET) : void |
|
| 16 | + { |
|
| 17 | + throw new \RuntimeException('Cannot seek a NoSeekStream'); |
|
| 18 | + } |
|
| 19 | + public function isSeekable() : bool |
|
| 20 | + { |
|
| 21 | + return \false; |
|
| 22 | + } |
|
| 23 | 23 | } |