@@ -15,206 +15,206 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | final class UriNormalizer |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Default normalizations which only include the ones that preserve semantics. |
|
| 20 | - */ |
|
| 21 | - public const PRESERVING_NORMALIZATIONS = |
|
| 22 | - self::CAPITALIZE_PERCENT_ENCODING | |
|
| 23 | - self::DECODE_UNRESERVED_CHARACTERS | |
|
| 24 | - self::CONVERT_EMPTY_PATH | |
|
| 25 | - self::REMOVE_DEFAULT_HOST | |
|
| 26 | - self::REMOVE_DEFAULT_PORT | |
|
| 27 | - self::REMOVE_DOT_SEGMENTS; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. |
|
| 31 | - * |
|
| 32 | - * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b |
|
| 33 | - */ |
|
| 34 | - public const CAPITALIZE_PERCENT_ENCODING = 1; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Decodes percent-encoded octets of unreserved characters. |
|
| 38 | - * |
|
| 39 | - * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), |
|
| 40 | - * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, |
|
| 41 | - * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. |
|
| 42 | - * |
|
| 43 | - * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/ |
|
| 44 | - */ |
|
| 45 | - public const DECODE_UNRESERVED_CHARACTERS = 2; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Converts the empty path to "/" for http and https URIs. |
|
| 49 | - * |
|
| 50 | - * Example: http://example.org → http://example.org/ |
|
| 51 | - */ |
|
| 52 | - public const CONVERT_EMPTY_PATH = 4; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Removes the default host of the given URI scheme from the URI. |
|
| 56 | - * |
|
| 57 | - * Only the "file" scheme defines the default host "localhost". |
|
| 58 | - * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` |
|
| 59 | - * are equivalent according to RFC 3986. The first format is not accepted |
|
| 60 | - * by PHPs stream functions and thus already normalized implicitly to the |
|
| 61 | - * second format in the Uri class. See `OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Uri::composeComponents`. |
|
| 62 | - * |
|
| 63 | - * Example: file://localhost/myfile → file:///myfile |
|
| 64 | - */ |
|
| 65 | - public const REMOVE_DEFAULT_HOST = 8; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Removes the default port of the given URI scheme from the URI. |
|
| 69 | - * |
|
| 70 | - * Example: http://example.org:80/ → http://example.org/ |
|
| 71 | - */ |
|
| 72 | - public const REMOVE_DEFAULT_PORT = 16; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Removes unnecessary dot-segments. |
|
| 76 | - * |
|
| 77 | - * Dot-segments in relative-path references are not removed as it would |
|
| 78 | - * change the semantics of the URI reference. |
|
| 79 | - * |
|
| 80 | - * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html |
|
| 81 | - */ |
|
| 82 | - public const REMOVE_DOT_SEGMENTS = 32; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Paths which include two or more adjacent slashes are converted to one. |
|
| 86 | - * |
|
| 87 | - * Webservers usually ignore duplicate slashes and treat those URIs equivalent. |
|
| 88 | - * But in theory those URIs do not need to be equivalent. So this normalization |
|
| 89 | - * may change the semantics. Encoded slashes (%2F) are not removed. |
|
| 90 | - * |
|
| 91 | - * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html |
|
| 92 | - */ |
|
| 93 | - public const REMOVE_DUPLICATE_SLASHES = 64; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Sort query parameters with their values in alphabetical order. |
|
| 97 | - * |
|
| 98 | - * However, the order of parameters in a URI may be significant (this is not defined by the standard). |
|
| 99 | - * So this normalization is not safe and may change the semantics of the URI. |
|
| 100 | - * |
|
| 101 | - * Example: ?lang=en&article=fred → ?article=fred&lang=en |
|
| 102 | - * |
|
| 103 | - * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the |
|
| 104 | - * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly. |
|
| 105 | - */ |
|
| 106 | - public const SORT_QUERY_PARAMETERS = 128; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Returns a normalized URI. |
|
| 110 | - * |
|
| 111 | - * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. |
|
| 112 | - * This methods adds additional normalizations that can be configured with the $flags parameter. |
|
| 113 | - * |
|
| 114 | - * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as |
|
| 115 | - * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are |
|
| 116 | - * treated equivalent which is not necessarily true according to RFC 3986. But that difference |
|
| 117 | - * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well. |
|
| 118 | - * |
|
| 119 | - * @param UriInterface $uri The URI to normalize |
|
| 120 | - * @param int $flags A bitmask of normalizations to apply, see constants |
|
| 121 | - * |
|
| 122 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2 |
|
| 123 | - */ |
|
| 124 | - public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface |
|
| 125 | - { |
|
| 126 | - if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { |
|
| 127 | - $uri = self::capitalizePercentEncoding($uri); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($flags & self::DECODE_UNRESERVED_CHARACTERS) { |
|
| 131 | - $uri = self::decodeUnreservedCharacters($uri); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' |
|
| 135 | - && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') |
|
| 136 | - ) { |
|
| 137 | - $uri = $uri->withPath('/'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { |
|
| 141 | - $uri = $uri->withHost(''); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { |
|
| 145 | - $uri = $uri->withPort(null); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { |
|
| 149 | - $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if ($flags & self::REMOVE_DUPLICATE_SLASHES) { |
|
| 153 | - $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { |
|
| 157 | - $queryKeyValues = explode('&', $uri->getQuery()); |
|
| 158 | - sort($queryKeyValues); |
|
| 159 | - $uri = $uri->withQuery(implode('&', $queryKeyValues)); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $uri; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Whether two URIs can be considered equivalent. |
|
| 167 | - * |
|
| 168 | - * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also |
|
| 169 | - * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be |
|
| 170 | - * resolved against the same base URI. If this is not the case, determination of equivalence or difference of |
|
| 171 | - * relative references does not mean anything. |
|
| 172 | - * |
|
| 173 | - * @param UriInterface $uri1 An URI to compare |
|
| 174 | - * @param UriInterface $uri2 An URI to compare |
|
| 175 | - * @param int $normalizations A bitmask of normalizations to apply, see constants |
|
| 176 | - * |
|
| 177 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1 |
|
| 178 | - */ |
|
| 179 | - public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
|
| 180 | - { |
|
| 181 | - return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
|
| 185 | - { |
|
| 186 | - $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
|
| 187 | - |
|
| 188 | - $callback = function (array $match): string { |
|
| 189 | - return strtoupper($match[0]); |
|
| 190 | - }; |
|
| 191 | - |
|
| 192 | - return |
|
| 193 | - $uri->withPath( |
|
| 194 | - preg_replace_callback($regex, $callback, $uri->getPath()) |
|
| 195 | - )->withQuery( |
|
| 196 | - preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
| 197 | - ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface |
|
| 201 | - { |
|
| 202 | - $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
|
| 203 | - |
|
| 204 | - $callback = function (array $match): string { |
|
| 205 | - return rawurldecode($match[0]); |
|
| 206 | - }; |
|
| 207 | - |
|
| 208 | - return |
|
| 209 | - $uri->withPath( |
|
| 210 | - preg_replace_callback($regex, $callback, $uri->getPath()) |
|
| 211 | - )->withQuery( |
|
| 212 | - preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
| 213 | - ); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - private function __construct() |
|
| 217 | - { |
|
| 218 | - // cannot be instantiated |
|
| 219 | - } |
|
| 18 | + /** |
|
| 19 | + * Default normalizations which only include the ones that preserve semantics. |
|
| 20 | + */ |
|
| 21 | + public const PRESERVING_NORMALIZATIONS = |
|
| 22 | + self::CAPITALIZE_PERCENT_ENCODING | |
|
| 23 | + self::DECODE_UNRESERVED_CHARACTERS | |
|
| 24 | + self::CONVERT_EMPTY_PATH | |
|
| 25 | + self::REMOVE_DEFAULT_HOST | |
|
| 26 | + self::REMOVE_DEFAULT_PORT | |
|
| 27 | + self::REMOVE_DOT_SEGMENTS; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. |
|
| 31 | + * |
|
| 32 | + * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b |
|
| 33 | + */ |
|
| 34 | + public const CAPITALIZE_PERCENT_ENCODING = 1; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Decodes percent-encoded octets of unreserved characters. |
|
| 38 | + * |
|
| 39 | + * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), |
|
| 40 | + * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, |
|
| 41 | + * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. |
|
| 42 | + * |
|
| 43 | + * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/ |
|
| 44 | + */ |
|
| 45 | + public const DECODE_UNRESERVED_CHARACTERS = 2; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Converts the empty path to "/" for http and https URIs. |
|
| 49 | + * |
|
| 50 | + * Example: http://example.org → http://example.org/ |
|
| 51 | + */ |
|
| 52 | + public const CONVERT_EMPTY_PATH = 4; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Removes the default host of the given URI scheme from the URI. |
|
| 56 | + * |
|
| 57 | + * Only the "file" scheme defines the default host "localhost". |
|
| 58 | + * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` |
|
| 59 | + * are equivalent according to RFC 3986. The first format is not accepted |
|
| 60 | + * by PHPs stream functions and thus already normalized implicitly to the |
|
| 61 | + * second format in the Uri class. See `OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Uri::composeComponents`. |
|
| 62 | + * |
|
| 63 | + * Example: file://localhost/myfile → file:///myfile |
|
| 64 | + */ |
|
| 65 | + public const REMOVE_DEFAULT_HOST = 8; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Removes the default port of the given URI scheme from the URI. |
|
| 69 | + * |
|
| 70 | + * Example: http://example.org:80/ → http://example.org/ |
|
| 71 | + */ |
|
| 72 | + public const REMOVE_DEFAULT_PORT = 16; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Removes unnecessary dot-segments. |
|
| 76 | + * |
|
| 77 | + * Dot-segments in relative-path references are not removed as it would |
|
| 78 | + * change the semantics of the URI reference. |
|
| 79 | + * |
|
| 80 | + * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html |
|
| 81 | + */ |
|
| 82 | + public const REMOVE_DOT_SEGMENTS = 32; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Paths which include two or more adjacent slashes are converted to one. |
|
| 86 | + * |
|
| 87 | + * Webservers usually ignore duplicate slashes and treat those URIs equivalent. |
|
| 88 | + * But in theory those URIs do not need to be equivalent. So this normalization |
|
| 89 | + * may change the semantics. Encoded slashes (%2F) are not removed. |
|
| 90 | + * |
|
| 91 | + * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html |
|
| 92 | + */ |
|
| 93 | + public const REMOVE_DUPLICATE_SLASHES = 64; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Sort query parameters with their values in alphabetical order. |
|
| 97 | + * |
|
| 98 | + * However, the order of parameters in a URI may be significant (this is not defined by the standard). |
|
| 99 | + * So this normalization is not safe and may change the semantics of the URI. |
|
| 100 | + * |
|
| 101 | + * Example: ?lang=en&article=fred → ?article=fred&lang=en |
|
| 102 | + * |
|
| 103 | + * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the |
|
| 104 | + * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly. |
|
| 105 | + */ |
|
| 106 | + public const SORT_QUERY_PARAMETERS = 128; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Returns a normalized URI. |
|
| 110 | + * |
|
| 111 | + * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. |
|
| 112 | + * This methods adds additional normalizations that can be configured with the $flags parameter. |
|
| 113 | + * |
|
| 114 | + * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as |
|
| 115 | + * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are |
|
| 116 | + * treated equivalent which is not necessarily true according to RFC 3986. But that difference |
|
| 117 | + * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well. |
|
| 118 | + * |
|
| 119 | + * @param UriInterface $uri The URI to normalize |
|
| 120 | + * @param int $flags A bitmask of normalizations to apply, see constants |
|
| 121 | + * |
|
| 122 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2 |
|
| 123 | + */ |
|
| 124 | + public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface |
|
| 125 | + { |
|
| 126 | + if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { |
|
| 127 | + $uri = self::capitalizePercentEncoding($uri); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($flags & self::DECODE_UNRESERVED_CHARACTERS) { |
|
| 131 | + $uri = self::decodeUnreservedCharacters($uri); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' |
|
| 135 | + && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') |
|
| 136 | + ) { |
|
| 137 | + $uri = $uri->withPath('/'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { |
|
| 141 | + $uri = $uri->withHost(''); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { |
|
| 145 | + $uri = $uri->withPort(null); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { |
|
| 149 | + $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if ($flags & self::REMOVE_DUPLICATE_SLASHES) { |
|
| 153 | + $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { |
|
| 157 | + $queryKeyValues = explode('&', $uri->getQuery()); |
|
| 158 | + sort($queryKeyValues); |
|
| 159 | + $uri = $uri->withQuery(implode('&', $queryKeyValues)); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $uri; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Whether two URIs can be considered equivalent. |
|
| 167 | + * |
|
| 168 | + * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also |
|
| 169 | + * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be |
|
| 170 | + * resolved against the same base URI. If this is not the case, determination of equivalence or difference of |
|
| 171 | + * relative references does not mean anything. |
|
| 172 | + * |
|
| 173 | + * @param UriInterface $uri1 An URI to compare |
|
| 174 | + * @param UriInterface $uri2 An URI to compare |
|
| 175 | + * @param int $normalizations A bitmask of normalizations to apply, see constants |
|
| 176 | + * |
|
| 177 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1 |
|
| 178 | + */ |
|
| 179 | + public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
|
| 180 | + { |
|
| 181 | + return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
|
| 185 | + { |
|
| 186 | + $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
|
| 187 | + |
|
| 188 | + $callback = function (array $match): string { |
|
| 189 | + return strtoupper($match[0]); |
|
| 190 | + }; |
|
| 191 | + |
|
| 192 | + return |
|
| 193 | + $uri->withPath( |
|
| 194 | + preg_replace_callback($regex, $callback, $uri->getPath()) |
|
| 195 | + )->withQuery( |
|
| 196 | + preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
| 197 | + ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface |
|
| 201 | + { |
|
| 202 | + $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
|
| 203 | + |
|
| 204 | + $callback = function (array $match): string { |
|
| 205 | + return rawurldecode($match[0]); |
|
| 206 | + }; |
|
| 207 | + |
|
| 208 | + return |
|
| 209 | + $uri->withPath( |
|
| 210 | + preg_replace_callback($regex, $callback, $uri->getPath()) |
|
| 211 | + )->withQuery( |
|
| 212 | + preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
| 213 | + ); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + private function __construct() |
|
| 217 | + { |
|
| 218 | + // cannot be instantiated |
|
| 219 | + } |
|
| 220 | 220 | } |
@@ -178,14 +178,14 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
| 180 | 180 | { |
| 181 | - return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
|
| 181 | + return (string)self::normalize($uri1, $normalizations) === (string)self::normalize($uri2, $normalizations); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
| 185 | 185 | { |
| 186 | 186 | $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
| 187 | 187 | |
| 188 | - $callback = function (array $match): string { |
|
| 188 | + $callback = function(array $match): string { |
|
| 189 | 189 | return strtoupper($match[0]); |
| 190 | 190 | }; |
| 191 | 191 | |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | { |
| 202 | 202 | $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
| 203 | 203 | |
| 204 | - $callback = function (array $match): string { |
|
| 204 | + $callback = function(array $match): string { |
|
| 205 | 205 | return rawurldecode($match[0]); |
| 206 | 206 | }; |
| 207 | 207 | |
@@ -13,8 +13,7 @@ |
||
| 13 | 13 | * |
| 14 | 14 | * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6 |
| 15 | 15 | */ |
| 16 | -final class UriNormalizer |
|
| 17 | -{ |
|
| 16 | +final class UriNormalizer { |
|
| 18 | 17 | /** |
| 19 | 18 | * Default normalizations which only include the ones that preserve semantics. |
| 20 | 19 | */ |
@@ -11,18 +11,18 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | final class NoSeekStream implements StreamInterface |
| 13 | 13 | { |
| 14 | - use StreamDecoratorTrait; |
|
| 14 | + use StreamDecoratorTrait; |
|
| 15 | 15 | |
| 16 | - /** @var StreamInterface */ |
|
| 17 | - private $stream; |
|
| 16 | + /** @var StreamInterface */ |
|
| 17 | + private $stream; |
|
| 18 | 18 | |
| 19 | - public function seek($offset, $whence = SEEK_SET): void |
|
| 20 | - { |
|
| 21 | - throw new \RuntimeException('Cannot seek a NoSeekStream'); |
|
| 22 | - } |
|
| 19 | + public function seek($offset, $whence = SEEK_SET): void |
|
| 20 | + { |
|
| 21 | + throw new \RuntimeException('Cannot seek a NoSeekStream'); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function isSeekable(): bool |
|
| 25 | - { |
|
| 26 | - return false; |
|
| 27 | - } |
|
| 24 | + public function isSeekable(): bool |
|
| 25 | + { |
|
| 26 | + return false; |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -9,8 +9,7 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * Stream decorator that prevents a stream from being seeked. |
| 11 | 11 | */ |
| 12 | -final class NoSeekStream implements StreamInterface |
|
| 13 | -{ |
|
| 12 | +final class NoSeekStream implements StreamInterface { |
|
| 14 | 13 | use StreamDecoratorTrait; |
| 15 | 14 | |
| 16 | 15 | /** @var StreamInterface */ |
@@ -13,40 +13,40 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | final class UriComparator |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Determines if a modified URL should be considered cross-origin with |
|
| 18 | - * respect to an original URL. |
|
| 19 | - */ |
|
| 20 | - public static function isCrossOrigin(UriInterface $original, UriInterface $modified): bool |
|
| 21 | - { |
|
| 22 | - if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) { |
|
| 23 | - return true; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - if ($original->getScheme() !== $modified->getScheme()) { |
|
| 27 | - return true; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - if (self::computePort($original) !== self::computePort($modified)) { |
|
| 31 | - return true; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - return false; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - private static function computePort(UriInterface $uri): int |
|
| 38 | - { |
|
| 39 | - $port = $uri->getPort(); |
|
| 40 | - |
|
| 41 | - if (null !== $port) { |
|
| 42 | - return $port; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return 'https' === $uri->getScheme() ? 443 : 80; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - private function __construct() |
|
| 49 | - { |
|
| 50 | - // cannot be instantiated |
|
| 51 | - } |
|
| 16 | + /** |
|
| 17 | + * Determines if a modified URL should be considered cross-origin with |
|
| 18 | + * respect to an original URL. |
|
| 19 | + */ |
|
| 20 | + public static function isCrossOrigin(UriInterface $original, UriInterface $modified): bool |
|
| 21 | + { |
|
| 22 | + if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) { |
|
| 23 | + return true; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + if ($original->getScheme() !== $modified->getScheme()) { |
|
| 27 | + return true; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + if (self::computePort($original) !== self::computePort($modified)) { |
|
| 31 | + return true; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + return false; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + private static function computePort(UriInterface $uri): int |
|
| 38 | + { |
|
| 39 | + $port = $uri->getPort(); |
|
| 40 | + |
|
| 41 | + if (null !== $port) { |
|
| 42 | + return $port; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return 'https' === $uri->getScheme() ? 443 : 80; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + private function __construct() |
|
| 49 | + { |
|
| 50 | + // cannot be instantiated |
|
| 51 | + } |
|
| 52 | 52 | } |
@@ -11,8 +11,7 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @author Graham Campbell |
| 13 | 13 | */ |
| 14 | -final class UriComparator |
|
| 15 | -{ |
|
| 14 | +final class UriComparator { |
|
| 16 | 15 | /** |
| 17 | 16 | * Determines if a modified URL should be considered cross-origin with |
| 18 | 17 | * respect to an original URL. |
@@ -15,166 +15,166 @@ |
||
| 15 | 15 | #[\AllowDynamicProperties] |
| 16 | 16 | final class FnStream implements StreamInterface |
| 17 | 17 | { |
| 18 | - private const SLOTS = [ |
|
| 19 | - '__toString', 'close', 'detach', 'rewind', |
|
| 20 | - 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
|
| 21 | - 'isReadable', 'read', 'getContents', 'getMetadata', |
|
| 22 | - ]; |
|
| 23 | - |
|
| 24 | - /** @var array<string, callable> */ |
|
| 25 | - private $methods; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @param array<string, callable> $methods Hash of method name to a callable. |
|
| 29 | - */ |
|
| 30 | - public function __construct(array $methods) |
|
| 31 | - { |
|
| 32 | - $this->methods = $methods; |
|
| 33 | - |
|
| 34 | - // Create the functions on the class |
|
| 35 | - foreach ($methods as $name => $fn) { |
|
| 36 | - $this->{'_fn_'.$name} = $fn; |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Lazily determine which methods are not implemented. |
|
| 42 | - * |
|
| 43 | - * @throws \BadMethodCallException |
|
| 44 | - */ |
|
| 45 | - public function __get(string $name): void |
|
| 46 | - { |
|
| 47 | - throw new \BadMethodCallException(str_replace('_fn_', '', $name) |
|
| 48 | - .'() is not implemented in the FnStream'); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The close method is called on the underlying stream only if possible. |
|
| 53 | - */ |
|
| 54 | - public function __destruct() |
|
| 55 | - { |
|
| 56 | - if (isset($this->_fn_close)) { |
|
| 57 | - ($this->_fn_close)(); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
| 63 | - * |
|
| 64 | - * @throws \LogicException |
|
| 65 | - */ |
|
| 66 | - public function __wakeup(): void |
|
| 67 | - { |
|
| 68 | - throw new \LogicException('FnStream should never be unserialized'); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Adds custom functionality to an underlying stream by intercepting |
|
| 73 | - * specific method calls. |
|
| 74 | - * |
|
| 75 | - * @param StreamInterface $stream Stream to decorate |
|
| 76 | - * @param array<string, callable> $methods Hash of method name to a closure |
|
| 77 | - * |
|
| 78 | - * @return FnStream |
|
| 79 | - */ |
|
| 80 | - public static function decorate(StreamInterface $stream, array $methods) |
|
| 81 | - { |
|
| 82 | - // If any of the required methods were not provided, then simply |
|
| 83 | - // proxy to the decorated stream. |
|
| 84 | - foreach (array_diff(self::SLOTS, array_keys($methods)) as $diff) { |
|
| 85 | - /** @var callable $callable */ |
|
| 86 | - $callable = [$stream, $diff]; |
|
| 87 | - $methods[$diff] = $callable; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return new self($methods); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function __toString(): string |
|
| 94 | - { |
|
| 95 | - try { |
|
| 96 | - /** @var string */ |
|
| 97 | - return ($this->_fn___toString)(); |
|
| 98 | - } catch (\Throwable $e) { |
|
| 99 | - if (\PHP_VERSION_ID >= 70400) { |
|
| 100 | - throw $e; |
|
| 101 | - } |
|
| 102 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
| 103 | - |
|
| 104 | - return ''; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function close(): void |
|
| 109 | - { |
|
| 110 | - ($this->_fn_close)(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function detach() |
|
| 114 | - { |
|
| 115 | - return ($this->_fn_detach)(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function getSize(): ?int |
|
| 119 | - { |
|
| 120 | - return ($this->_fn_getSize)(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - public function tell(): int |
|
| 124 | - { |
|
| 125 | - return ($this->_fn_tell)(); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public function eof(): bool |
|
| 129 | - { |
|
| 130 | - return ($this->_fn_eof)(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function isSeekable(): bool |
|
| 134 | - { |
|
| 135 | - return ($this->_fn_isSeekable)(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - public function rewind(): void |
|
| 139 | - { |
|
| 140 | - ($this->_fn_rewind)(); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - public function seek($offset, $whence = SEEK_SET): void |
|
| 144 | - { |
|
| 145 | - ($this->_fn_seek)($offset, $whence); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function isWritable(): bool |
|
| 149 | - { |
|
| 150 | - return ($this->_fn_isWritable)(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function write($string): int |
|
| 154 | - { |
|
| 155 | - return ($this->_fn_write)($string); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function isReadable(): bool |
|
| 159 | - { |
|
| 160 | - return ($this->_fn_isReadable)(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - public function read($length): string |
|
| 164 | - { |
|
| 165 | - return ($this->_fn_read)($length); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - public function getContents(): string |
|
| 169 | - { |
|
| 170 | - return ($this->_fn_getContents)(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @return mixed |
|
| 175 | - */ |
|
| 176 | - public function getMetadata($key = null) |
|
| 177 | - { |
|
| 178 | - return ($this->_fn_getMetadata)($key); |
|
| 179 | - } |
|
| 18 | + private const SLOTS = [ |
|
| 19 | + '__toString', 'close', 'detach', 'rewind', |
|
| 20 | + 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
|
| 21 | + 'isReadable', 'read', 'getContents', 'getMetadata', |
|
| 22 | + ]; |
|
| 23 | + |
|
| 24 | + /** @var array<string, callable> */ |
|
| 25 | + private $methods; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @param array<string, callable> $methods Hash of method name to a callable. |
|
| 29 | + */ |
|
| 30 | + public function __construct(array $methods) |
|
| 31 | + { |
|
| 32 | + $this->methods = $methods; |
|
| 33 | + |
|
| 34 | + // Create the functions on the class |
|
| 35 | + foreach ($methods as $name => $fn) { |
|
| 36 | + $this->{'_fn_'.$name} = $fn; |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Lazily determine which methods are not implemented. |
|
| 42 | + * |
|
| 43 | + * @throws \BadMethodCallException |
|
| 44 | + */ |
|
| 45 | + public function __get(string $name): void |
|
| 46 | + { |
|
| 47 | + throw new \BadMethodCallException(str_replace('_fn_', '', $name) |
|
| 48 | + .'() is not implemented in the FnStream'); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The close method is called on the underlying stream only if possible. |
|
| 53 | + */ |
|
| 54 | + public function __destruct() |
|
| 55 | + { |
|
| 56 | + if (isset($this->_fn_close)) { |
|
| 57 | + ($this->_fn_close)(); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
| 63 | + * |
|
| 64 | + * @throws \LogicException |
|
| 65 | + */ |
|
| 66 | + public function __wakeup(): void |
|
| 67 | + { |
|
| 68 | + throw new \LogicException('FnStream should never be unserialized'); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Adds custom functionality to an underlying stream by intercepting |
|
| 73 | + * specific method calls. |
|
| 74 | + * |
|
| 75 | + * @param StreamInterface $stream Stream to decorate |
|
| 76 | + * @param array<string, callable> $methods Hash of method name to a closure |
|
| 77 | + * |
|
| 78 | + * @return FnStream |
|
| 79 | + */ |
|
| 80 | + public static function decorate(StreamInterface $stream, array $methods) |
|
| 81 | + { |
|
| 82 | + // If any of the required methods were not provided, then simply |
|
| 83 | + // proxy to the decorated stream. |
|
| 84 | + foreach (array_diff(self::SLOTS, array_keys($methods)) as $diff) { |
|
| 85 | + /** @var callable $callable */ |
|
| 86 | + $callable = [$stream, $diff]; |
|
| 87 | + $methods[$diff] = $callable; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return new self($methods); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function __toString(): string |
|
| 94 | + { |
|
| 95 | + try { |
|
| 96 | + /** @var string */ |
|
| 97 | + return ($this->_fn___toString)(); |
|
| 98 | + } catch (\Throwable $e) { |
|
| 99 | + if (\PHP_VERSION_ID >= 70400) { |
|
| 100 | + throw $e; |
|
| 101 | + } |
|
| 102 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
| 103 | + |
|
| 104 | + return ''; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function close(): void |
|
| 109 | + { |
|
| 110 | + ($this->_fn_close)(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function detach() |
|
| 114 | + { |
|
| 115 | + return ($this->_fn_detach)(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function getSize(): ?int |
|
| 119 | + { |
|
| 120 | + return ($this->_fn_getSize)(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + public function tell(): int |
|
| 124 | + { |
|
| 125 | + return ($this->_fn_tell)(); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public function eof(): bool |
|
| 129 | + { |
|
| 130 | + return ($this->_fn_eof)(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function isSeekable(): bool |
|
| 134 | + { |
|
| 135 | + return ($this->_fn_isSeekable)(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + public function rewind(): void |
|
| 139 | + { |
|
| 140 | + ($this->_fn_rewind)(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + public function seek($offset, $whence = SEEK_SET): void |
|
| 144 | + { |
|
| 145 | + ($this->_fn_seek)($offset, $whence); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function isWritable(): bool |
|
| 149 | + { |
|
| 150 | + return ($this->_fn_isWritable)(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function write($string): int |
|
| 154 | + { |
|
| 155 | + return ($this->_fn_write)($string); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function isReadable(): bool |
|
| 159 | + { |
|
| 160 | + return ($this->_fn_isReadable)(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + public function read($length): string |
|
| 164 | + { |
|
| 165 | + return ($this->_fn_read)($length); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + public function getContents(): string |
|
| 169 | + { |
|
| 170 | + return ($this->_fn_getContents)(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @return mixed |
|
| 175 | + */ |
|
| 176 | + public function getMetadata($key = null) |
|
| 177 | + { |
|
| 178 | + return ($this->_fn_getMetadata)($key); |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -99,7 +99,7 @@ |
||
| 99 | 99 | if (\PHP_VERSION_ID >= 70400) { |
| 100 | 100 | throw $e; |
| 101 | 101 | } |
| 102 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
| 102 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string)$e), E_USER_ERROR); |
|
| 103 | 103 | |
| 104 | 104 | return ''; |
| 105 | 105 | } |
@@ -13,8 +13,7 @@ |
||
| 13 | 13 | * to create a concrete class for a simple extension point. |
| 14 | 14 | */ |
| 15 | 15 | #[\AllowDynamicProperties] |
| 16 | -final class FnStream implements StreamInterface |
|
| 17 | -{ |
|
| 16 | +final class FnStream implements StreamInterface { |
|
| 18 | 17 | private const SLOTS = [ |
| 19 | 18 | '__toString', 'close', 'detach', 'rewind', |
| 20 | 19 | 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
@@ -16,132 +16,132 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class BufferStream implements StreamInterface |
| 18 | 18 | { |
| 19 | - /** @var int */ |
|
| 20 | - private $hwm; |
|
| 21 | - |
|
| 22 | - /** @var string */ |
|
| 23 | - private $buffer = ''; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @param int $hwm High water mark, representing the preferred maximum |
|
| 27 | - * buffer size. If the size of the buffer exceeds the high |
|
| 28 | - * water mark, then calls to write will continue to succeed |
|
| 29 | - * but will return 0 to inform writers to slow down |
|
| 30 | - * until the buffer has been drained by reading from it. |
|
| 31 | - */ |
|
| 32 | - public function __construct(int $hwm = 16384) |
|
| 33 | - { |
|
| 34 | - $this->hwm = $hwm; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function __toString(): string |
|
| 38 | - { |
|
| 39 | - return $this->getContents(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function getContents(): string |
|
| 43 | - { |
|
| 44 | - $buffer = $this->buffer; |
|
| 45 | - $this->buffer = ''; |
|
| 46 | - |
|
| 47 | - return $buffer; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function close(): void |
|
| 51 | - { |
|
| 52 | - $this->buffer = ''; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function detach() |
|
| 56 | - { |
|
| 57 | - $this->close(); |
|
| 58 | - |
|
| 59 | - return null; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function getSize(): ?int |
|
| 63 | - { |
|
| 64 | - return strlen($this->buffer); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function isReadable(): bool |
|
| 68 | - { |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function isWritable(): bool |
|
| 73 | - { |
|
| 74 | - return true; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function isSeekable(): bool |
|
| 78 | - { |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function rewind(): void |
|
| 83 | - { |
|
| 84 | - $this->seek(0); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function seek($offset, $whence = SEEK_SET): void |
|
| 88 | - { |
|
| 89 | - throw new \RuntimeException('Cannot seek a BufferStream'); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function eof(): bool |
|
| 93 | - { |
|
| 94 | - return strlen($this->buffer) === 0; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - public function tell(): int |
|
| 98 | - { |
|
| 99 | - throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Reads data from the buffer. |
|
| 104 | - */ |
|
| 105 | - public function read($length): string |
|
| 106 | - { |
|
| 107 | - $currentLength = strlen($this->buffer); |
|
| 108 | - |
|
| 109 | - if ($length >= $currentLength) { |
|
| 110 | - // No need to slice the buffer because we don't have enough data. |
|
| 111 | - $result = $this->buffer; |
|
| 112 | - $this->buffer = ''; |
|
| 113 | - } else { |
|
| 114 | - // Slice up the result to provide a subset of the buffer. |
|
| 115 | - $result = substr($this->buffer, 0, $length); |
|
| 116 | - $this->buffer = substr($this->buffer, $length); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $result; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Writes data to the buffer. |
|
| 124 | - */ |
|
| 125 | - public function write($string): int |
|
| 126 | - { |
|
| 127 | - $this->buffer .= $string; |
|
| 128 | - |
|
| 129 | - if (strlen($this->buffer) >= $this->hwm) { |
|
| 130 | - return 0; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return strlen($string); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @return mixed |
|
| 138 | - */ |
|
| 139 | - public function getMetadata($key = null) |
|
| 140 | - { |
|
| 141 | - if ($key === 'hwm') { |
|
| 142 | - return $this->hwm; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return $key ? null : []; |
|
| 146 | - } |
|
| 19 | + /** @var int */ |
|
| 20 | + private $hwm; |
|
| 21 | + |
|
| 22 | + /** @var string */ |
|
| 23 | + private $buffer = ''; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @param int $hwm High water mark, representing the preferred maximum |
|
| 27 | + * buffer size. If the size of the buffer exceeds the high |
|
| 28 | + * water mark, then calls to write will continue to succeed |
|
| 29 | + * but will return 0 to inform writers to slow down |
|
| 30 | + * until the buffer has been drained by reading from it. |
|
| 31 | + */ |
|
| 32 | + public function __construct(int $hwm = 16384) |
|
| 33 | + { |
|
| 34 | + $this->hwm = $hwm; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function __toString(): string |
|
| 38 | + { |
|
| 39 | + return $this->getContents(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function getContents(): string |
|
| 43 | + { |
|
| 44 | + $buffer = $this->buffer; |
|
| 45 | + $this->buffer = ''; |
|
| 46 | + |
|
| 47 | + return $buffer; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function close(): void |
|
| 51 | + { |
|
| 52 | + $this->buffer = ''; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function detach() |
|
| 56 | + { |
|
| 57 | + $this->close(); |
|
| 58 | + |
|
| 59 | + return null; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function getSize(): ?int |
|
| 63 | + { |
|
| 64 | + return strlen($this->buffer); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function isReadable(): bool |
|
| 68 | + { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function isWritable(): bool |
|
| 73 | + { |
|
| 74 | + return true; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function isSeekable(): bool |
|
| 78 | + { |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function rewind(): void |
|
| 83 | + { |
|
| 84 | + $this->seek(0); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function seek($offset, $whence = SEEK_SET): void |
|
| 88 | + { |
|
| 89 | + throw new \RuntimeException('Cannot seek a BufferStream'); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function eof(): bool |
|
| 93 | + { |
|
| 94 | + return strlen($this->buffer) === 0; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + public function tell(): int |
|
| 98 | + { |
|
| 99 | + throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Reads data from the buffer. |
|
| 104 | + */ |
|
| 105 | + public function read($length): string |
|
| 106 | + { |
|
| 107 | + $currentLength = strlen($this->buffer); |
|
| 108 | + |
|
| 109 | + if ($length >= $currentLength) { |
|
| 110 | + // No need to slice the buffer because we don't have enough data. |
|
| 111 | + $result = $this->buffer; |
|
| 112 | + $this->buffer = ''; |
|
| 113 | + } else { |
|
| 114 | + // Slice up the result to provide a subset of the buffer. |
|
| 115 | + $result = substr($this->buffer, 0, $length); |
|
| 116 | + $this->buffer = substr($this->buffer, $length); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $result; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Writes data to the buffer. |
|
| 124 | + */ |
|
| 125 | + public function write($string): int |
|
| 126 | + { |
|
| 127 | + $this->buffer .= $string; |
|
| 128 | + |
|
| 129 | + if (strlen($this->buffer) >= $this->hwm) { |
|
| 130 | + return 0; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return strlen($string); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @return mixed |
|
| 138 | + */ |
|
| 139 | + public function getMetadata($key = null) |
|
| 140 | + { |
|
| 141 | + if ($key === 'hwm') { |
|
| 142 | + return $this->hwm; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return $key ? null : []; |
|
| 146 | + } |
|
| 147 | 147 | } |
@@ -14,8 +14,7 @@ |
||
| 14 | 14 | * what the configured high water mark of the stream is, or the maximum |
| 15 | 15 | * preferred size of the buffer. |
| 16 | 16 | */ |
| 17 | -final class BufferStream implements StreamInterface |
|
| 18 | -{ |
|
| 17 | +final class BufferStream implements StreamInterface { |
|
| 19 | 18 | /** @var int */ |
| 20 | 19 | private $hwm; |
| 21 | 20 | |
@@ -6,8 +6,8 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface BodySummarizerInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Returns a summarized message body. |
|
| 11 | - */ |
|
| 12 | - public function summarize(MessageInterface $message): ?string; |
|
| 9 | + /** |
|
| 10 | + * Returns a summarized message body. |
|
| 11 | + */ |
|
| 12 | + public function summarize(MessageInterface $message): ?string; |
|
| 13 | 13 | } |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\MessageInterface; |
| 6 | 6 | |
| 7 | -interface BodySummarizerInterface |
|
| 8 | -{ |
|
| 7 | +interface BodySummarizerInterface { |
|
| 9 | 8 | /** |
| 10 | 9 | * Returns a summarized message body. |
| 11 | 10 | */ |
@@ -15,105 +15,105 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class RetryMiddleware |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 20 | - */ |
|
| 21 | - private $nextHandler; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var callable |
|
| 25 | - */ |
|
| 26 | - private $decider; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var callable(int) |
|
| 30 | - */ |
|
| 31 | - private $delay; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param callable $decider Function that accepts the number of retries, |
|
| 35 | - * a request, [response], and [exception] and |
|
| 36 | - * returns true if the request is to be |
|
| 37 | - * retried. |
|
| 38 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 39 | - * @param (callable(int): int)|null $delay Function that accepts the number of retries |
|
| 40 | - * and returns the number of |
|
| 41 | - * milliseconds to delay. |
|
| 42 | - */ |
|
| 43 | - public function __construct(callable $decider, callable $nextHandler, callable $delay = null) |
|
| 44 | - { |
|
| 45 | - $this->decider = $decider; |
|
| 46 | - $this->nextHandler = $nextHandler; |
|
| 47 | - $this->delay = $delay ?: __CLASS__.'::exponentialDelay'; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Default exponential backoff delay function. |
|
| 52 | - * |
|
| 53 | - * @return int milliseconds. |
|
| 54 | - */ |
|
| 55 | - public static function exponentialDelay(int $retries): int |
|
| 56 | - { |
|
| 57 | - return (int) 2 ** ($retries - 1) * 1000; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 61 | - { |
|
| 62 | - if (!isset($options['retries'])) { |
|
| 63 | - $options['retries'] = 0; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $fn = $this->nextHandler; |
|
| 67 | - |
|
| 68 | - return $fn($request, $options) |
|
| 69 | - ->then( |
|
| 70 | - $this->onFulfilled($request, $options), |
|
| 71 | - $this->onRejected($request, $options) |
|
| 72 | - ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Execute fulfilled closure |
|
| 77 | - */ |
|
| 78 | - private function onFulfilled(RequestInterface $request, array $options): callable |
|
| 79 | - { |
|
| 80 | - return function ($value) use ($request, $options) { |
|
| 81 | - if (!($this->decider)( |
|
| 82 | - $options['retries'], |
|
| 83 | - $request, |
|
| 84 | - $value, |
|
| 85 | - null |
|
| 86 | - )) { |
|
| 87 | - return $value; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $this->doRetry($request, $options, $value); |
|
| 91 | - }; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Execute rejected closure |
|
| 96 | - */ |
|
| 97 | - private function onRejected(RequestInterface $req, array $options): callable |
|
| 98 | - { |
|
| 99 | - return function ($reason) use ($req, $options) { |
|
| 100 | - if (!($this->decider)( |
|
| 101 | - $options['retries'], |
|
| 102 | - $req, |
|
| 103 | - null, |
|
| 104 | - $reason |
|
| 105 | - )) { |
|
| 106 | - return P\Create::rejectionFor($reason); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $this->doRetry($req, $options); |
|
| 110 | - }; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null): PromiseInterface |
|
| 114 | - { |
|
| 115 | - $options['delay'] = ($this->delay)(++$options['retries'], $response, $request); |
|
| 116 | - |
|
| 117 | - return $this($request, $options); |
|
| 118 | - } |
|
| 18 | + /** |
|
| 19 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 20 | + */ |
|
| 21 | + private $nextHandler; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var callable |
|
| 25 | + */ |
|
| 26 | + private $decider; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var callable(int) |
|
| 30 | + */ |
|
| 31 | + private $delay; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param callable $decider Function that accepts the number of retries, |
|
| 35 | + * a request, [response], and [exception] and |
|
| 36 | + * returns true if the request is to be |
|
| 37 | + * retried. |
|
| 38 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 39 | + * @param (callable(int): int)|null $delay Function that accepts the number of retries |
|
| 40 | + * and returns the number of |
|
| 41 | + * milliseconds to delay. |
|
| 42 | + */ |
|
| 43 | + public function __construct(callable $decider, callable $nextHandler, callable $delay = null) |
|
| 44 | + { |
|
| 45 | + $this->decider = $decider; |
|
| 46 | + $this->nextHandler = $nextHandler; |
|
| 47 | + $this->delay = $delay ?: __CLASS__.'::exponentialDelay'; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Default exponential backoff delay function. |
|
| 52 | + * |
|
| 53 | + * @return int milliseconds. |
|
| 54 | + */ |
|
| 55 | + public static function exponentialDelay(int $retries): int |
|
| 56 | + { |
|
| 57 | + return (int) 2 ** ($retries - 1) * 1000; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 61 | + { |
|
| 62 | + if (!isset($options['retries'])) { |
|
| 63 | + $options['retries'] = 0; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $fn = $this->nextHandler; |
|
| 67 | + |
|
| 68 | + return $fn($request, $options) |
|
| 69 | + ->then( |
|
| 70 | + $this->onFulfilled($request, $options), |
|
| 71 | + $this->onRejected($request, $options) |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Execute fulfilled closure |
|
| 77 | + */ |
|
| 78 | + private function onFulfilled(RequestInterface $request, array $options): callable |
|
| 79 | + { |
|
| 80 | + return function ($value) use ($request, $options) { |
|
| 81 | + if (!($this->decider)( |
|
| 82 | + $options['retries'], |
|
| 83 | + $request, |
|
| 84 | + $value, |
|
| 85 | + null |
|
| 86 | + )) { |
|
| 87 | + return $value; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $this->doRetry($request, $options, $value); |
|
| 91 | + }; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Execute rejected closure |
|
| 96 | + */ |
|
| 97 | + private function onRejected(RequestInterface $req, array $options): callable |
|
| 98 | + { |
|
| 99 | + return function ($reason) use ($req, $options) { |
|
| 100 | + if (!($this->decider)( |
|
| 101 | + $options['retries'], |
|
| 102 | + $req, |
|
| 103 | + null, |
|
| 104 | + $reason |
|
| 105 | + )) { |
|
| 106 | + return P\Create::rejectionFor($reason); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $this->doRetry($req, $options); |
|
| 110 | + }; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null): PromiseInterface |
|
| 114 | + { |
|
| 115 | + $options['delay'] = ($this->delay)(++$options['retries'], $response, $request); |
|
| 116 | + |
|
| 117 | + return $this($request, $options); |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public static function exponentialDelay(int $retries): int |
| 56 | 56 | { |
| 57 | - return (int) 2 ** ($retries - 1) * 1000; |
|
| 57 | + return (int)2 ** ($retries - 1) * 1000; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | public function __invoke(RequestInterface $request, array $options): PromiseInterface |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | private function onFulfilled(RequestInterface $request, array $options): callable |
| 79 | 79 | { |
| 80 | - return function ($value) use ($request, $options) { |
|
| 80 | + return function($value) use ($request, $options) { |
|
| 81 | 81 | if (!($this->decider)( |
| 82 | 82 | $options['retries'], |
| 83 | 83 | $request, |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | */ |
| 97 | 97 | private function onRejected(RequestInterface $req, array $options): callable |
| 98 | 98 | { |
| 99 | - return function ($reason) use ($req, $options) { |
|
| 99 | + return function($reason) use ($req, $options) { |
|
| 100 | 100 | if (!($this->decider)( |
| 101 | 101 | $options['retries'], |
| 102 | 102 | $req, |
@@ -13,8 +13,7 @@ |
||
| 13 | 13 | * |
| 14 | 14 | * @final |
| 15 | 15 | */ |
| 16 | -class RetryMiddleware |
|
| 17 | -{ |
|
| 16 | +class RetryMiddleware { |
|
| 18 | 17 | /** |
| 19 | 18 | * @var callable(RequestInterface, array): PromiseInterface |
| 20 | 19 | */ |
@@ -7,12 +7,12 @@ |
||
| 7 | 7 | |
| 8 | 8 | interface MessageFormatterInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Returns a formatted message string. |
|
| 12 | - * |
|
| 13 | - * @param RequestInterface $request Request that was sent |
|
| 14 | - * @param ResponseInterface|null $response Response that was received |
|
| 15 | - * @param \Throwable|null $error Exception that was received |
|
| 16 | - */ |
|
| 17 | - public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string; |
|
| 10 | + /** |
|
| 11 | + * Returns a formatted message string. |
|
| 12 | + * |
|
| 13 | + * @param RequestInterface $request Request that was sent |
|
| 14 | + * @param ResponseInterface|null $response Response that was received |
|
| 15 | + * @param \Throwable|null $error Exception that was received |
|
| 16 | + */ |
|
| 17 | + public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string; |
|
| 18 | 18 | } |
@@ -5,8 +5,7 @@ |
||
| 5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
| 6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
| 7 | 7 | |
| 8 | -interface MessageFormatterInterface |
|
| 9 | -{ |
|
| 8 | +interface MessageFormatterInterface { |
|
| 10 | 9 | /** |
| 11 | 10 | * Returns a formatted message string. |
| 12 | 11 | * |
@@ -23,103 +23,103 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Pool implements PromisorInterface |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var EachPromise |
|
| 28 | - */ |
|
| 29 | - private $each; |
|
| 26 | + /** |
|
| 27 | + * @var EachPromise |
|
| 28 | + */ |
|
| 29 | + private $each; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param ClientInterface $client Client used to send the requests. |
|
| 33 | - * @param array|\Iterator $requests Requests or functions that return |
|
| 34 | - * requests to send concurrently. |
|
| 35 | - * @param array $config Associative array of options |
|
| 36 | - * - concurrency: (int) Maximum number of requests to send concurrently |
|
| 37 | - * - options: Array of request options to apply to each request. |
|
| 38 | - * - fulfilled: (callable) Function to invoke when a request completes. |
|
| 39 | - * - rejected: (callable) Function to invoke when a request is rejected. |
|
| 40 | - */ |
|
| 41 | - public function __construct(ClientInterface $client, $requests, array $config = []) |
|
| 42 | - { |
|
| 43 | - if (!isset($config['concurrency'])) { |
|
| 44 | - $config['concurrency'] = 25; |
|
| 45 | - } |
|
| 31 | + /** |
|
| 32 | + * @param ClientInterface $client Client used to send the requests. |
|
| 33 | + * @param array|\Iterator $requests Requests or functions that return |
|
| 34 | + * requests to send concurrently. |
|
| 35 | + * @param array $config Associative array of options |
|
| 36 | + * - concurrency: (int) Maximum number of requests to send concurrently |
|
| 37 | + * - options: Array of request options to apply to each request. |
|
| 38 | + * - fulfilled: (callable) Function to invoke when a request completes. |
|
| 39 | + * - rejected: (callable) Function to invoke when a request is rejected. |
|
| 40 | + */ |
|
| 41 | + public function __construct(ClientInterface $client, $requests, array $config = []) |
|
| 42 | + { |
|
| 43 | + if (!isset($config['concurrency'])) { |
|
| 44 | + $config['concurrency'] = 25; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if (isset($config['options'])) { |
|
| 48 | - $opts = $config['options']; |
|
| 49 | - unset($config['options']); |
|
| 50 | - } else { |
|
| 51 | - $opts = []; |
|
| 52 | - } |
|
| 47 | + if (isset($config['options'])) { |
|
| 48 | + $opts = $config['options']; |
|
| 49 | + unset($config['options']); |
|
| 50 | + } else { |
|
| 51 | + $opts = []; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - $iterable = P\Create::iterFor($requests); |
|
| 55 | - $requests = static function () use ($iterable, $client, $opts) { |
|
| 56 | - foreach ($iterable as $key => $rfn) { |
|
| 57 | - if ($rfn instanceof RequestInterface) { |
|
| 58 | - yield $key => $client->sendAsync($rfn, $opts); |
|
| 59 | - } elseif (\is_callable($rfn)) { |
|
| 60 | - yield $key => $rfn($opts); |
|
| 61 | - } else { |
|
| 62 | - throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr7\Http\Message\RequestInterface or a callable that returns a promise that fulfills with a Psr7\Message\Http\ResponseInterface object.'); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - }; |
|
| 54 | + $iterable = P\Create::iterFor($requests); |
|
| 55 | + $requests = static function () use ($iterable, $client, $opts) { |
|
| 56 | + foreach ($iterable as $key => $rfn) { |
|
| 57 | + if ($rfn instanceof RequestInterface) { |
|
| 58 | + yield $key => $client->sendAsync($rfn, $opts); |
|
| 59 | + } elseif (\is_callable($rfn)) { |
|
| 60 | + yield $key => $rfn($opts); |
|
| 61 | + } else { |
|
| 62 | + throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr7\Http\Message\RequestInterface or a callable that returns a promise that fulfills with a Psr7\Message\Http\ResponseInterface object.'); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + }; |
|
| 66 | 66 | |
| 67 | - $this->each = new EachPromise($requests(), $config); |
|
| 68 | - } |
|
| 67 | + $this->each = new EachPromise($requests(), $config); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Get promise |
|
| 72 | - */ |
|
| 73 | - public function promise(): PromiseInterface |
|
| 74 | - { |
|
| 75 | - return $this->each->promise(); |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * Get promise |
|
| 72 | + */ |
|
| 73 | + public function promise(): PromiseInterface |
|
| 74 | + { |
|
| 75 | + return $this->each->promise(); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Sends multiple requests concurrently and returns an array of responses |
|
| 80 | - * and exceptions that uses the same ordering as the provided requests. |
|
| 81 | - * |
|
| 82 | - * IMPORTANT: This method keeps every request and response in memory, and |
|
| 83 | - * as such, is NOT recommended when sending a large number or an |
|
| 84 | - * indeterminate number of requests concurrently. |
|
| 85 | - * |
|
| 86 | - * @param ClientInterface $client Client used to send the requests |
|
| 87 | - * @param array|\Iterator $requests Requests to send concurrently. |
|
| 88 | - * @param array $options Passes through the options available in |
|
| 89 | - * {@see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Pool::__construct} |
|
| 90 | - * |
|
| 91 | - * @return array Returns an array containing the response or an exception |
|
| 92 | - * in the same order that the requests were sent. |
|
| 93 | - * |
|
| 94 | - * @throws \InvalidArgumentException if the event format is incorrect. |
|
| 95 | - */ |
|
| 96 | - public static function batch(ClientInterface $client, $requests, array $options = []): array |
|
| 97 | - { |
|
| 98 | - $res = []; |
|
| 99 | - self::cmpCallback($options, 'fulfilled', $res); |
|
| 100 | - self::cmpCallback($options, 'rejected', $res); |
|
| 101 | - $pool = new static($client, $requests, $options); |
|
| 102 | - $pool->promise()->wait(); |
|
| 103 | - \ksort($res); |
|
| 78 | + /** |
|
| 79 | + * Sends multiple requests concurrently and returns an array of responses |
|
| 80 | + * and exceptions that uses the same ordering as the provided requests. |
|
| 81 | + * |
|
| 82 | + * IMPORTANT: This method keeps every request and response in memory, and |
|
| 83 | + * as such, is NOT recommended when sending a large number or an |
|
| 84 | + * indeterminate number of requests concurrently. |
|
| 85 | + * |
|
| 86 | + * @param ClientInterface $client Client used to send the requests |
|
| 87 | + * @param array|\Iterator $requests Requests to send concurrently. |
|
| 88 | + * @param array $options Passes through the options available in |
|
| 89 | + * {@see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Pool::__construct} |
|
| 90 | + * |
|
| 91 | + * @return array Returns an array containing the response or an exception |
|
| 92 | + * in the same order that the requests were sent. |
|
| 93 | + * |
|
| 94 | + * @throws \InvalidArgumentException if the event format is incorrect. |
|
| 95 | + */ |
|
| 96 | + public static function batch(ClientInterface $client, $requests, array $options = []): array |
|
| 97 | + { |
|
| 98 | + $res = []; |
|
| 99 | + self::cmpCallback($options, 'fulfilled', $res); |
|
| 100 | + self::cmpCallback($options, 'rejected', $res); |
|
| 101 | + $pool = new static($client, $requests, $options); |
|
| 102 | + $pool->promise()->wait(); |
|
| 103 | + \ksort($res); |
|
| 104 | 104 | |
| 105 | - return $res; |
|
| 106 | - } |
|
| 105 | + return $res; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Execute callback(s) |
|
| 110 | - */ |
|
| 111 | - private static function cmpCallback(array &$options, string $name, array &$results): void |
|
| 112 | - { |
|
| 113 | - if (!isset($options[$name])) { |
|
| 114 | - $options[$name] = static function ($v, $k) use (&$results) { |
|
| 115 | - $results[$k] = $v; |
|
| 116 | - }; |
|
| 117 | - } else { |
|
| 118 | - $currentFn = $options[$name]; |
|
| 119 | - $options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
|
| 120 | - $currentFn($v, $k); |
|
| 121 | - $results[$k] = $v; |
|
| 122 | - }; |
|
| 123 | - } |
|
| 124 | - } |
|
| 108 | + /** |
|
| 109 | + * Execute callback(s) |
|
| 110 | + */ |
|
| 111 | + private static function cmpCallback(array &$options, string $name, array &$results): void |
|
| 112 | + { |
|
| 113 | + if (!isset($options[$name])) { |
|
| 114 | + $options[$name] = static function ($v, $k) use (&$results) { |
|
| 115 | + $results[$k] = $v; |
|
| 116 | + }; |
|
| 117 | + } else { |
|
| 118 | + $currentFn = $options[$name]; |
|
| 119 | + $options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
|
| 120 | + $currentFn($v, $k); |
|
| 121 | + $results[$k] = $v; |
|
| 122 | + }; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $iterable = P\Create::iterFor($requests); |
| 55 | - $requests = static function () use ($iterable, $client, $opts) { |
|
| 55 | + $requests = static function() use ($iterable, $client, $opts) { |
|
| 56 | 56 | foreach ($iterable as $key => $rfn) { |
| 57 | 57 | if ($rfn instanceof RequestInterface) { |
| 58 | 58 | yield $key => $client->sendAsync($rfn, $opts); |
@@ -111,12 +111,12 @@ discard block |
||
| 111 | 111 | private static function cmpCallback(array &$options, string $name, array &$results): void |
| 112 | 112 | { |
| 113 | 113 | if (!isset($options[$name])) { |
| 114 | - $options[$name] = static function ($v, $k) use (&$results) { |
|
| 114 | + $options[$name] = static function($v, $k) use (&$results) { |
|
| 115 | 115 | $results[$k] = $v; |
| 116 | 116 | }; |
| 117 | 117 | } else { |
| 118 | 118 | $currentFn = $options[$name]; |
| 119 | - $options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
|
| 119 | + $options[$name] = static function($v, $k) use (&$results, $currentFn) { |
|
| 120 | 120 | $currentFn($v, $k); |
| 121 | 121 | $results[$k] = $v; |
| 122 | 122 | }; |
@@ -21,8 +21,7 @@ |
||
| 21 | 21 | * |
| 22 | 22 | * @final |
| 23 | 23 | */ |
| 24 | -class Pool implements PromisorInterface |
|
| 25 | -{ |
|
| 24 | +class Pool implements PromisorInterface { |
|
| 26 | 25 | /** |
| 27 | 26 | * @var EachPromise |
| 28 | 27 | */ |