@@ -16,7 +16,6 @@ |
||
| 16 | 16 | |
| 17 | 17 | use Exception; |
| 18 | 18 | |
| 19 | -class ConfigException extends Exception implements ElasticsearchException |
|
| 20 | -{ |
|
| 19 | +class ConfigException extends Exception implements ElasticsearchException { |
|
| 21 | 20 | |
| 22 | 21 | } |
| 23 | 22 | \ No newline at end of file |
@@ -16,7 +16,6 @@ |
||
| 16 | 16 | |
| 17 | 17 | use InvalidArgumentException as InvalidArgument; |
| 18 | 18 | |
| 19 | -class InvalidArgumentException extends InvalidArgument implements ElasticsearchException |
|
| 20 | -{ |
|
| 19 | +class InvalidArgumentException extends InvalidArgument implements ElasticsearchException { |
|
| 21 | 20 | |
| 22 | 21 | } |
| 23 | 22 | \ No newline at end of file |
@@ -27,149 +27,149 @@ |
||
| 27 | 27 | |
| 28 | 28 | final class Client implements ClientInterface |
| 29 | 29 | { |
| 30 | - const CLIENT_NAME = 'es'; |
|
| 31 | - const VERSION = '8.13.0'; |
|
| 32 | - const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8'; |
|
| 30 | + const CLIENT_NAME = 'es'; |
|
| 31 | + const VERSION = '8.13.0'; |
|
| 32 | + const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8'; |
|
| 33 | 33 | |
| 34 | - use ClientEndpointsTrait; |
|
| 35 | - use EndpointTrait; |
|
| 36 | - use NamespaceTrait; |
|
| 34 | + use ClientEndpointsTrait; |
|
| 35 | + use EndpointTrait; |
|
| 36 | + use NamespaceTrait; |
|
| 37 | 37 | |
| 38 | - protected Transport $transport; |
|
| 39 | - protected LoggerInterface $logger; |
|
| 38 | + protected Transport $transport; |
|
| 39 | + protected LoggerInterface $logger; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Specify is the request is asyncronous |
|
| 43 | - */ |
|
| 44 | - protected bool $async = false; |
|
| 41 | + /** |
|
| 42 | + * Specify is the request is asyncronous |
|
| 43 | + */ |
|
| 44 | + protected bool $async = false; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Enable or disable the x-elastic-meta-header |
|
| 48 | - */ |
|
| 49 | - protected bool $elasticMetaHeader = true; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Enable or disable the response Exception |
|
| 53 | - */ |
|
| 54 | - protected bool $responseException = true; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The endpoint namespace storage |
|
| 58 | - */ |
|
| 59 | - protected array $namespace; |
|
| 60 | - |
|
| 61 | - public function __construct( |
|
| 62 | - Transport $transport, |
|
| 63 | - LoggerInterface $logger |
|
| 64 | - ) { |
|
| 65 | - $this->transport = $transport; |
|
| 66 | - $this->logger = $logger; |
|
| 46 | + /** |
|
| 47 | + * Enable or disable the x-elastic-meta-header |
|
| 48 | + */ |
|
| 49 | + protected bool $elasticMetaHeader = true; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Enable or disable the response Exception |
|
| 53 | + */ |
|
| 54 | + protected bool $responseException = true; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The endpoint namespace storage |
|
| 58 | + */ |
|
| 59 | + protected array $namespace; |
|
| 60 | + |
|
| 61 | + public function __construct( |
|
| 62 | + Transport $transport, |
|
| 63 | + LoggerInterface $logger |
|
| 64 | + ) { |
|
| 65 | + $this->transport = $transport; |
|
| 66 | + $this->logger = $logger; |
|
| 67 | 67 | |
| 68 | - $this->defaultTransportSettings($this->transport); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @inheritdoc |
|
| 73 | - */ |
|
| 74 | - public function getTransport(): Transport |
|
| 75 | - { |
|
| 76 | - return $this->transport; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @inheritdoc |
|
| 81 | - */ |
|
| 82 | - public function getLogger(): LoggerInterface |
|
| 83 | - { |
|
| 84 | - return $this->logger; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Set the default settings for Elasticsearch |
|
| 89 | - */ |
|
| 90 | - protected function defaultTransportSettings(Transport $transport): void |
|
| 91 | - { |
|
| 92 | - $transport->setUserAgent('elasticsearch-php', self::VERSION); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @inheritdoc |
|
| 97 | - */ |
|
| 98 | - public function setAsync(bool $async): self |
|
| 99 | - { |
|
| 100 | - $this->async = $async; |
|
| 101 | - return $this; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @inheritdoc |
|
| 106 | - */ |
|
| 107 | - public function getAsync(): bool |
|
| 108 | - { |
|
| 109 | - return $this->async; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @inheritdoc |
|
| 114 | - */ |
|
| 115 | - public function setElasticMetaHeader(bool $active): self |
|
| 116 | - { |
|
| 117 | - $this->elasticMetaHeader = $active; |
|
| 118 | - return $this; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @inheritdoc |
|
| 123 | - */ |
|
| 124 | - public function getElasticMetaHeader(): bool |
|
| 125 | - { |
|
| 126 | - return $this->elasticMetaHeader; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @inheritdoc |
|
| 131 | - */ |
|
| 132 | - public function setResponseException(bool $active): self |
|
| 133 | - { |
|
| 134 | - $this->responseException = $active; |
|
| 135 | - return $this; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @inheritdoc |
|
| 140 | - */ |
|
| 141 | - public function getResponseException(): bool |
|
| 142 | - { |
|
| 143 | - return $this->responseException; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @inheritdoc |
|
| 148 | - */ |
|
| 149 | - public function sendRequest(RequestInterface $request) |
|
| 150 | - { |
|
| 151 | - // If async returns a Promise |
|
| 152 | - if ($this->getAsync()) { |
|
| 153 | - if ($this->getElasticMetaHeader()) { |
|
| 154 | - $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, true); |
|
| 155 | - } |
|
| 156 | - $this->transport->setAsyncOnSuccess( |
|
| 157 | - $request->getMethod() === 'HEAD' |
|
| 158 | - ? new AsyncOnSuccessNoException |
|
| 159 | - : ($this->getResponseException() ? new AsyncOnSuccess : new AsyncOnSuccessNoException) |
|
| 160 | - ); |
|
| 161 | - return $this->transport->sendAsyncRequest($request); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if ($this->getElasticMetaHeader()) { |
|
| 165 | - $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, false); |
|
| 166 | - } |
|
| 167 | - $start = microtime(true); |
|
| 168 | - $response = $this->transport->sendRequest($request); |
|
| 169 | - $this->logger->info(sprintf("Response time in %.3f sec", microtime(true) - $start)); |
|
| 170 | - |
|
| 171 | - $result = new Elasticsearch; |
|
| 172 | - $result->setResponse($response, $request->getMethod() === 'HEAD' ? false : $this->getResponseException()); |
|
| 173 | - return $result; |
|
| 174 | - } |
|
| 68 | + $this->defaultTransportSettings($this->transport); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @inheritdoc |
|
| 73 | + */ |
|
| 74 | + public function getTransport(): Transport |
|
| 75 | + { |
|
| 76 | + return $this->transport; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @inheritdoc |
|
| 81 | + */ |
|
| 82 | + public function getLogger(): LoggerInterface |
|
| 83 | + { |
|
| 84 | + return $this->logger; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Set the default settings for Elasticsearch |
|
| 89 | + */ |
|
| 90 | + protected function defaultTransportSettings(Transport $transport): void |
|
| 91 | + { |
|
| 92 | + $transport->setUserAgent('elasticsearch-php', self::VERSION); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @inheritdoc |
|
| 97 | + */ |
|
| 98 | + public function setAsync(bool $async): self |
|
| 99 | + { |
|
| 100 | + $this->async = $async; |
|
| 101 | + return $this; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @inheritdoc |
|
| 106 | + */ |
|
| 107 | + public function getAsync(): bool |
|
| 108 | + { |
|
| 109 | + return $this->async; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @inheritdoc |
|
| 114 | + */ |
|
| 115 | + public function setElasticMetaHeader(bool $active): self |
|
| 116 | + { |
|
| 117 | + $this->elasticMetaHeader = $active; |
|
| 118 | + return $this; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @inheritdoc |
|
| 123 | + */ |
|
| 124 | + public function getElasticMetaHeader(): bool |
|
| 125 | + { |
|
| 126 | + return $this->elasticMetaHeader; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @inheritdoc |
|
| 131 | + */ |
|
| 132 | + public function setResponseException(bool $active): self |
|
| 133 | + { |
|
| 134 | + $this->responseException = $active; |
|
| 135 | + return $this; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @inheritdoc |
|
| 140 | + */ |
|
| 141 | + public function getResponseException(): bool |
|
| 142 | + { |
|
| 143 | + return $this->responseException; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @inheritdoc |
|
| 148 | + */ |
|
| 149 | + public function sendRequest(RequestInterface $request) |
|
| 150 | + { |
|
| 151 | + // If async returns a Promise |
|
| 152 | + if ($this->getAsync()) { |
|
| 153 | + if ($this->getElasticMetaHeader()) { |
|
| 154 | + $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, true); |
|
| 155 | + } |
|
| 156 | + $this->transport->setAsyncOnSuccess( |
|
| 157 | + $request->getMethod() === 'HEAD' |
|
| 158 | + ? new AsyncOnSuccessNoException |
|
| 159 | + : ($this->getResponseException() ? new AsyncOnSuccess : new AsyncOnSuccessNoException) |
|
| 160 | + ); |
|
| 161 | + return $this->transport->sendAsyncRequest($request); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if ($this->getElasticMetaHeader()) { |
|
| 165 | + $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, false); |
|
| 166 | + } |
|
| 167 | + $start = microtime(true); |
|
| 168 | + $response = $this->transport->sendRequest($request); |
|
| 169 | + $this->logger->info(sprintf("Response time in %.3f sec", microtime(true) - $start)); |
|
| 170 | + |
|
| 171 | + $result = new Elasticsearch; |
|
| 172 | + $result->setResponse($response, $request->getMethod() === 'HEAD' ? false : $this->getResponseException()); |
|
| 173 | + return $result; |
|
| 174 | + } |
|
| 175 | 175 | } |
| 176 | 176 | \ No newline at end of file |
@@ -10,7 +10,7 @@ |
||
| 10 | 10 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 11 | 11 | * See the LICENSE file in the project root for more information. |
| 12 | 12 | */ |
| 13 | -declare(strict_types = 1); |
|
| 13 | +declare(strict_types=1); |
|
| 14 | 14 | |
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch; |
| 16 | 16 | |
@@ -25,8 +25,7 @@ |
||
| 25 | 25 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
| 26 | 26 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Log\LoggerInterface; |
| 27 | 27 | |
| 28 | -final class Client implements ClientInterface |
|
| 29 | -{ |
|
| 28 | +final class Client implements ClientInterface { |
|
| 30 | 29 | const CLIENT_NAME = 'es'; |
| 31 | 30 | const VERSION = '8.13.0'; |
| 32 | 31 | const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8'; |
@@ -22,145 +22,145 @@ |
||
| 22 | 22 | class SearchHitIterator implements Iterator, Countable |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var SearchResponseIterator |
|
| 27 | - */ |
|
| 28 | - private SearchResponseIterator $searchResponses; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var int |
|
| 32 | - */ |
|
| 33 | - protected int $currentKey; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var int |
|
| 37 | - */ |
|
| 38 | - protected int $currentHitIndex; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var array|null |
|
| 42 | - */ |
|
| 43 | - protected ?array $currentHitData; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var int |
|
| 47 | - */ |
|
| 48 | - protected int $count = 0; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Constructor |
|
| 52 | - * |
|
| 53 | - * @param SearchResponseIterator $searchResponses |
|
| 54 | - */ |
|
| 55 | - public function __construct(SearchResponseIterator $searchResponses) |
|
| 56 | - { |
|
| 57 | - $this->searchResponses = $searchResponses; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Rewinds the internal SearchResponseIterator and itself |
|
| 62 | - * |
|
| 63 | - * @return void |
|
| 64 | - * @throws ClientResponseException |
|
| 65 | - * @throws ServerResponseException |
|
| 66 | - * @see Iterator::rewind() |
|
| 67 | - */ |
|
| 68 | - public function rewind(): void |
|
| 69 | - { |
|
| 70 | - $this->currentKey = 0; |
|
| 71 | - $this->searchResponses->rewind(); |
|
| 72 | - |
|
| 73 | - // The first page may be empty. In that case, the next page is fetched. |
|
| 74 | - $currentPage = $this->searchResponses->current(); |
|
| 75 | - if ($this->searchResponses->valid() && empty($currentPage['hits']['hits'])) { |
|
| 76 | - $this->searchResponses->next(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $this->count = 0; |
|
| 80 | - if (isset($currentPage['hits']['total']['value'], $currentPage['hits']['total'])) { |
|
| 81 | - $this->count = $currentPage['hits']['total']['value'] ?? $currentPage['hits']['total']; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $this->readPageData(); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Advances pointer of the current hit to the next one in the current page. If there |
|
| 89 | - * isn't a next hit in the current page, then it advances the current page and moves the |
|
| 90 | - * pointer to the first hit in the page. |
|
| 91 | - * |
|
| 92 | - * @return void |
|
| 93 | - * @throws ClientResponseException |
|
| 94 | - * @throws ServerResponseException |
|
| 95 | - * @see Iterator::next() |
|
| 96 | - */ |
|
| 97 | - public function next(): void |
|
| 98 | - { |
|
| 99 | - $this->currentKey++; |
|
| 100 | - $this->currentHitIndex++; |
|
| 101 | - $currentPage = $this->searchResponses->current(); |
|
| 102 | - if (isset($currentPage['hits']['hits'][$this->currentHitIndex])) { |
|
| 103 | - $this->currentHitData = $currentPage['hits']['hits'][$this->currentHitIndex]; |
|
| 104 | - } else { |
|
| 105 | - $this->searchResponses->next(); |
|
| 106 | - $this->readPageData(); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Returns a boolean indicating whether or not the current pointer has valid data |
|
| 112 | - * |
|
| 113 | - * @return bool |
|
| 114 | - * @see Iterator::valid() |
|
| 115 | - */ |
|
| 116 | - public function valid(): bool |
|
| 117 | - { |
|
| 118 | - return is_array($this->currentHitData); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Returns the current hit |
|
| 123 | - * |
|
| 124 | - * @return array |
|
| 125 | - * @see Iterator::current() |
|
| 126 | - */ |
|
| 127 | - public function current(): array |
|
| 128 | - { |
|
| 129 | - return $this->currentHitData; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Returns the current hit index. The hit index spans all pages. |
|
| 134 | - * |
|
| 135 | - * @return int |
|
| 136 | - * @see Iterator::key() |
|
| 137 | - */ |
|
| 138 | - public function key(): int |
|
| 139 | - { |
|
| 140 | - return $this->currentKey; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Advances the internal SearchResponseIterator and resets the currentHitIndex to 0 |
|
| 145 | - * |
|
| 146 | - * @internal |
|
| 147 | - */ |
|
| 148 | - private function readPageData(): void |
|
| 149 | - { |
|
| 150 | - if ($this->searchResponses->valid()) { |
|
| 151 | - $currentPage = $this->searchResponses->current(); |
|
| 152 | - $this->currentHitIndex = 0; |
|
| 153 | - $this->currentHitData = $currentPage['hits']['hits'][$this->currentHitIndex]; |
|
| 154 | - } else { |
|
| 155 | - $this->currentHitData = null; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * {@inheritDoc} |
|
| 161 | - */ |
|
| 162 | - public function count(): int |
|
| 163 | - { |
|
| 164 | - return $this->count; |
|
| 165 | - } |
|
| 25 | + /** |
|
| 26 | + * @var SearchResponseIterator |
|
| 27 | + */ |
|
| 28 | + private SearchResponseIterator $searchResponses; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var int |
|
| 32 | + */ |
|
| 33 | + protected int $currentKey; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var int |
|
| 37 | + */ |
|
| 38 | + protected int $currentHitIndex; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var array|null |
|
| 42 | + */ |
|
| 43 | + protected ?array $currentHitData; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var int |
|
| 47 | + */ |
|
| 48 | + protected int $count = 0; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Constructor |
|
| 52 | + * |
|
| 53 | + * @param SearchResponseIterator $searchResponses |
|
| 54 | + */ |
|
| 55 | + public function __construct(SearchResponseIterator $searchResponses) |
|
| 56 | + { |
|
| 57 | + $this->searchResponses = $searchResponses; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Rewinds the internal SearchResponseIterator and itself |
|
| 62 | + * |
|
| 63 | + * @return void |
|
| 64 | + * @throws ClientResponseException |
|
| 65 | + * @throws ServerResponseException |
|
| 66 | + * @see Iterator::rewind() |
|
| 67 | + */ |
|
| 68 | + public function rewind(): void |
|
| 69 | + { |
|
| 70 | + $this->currentKey = 0; |
|
| 71 | + $this->searchResponses->rewind(); |
|
| 72 | + |
|
| 73 | + // The first page may be empty. In that case, the next page is fetched. |
|
| 74 | + $currentPage = $this->searchResponses->current(); |
|
| 75 | + if ($this->searchResponses->valid() && empty($currentPage['hits']['hits'])) { |
|
| 76 | + $this->searchResponses->next(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $this->count = 0; |
|
| 80 | + if (isset($currentPage['hits']['total']['value'], $currentPage['hits']['total'])) { |
|
| 81 | + $this->count = $currentPage['hits']['total']['value'] ?? $currentPage['hits']['total']; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $this->readPageData(); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Advances pointer of the current hit to the next one in the current page. If there |
|
| 89 | + * isn't a next hit in the current page, then it advances the current page and moves the |
|
| 90 | + * pointer to the first hit in the page. |
|
| 91 | + * |
|
| 92 | + * @return void |
|
| 93 | + * @throws ClientResponseException |
|
| 94 | + * @throws ServerResponseException |
|
| 95 | + * @see Iterator::next() |
|
| 96 | + */ |
|
| 97 | + public function next(): void |
|
| 98 | + { |
|
| 99 | + $this->currentKey++; |
|
| 100 | + $this->currentHitIndex++; |
|
| 101 | + $currentPage = $this->searchResponses->current(); |
|
| 102 | + if (isset($currentPage['hits']['hits'][$this->currentHitIndex])) { |
|
| 103 | + $this->currentHitData = $currentPage['hits']['hits'][$this->currentHitIndex]; |
|
| 104 | + } else { |
|
| 105 | + $this->searchResponses->next(); |
|
| 106 | + $this->readPageData(); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Returns a boolean indicating whether or not the current pointer has valid data |
|
| 112 | + * |
|
| 113 | + * @return bool |
|
| 114 | + * @see Iterator::valid() |
|
| 115 | + */ |
|
| 116 | + public function valid(): bool |
|
| 117 | + { |
|
| 118 | + return is_array($this->currentHitData); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Returns the current hit |
|
| 123 | + * |
|
| 124 | + * @return array |
|
| 125 | + * @see Iterator::current() |
|
| 126 | + */ |
|
| 127 | + public function current(): array |
|
| 128 | + { |
|
| 129 | + return $this->currentHitData; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Returns the current hit index. The hit index spans all pages. |
|
| 134 | + * |
|
| 135 | + * @return int |
|
| 136 | + * @see Iterator::key() |
|
| 137 | + */ |
|
| 138 | + public function key(): int |
|
| 139 | + { |
|
| 140 | + return $this->currentKey; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Advances the internal SearchResponseIterator and resets the currentHitIndex to 0 |
|
| 145 | + * |
|
| 146 | + * @internal |
|
| 147 | + */ |
|
| 148 | + private function readPageData(): void |
|
| 149 | + { |
|
| 150 | + if ($this->searchResponses->valid()) { |
|
| 151 | + $currentPage = $this->searchResponses->current(); |
|
| 152 | + $this->currentHitIndex = 0; |
|
| 153 | + $this->currentHitData = $currentPage['hits']['hits'][$this->currentHitIndex]; |
|
| 154 | + } else { |
|
| 155 | + $this->currentHitData = null; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * {@inheritDoc} |
|
| 161 | + */ |
|
| 162 | + public function count(): int |
|
| 163 | + { |
|
| 164 | + return $this->count; |
|
| 165 | + } |
|
| 166 | 166 | } |
| 167 | 167 | \ No newline at end of file |
@@ -10,7 +10,7 @@ |
||
| 10 | 10 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 11 | 11 | * See the LICENSE file in the project root for more information. |
| 12 | 12 | */ |
| 13 | -declare(strict_types = 1); |
|
| 13 | +declare(strict_types=1); |
|
| 14 | 14 | |
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Helper\Iterators; |
| 16 | 16 | |
@@ -22,171 +22,171 @@ |
||
| 22 | 22 | class SearchResponseIterator implements Iterator |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var ClientInterface |
|
| 27 | - */ |
|
| 28 | - private ClientInterface $client; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private array $params; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var int |
|
| 37 | - */ |
|
| 38 | - private int $currentKey = 0; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - private array $currentScrolledResponse; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var string|null |
|
| 47 | - */ |
|
| 48 | - private ?string $scrollId; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var string duration |
|
| 52 | - */ |
|
| 53 | - private $scrollTtl; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Constructor |
|
| 57 | - * |
|
| 58 | - * @param ClientInterface $client |
|
| 59 | - * @param array $searchParams Associative array of parameters |
|
| 60 | - * @see ClientInterface::search() |
|
| 61 | - */ |
|
| 62 | - public function __construct(ClientInterface $client, array $searchParams) |
|
| 63 | - { |
|
| 64 | - $this->client = $client; |
|
| 65 | - $this->params = $searchParams; |
|
| 66 | - |
|
| 67 | - if (isset($searchParams['scroll'])) { |
|
| 68 | - $this->scrollTtl = $searchParams['scroll']; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Destructor |
|
| 74 | - * |
|
| 75 | - * @throws ClientResponseException |
|
| 76 | - * @throws ServerResponseException |
|
| 77 | - */ |
|
| 78 | - public function __destruct() |
|
| 79 | - { |
|
| 80 | - $this->clearScroll(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Sets the time to live duration of a scroll window |
|
| 85 | - * |
|
| 86 | - * @param string $timeToLive |
|
| 87 | - * @return $this |
|
| 88 | - */ |
|
| 89 | - public function setScrollTimeout(string $timeToLive): SearchResponseIterator |
|
| 90 | - { |
|
| 91 | - $this->scrollTtl = $timeToLive; |
|
| 92 | - return $this; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Clears the current scroll window if there is a scroll_id stored |
|
| 97 | - * |
|
| 98 | - * @return void |
|
| 99 | - * @throws ClientResponseException |
|
| 100 | - * @throws ServerResponseException |
|
| 101 | - */ |
|
| 102 | - private function clearScroll(): void |
|
| 103 | - { |
|
| 104 | - if (!empty($this->scrollId)) { |
|
| 105 | - /* @phpstan-ignore-next-line */ |
|
| 106 | - $this->client->clearScroll( |
|
| 107 | - [ |
|
| 108 | - 'body' => [ |
|
| 109 | - 'scroll_id' => $this->scrollId |
|
| 110 | - ], |
|
| 111 | - 'client' => [ |
|
| 112 | - 'ignore' => 404 |
|
| 113 | - ] |
|
| 114 | - ] |
|
| 115 | - ); |
|
| 116 | - $this->scrollId = null; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Rewinds the iterator by performing the initial search. |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - * @throws ClientResponseException |
|
| 125 | - * @throws ServerResponseException |
|
| 126 | - * @see Iterator::rewind() |
|
| 127 | - */ |
|
| 128 | - public function rewind(): void |
|
| 129 | - { |
|
| 130 | - $this->clearScroll(); |
|
| 131 | - $this->currentKey = 0; |
|
| 132 | - /* @phpstan-ignore-next-line */ |
|
| 133 | - $this->currentScrolledResponse = $this->client->search($this->params)->asArray(); |
|
| 134 | - $this->scrollId = $this->currentScrolledResponse['_scroll_id']; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Fetches every "page" after the first one using the lastest "scroll_id" |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - * @throws ClientResponseException |
|
| 142 | - * @throws ServerResponseException |
|
| 143 | - * @see Iterator::next() |
|
| 144 | - */ |
|
| 145 | - public function next(): void |
|
| 146 | - { |
|
| 147 | - /* @phpstan-ignore-next-line */ |
|
| 148 | - $this->currentScrolledResponse = $this->client->scroll( |
|
| 149 | - [ |
|
| 150 | - 'body' => [ |
|
| 151 | - 'scroll_id' => $this->scrollId, |
|
| 152 | - 'scroll' => $this->scrollTtl |
|
| 153 | - ] |
|
| 154 | - ] |
|
| 155 | - )->asArray(); |
|
| 156 | - $this->scrollId = $this->currentScrolledResponse['_scroll_id']; |
|
| 157 | - $this->currentKey++; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Returns a boolean value indicating if the current page is valid or not |
|
| 162 | - * |
|
| 163 | - * @return bool |
|
| 164 | - * @see Iterator::valid() |
|
| 165 | - */ |
|
| 166 | - public function valid(): bool |
|
| 167 | - { |
|
| 168 | - return isset($this->currentScrolledResponse['hits']['hits'][0]); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Returns the current "page" |
|
| 173 | - * |
|
| 174 | - * @return array |
|
| 175 | - * @see Iterator::current() |
|
| 176 | - */ |
|
| 177 | - public function current(): array |
|
| 178 | - { |
|
| 179 | - return $this->currentScrolledResponse; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Returns the current "page number" of the current "page" |
|
| 184 | - * |
|
| 185 | - * @return int |
|
| 186 | - * @see Iterator::key() |
|
| 187 | - */ |
|
| 188 | - public function key(): int |
|
| 189 | - { |
|
| 190 | - return $this->currentKey; |
|
| 191 | - } |
|
| 25 | + /** |
|
| 26 | + * @var ClientInterface |
|
| 27 | + */ |
|
| 28 | + private ClientInterface $client; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private array $params; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var int |
|
| 37 | + */ |
|
| 38 | + private int $currentKey = 0; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + private array $currentScrolledResponse; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var string|null |
|
| 47 | + */ |
|
| 48 | + private ?string $scrollId; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var string duration |
|
| 52 | + */ |
|
| 53 | + private $scrollTtl; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Constructor |
|
| 57 | + * |
|
| 58 | + * @param ClientInterface $client |
|
| 59 | + * @param array $searchParams Associative array of parameters |
|
| 60 | + * @see ClientInterface::search() |
|
| 61 | + */ |
|
| 62 | + public function __construct(ClientInterface $client, array $searchParams) |
|
| 63 | + { |
|
| 64 | + $this->client = $client; |
|
| 65 | + $this->params = $searchParams; |
|
| 66 | + |
|
| 67 | + if (isset($searchParams['scroll'])) { |
|
| 68 | + $this->scrollTtl = $searchParams['scroll']; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Destructor |
|
| 74 | + * |
|
| 75 | + * @throws ClientResponseException |
|
| 76 | + * @throws ServerResponseException |
|
| 77 | + */ |
|
| 78 | + public function __destruct() |
|
| 79 | + { |
|
| 80 | + $this->clearScroll(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Sets the time to live duration of a scroll window |
|
| 85 | + * |
|
| 86 | + * @param string $timeToLive |
|
| 87 | + * @return $this |
|
| 88 | + */ |
|
| 89 | + public function setScrollTimeout(string $timeToLive): SearchResponseIterator |
|
| 90 | + { |
|
| 91 | + $this->scrollTtl = $timeToLive; |
|
| 92 | + return $this; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Clears the current scroll window if there is a scroll_id stored |
|
| 97 | + * |
|
| 98 | + * @return void |
|
| 99 | + * @throws ClientResponseException |
|
| 100 | + * @throws ServerResponseException |
|
| 101 | + */ |
|
| 102 | + private function clearScroll(): void |
|
| 103 | + { |
|
| 104 | + if (!empty($this->scrollId)) { |
|
| 105 | + /* @phpstan-ignore-next-line */ |
|
| 106 | + $this->client->clearScroll( |
|
| 107 | + [ |
|
| 108 | + 'body' => [ |
|
| 109 | + 'scroll_id' => $this->scrollId |
|
| 110 | + ], |
|
| 111 | + 'client' => [ |
|
| 112 | + 'ignore' => 404 |
|
| 113 | + ] |
|
| 114 | + ] |
|
| 115 | + ); |
|
| 116 | + $this->scrollId = null; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Rewinds the iterator by performing the initial search. |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + * @throws ClientResponseException |
|
| 125 | + * @throws ServerResponseException |
|
| 126 | + * @see Iterator::rewind() |
|
| 127 | + */ |
|
| 128 | + public function rewind(): void |
|
| 129 | + { |
|
| 130 | + $this->clearScroll(); |
|
| 131 | + $this->currentKey = 0; |
|
| 132 | + /* @phpstan-ignore-next-line */ |
|
| 133 | + $this->currentScrolledResponse = $this->client->search($this->params)->asArray(); |
|
| 134 | + $this->scrollId = $this->currentScrolledResponse['_scroll_id']; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Fetches every "page" after the first one using the lastest "scroll_id" |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + * @throws ClientResponseException |
|
| 142 | + * @throws ServerResponseException |
|
| 143 | + * @see Iterator::next() |
|
| 144 | + */ |
|
| 145 | + public function next(): void |
|
| 146 | + { |
|
| 147 | + /* @phpstan-ignore-next-line */ |
|
| 148 | + $this->currentScrolledResponse = $this->client->scroll( |
|
| 149 | + [ |
|
| 150 | + 'body' => [ |
|
| 151 | + 'scroll_id' => $this->scrollId, |
|
| 152 | + 'scroll' => $this->scrollTtl |
|
| 153 | + ] |
|
| 154 | + ] |
|
| 155 | + )->asArray(); |
|
| 156 | + $this->scrollId = $this->currentScrolledResponse['_scroll_id']; |
|
| 157 | + $this->currentKey++; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Returns a boolean value indicating if the current page is valid or not |
|
| 162 | + * |
|
| 163 | + * @return bool |
|
| 164 | + * @see Iterator::valid() |
|
| 165 | + */ |
|
| 166 | + public function valid(): bool |
|
| 167 | + { |
|
| 168 | + return isset($this->currentScrolledResponse['hits']['hits'][0]); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Returns the current "page" |
|
| 173 | + * |
|
| 174 | + * @return array |
|
| 175 | + * @see Iterator::current() |
|
| 176 | + */ |
|
| 177 | + public function current(): array |
|
| 178 | + { |
|
| 179 | + return $this->currentScrolledResponse; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Returns the current "page number" of the current "page" |
|
| 184 | + * |
|
| 185 | + * @return int |
|
| 186 | + * @see Iterator::key() |
|
| 187 | + */ |
|
| 188 | + public function key(): int |
|
| 189 | + { |
|
| 190 | + return $this->currentKey; |
|
| 191 | + } |
|
| 192 | 192 | } |
| 193 | 193 | \ No newline at end of file |
@@ -10,7 +10,7 @@ |
||
| 10 | 10 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 11 | 11 | * See the LICENSE file in the project root for more information. |
| 12 | 12 | */ |
| 13 | -declare(strict_types = 1); |
|
| 13 | +declare(strict_types=1); |
|
| 14 | 14 | |
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Helper\Iterators; |
| 16 | 16 | |
@@ -19,8 +19,7 @@ |
||
| 19 | 19 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception\ServerResponseException; |
| 20 | 20 | use Iterator; |
| 21 | 21 | |
| 22 | -class SearchResponseIterator implements Iterator |
|
| 23 | -{ |
|
| 22 | +class SearchResponseIterator implements Iterator { |
|
| 24 | 23 | |
| 25 | 24 | /** |
| 26 | 25 | * @var ClientInterface |
@@ -11,167 +11,167 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | final class Utils |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Debug function used to describe the provided value type and class. |
|
| 16 | - * |
|
| 17 | - * @param mixed $input |
|
| 18 | - * |
|
| 19 | - * @return string Returns a string containing the type of the variable and |
|
| 20 | - * if a class is provided, the class name. |
|
| 21 | - */ |
|
| 22 | - public static function describeType($input): string |
|
| 23 | - { |
|
| 24 | - switch (\gettype($input)) { |
|
| 25 | - case 'object': |
|
| 26 | - return 'object('.\get_class($input).')'; |
|
| 27 | - case 'array': |
|
| 28 | - return 'array('.\count($input).')'; |
|
| 29 | - default: |
|
| 30 | - \ob_start(); |
|
| 31 | - \var_dump($input); |
|
| 32 | - // normalize float vs double |
|
| 33 | - /** @var string $varDumpContent */ |
|
| 34 | - $varDumpContent = \ob_get_clean(); |
|
| 35 | - |
|
| 36 | - return \str_replace('double(', 'float(', \rtrim($varDumpContent)); |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Parses an array of header lines into an associative array of headers. |
|
| 42 | - * |
|
| 43 | - * @param iterable $lines Header lines array of strings in the following |
|
| 44 | - * format: "Name: Value" |
|
| 45 | - */ |
|
| 46 | - public static function headersFromLines(iterable $lines): array |
|
| 47 | - { |
|
| 48 | - $headers = []; |
|
| 49 | - |
|
| 50 | - foreach ($lines as $line) { |
|
| 51 | - $parts = \explode(':', $line, 2); |
|
| 52 | - $headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - return $headers; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Returns a debug stream based on the provided variable. |
|
| 60 | - * |
|
| 61 | - * @param mixed $value Optional value |
|
| 62 | - * |
|
| 63 | - * @return resource |
|
| 64 | - */ |
|
| 65 | - public static function debugResource($value = null) |
|
| 66 | - { |
|
| 67 | - if (\is_resource($value)) { |
|
| 68 | - return $value; |
|
| 69 | - } |
|
| 70 | - if (\defined('STDOUT')) { |
|
| 71 | - return \STDOUT; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::tryFopen('php://output', 'w'); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Chooses and creates a default handler to use based on the environment. |
|
| 79 | - * |
|
| 80 | - * The returned handler is not wrapped by any default middlewares. |
|
| 81 | - * |
|
| 82 | - * @return callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface Returns the best handler for the given system. |
|
| 83 | - * |
|
| 84 | - * @throws \RuntimeException if no viable Handler is available. |
|
| 85 | - */ |
|
| 86 | - public static function chooseHandler(): callable |
|
| 87 | - { |
|
| 88 | - $handler = null; |
|
| 89 | - |
|
| 90 | - if (\defined('CURLOPT_CUSTOMREQUEST')) { |
|
| 91 | - if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) { |
|
| 92 | - $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); |
|
| 93 | - } elseif (\function_exists('curl_exec')) { |
|
| 94 | - $handler = new CurlHandler(); |
|
| 95 | - } elseif (\function_exists('curl_multi_exec')) { |
|
| 96 | - $handler = new CurlMultiHandler(); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - if (\ini_get('allow_url_fopen')) { |
|
| 101 | - $handler = $handler |
|
| 102 | - ? Proxy::wrapStreaming($handler, new StreamHandler()) |
|
| 103 | - : new StreamHandler(); |
|
| 104 | - } elseif (!$handler) { |
|
| 105 | - throw new \RuntimeException('OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return $handler; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Get the default User-Agent string to use with Guzzle. |
|
| 113 | - */ |
|
| 114 | - public static function defaultUserAgent(): string |
|
| 115 | - { |
|
| 116 | - return sprintf('GuzzleHttp/%d', ClientInterface::MAJOR_VERSION); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Returns the default cacert bundle for the current system. |
|
| 121 | - * |
|
| 122 | - * First, the openssl.cafile and curl.cainfo php.ini settings are checked. |
|
| 123 | - * If those settings are not configured, then the common locations for |
|
| 124 | - * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X |
|
| 125 | - * and Windows are checked. If any of these file locations are found on |
|
| 126 | - * disk, they will be utilized. |
|
| 127 | - * |
|
| 128 | - * Note: the result of this function is cached for subsequent calls. |
|
| 129 | - * |
|
| 130 | - * @throws \RuntimeException if no bundle can be found. |
|
| 131 | - * |
|
| 132 | - * @deprecated Utils::defaultCaBundle will be removed in guzzlehttp/guzzle:8.0. This method is not needed in PHP 5.6+. |
|
| 133 | - */ |
|
| 134 | - public static function defaultCaBundle(): string |
|
| 135 | - { |
|
| 136 | - static $cached = null; |
|
| 137 | - static $cafiles = [ |
|
| 138 | - // Red Hat, CentOS, Fedora (provided by the ca-certificates package) |
|
| 139 | - '/etc/pki/tls/certs/ca-bundle.crt', |
|
| 140 | - // Ubuntu, Debian (provided by the ca-certificates package) |
|
| 141 | - '/etc/ssl/certs/ca-certificates.crt', |
|
| 142 | - // FreeBSD (provided by the ca_root_nss package) |
|
| 143 | - '/usr/local/share/certs/ca-root-nss.crt', |
|
| 144 | - // SLES 12 (provided by the ca-certificates package) |
|
| 145 | - '/var/lib/ca-certificates/ca-bundle.pem', |
|
| 146 | - // OS X provided by homebrew (using the default path) |
|
| 147 | - '/usr/local/etc/openssl/cert.pem', |
|
| 148 | - // Google app engine |
|
| 149 | - '/etc/ca-certificates.crt', |
|
| 150 | - // Windows? |
|
| 151 | - 'C:\\windows\\system32\\curl-ca-bundle.crt', |
|
| 152 | - 'C:\\windows\\curl-ca-bundle.crt', |
|
| 153 | - ]; |
|
| 154 | - |
|
| 155 | - if ($cached) { |
|
| 156 | - return $cached; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if ($ca = \ini_get('openssl.cafile')) { |
|
| 160 | - return $cached = $ca; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - if ($ca = \ini_get('curl.cainfo')) { |
|
| 164 | - return $cached = $ca; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - foreach ($cafiles as $filename) { |
|
| 168 | - if (\file_exists($filename)) { |
|
| 169 | - return $cached = $filename; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - throw new \RuntimeException( |
|
| 174 | - <<< EOT |
|
| 14 | + /** |
|
| 15 | + * Debug function used to describe the provided value type and class. |
|
| 16 | + * |
|
| 17 | + * @param mixed $input |
|
| 18 | + * |
|
| 19 | + * @return string Returns a string containing the type of the variable and |
|
| 20 | + * if a class is provided, the class name. |
|
| 21 | + */ |
|
| 22 | + public static function describeType($input): string |
|
| 23 | + { |
|
| 24 | + switch (\gettype($input)) { |
|
| 25 | + case 'object': |
|
| 26 | + return 'object('.\get_class($input).')'; |
|
| 27 | + case 'array': |
|
| 28 | + return 'array('.\count($input).')'; |
|
| 29 | + default: |
|
| 30 | + \ob_start(); |
|
| 31 | + \var_dump($input); |
|
| 32 | + // normalize float vs double |
|
| 33 | + /** @var string $varDumpContent */ |
|
| 34 | + $varDumpContent = \ob_get_clean(); |
|
| 35 | + |
|
| 36 | + return \str_replace('double(', 'float(', \rtrim($varDumpContent)); |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Parses an array of header lines into an associative array of headers. |
|
| 42 | + * |
|
| 43 | + * @param iterable $lines Header lines array of strings in the following |
|
| 44 | + * format: "Name: Value" |
|
| 45 | + */ |
|
| 46 | + public static function headersFromLines(iterable $lines): array |
|
| 47 | + { |
|
| 48 | + $headers = []; |
|
| 49 | + |
|
| 50 | + foreach ($lines as $line) { |
|
| 51 | + $parts = \explode(':', $line, 2); |
|
| 52 | + $headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + return $headers; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Returns a debug stream based on the provided variable. |
|
| 60 | + * |
|
| 61 | + * @param mixed $value Optional value |
|
| 62 | + * |
|
| 63 | + * @return resource |
|
| 64 | + */ |
|
| 65 | + public static function debugResource($value = null) |
|
| 66 | + { |
|
| 67 | + if (\is_resource($value)) { |
|
| 68 | + return $value; |
|
| 69 | + } |
|
| 70 | + if (\defined('STDOUT')) { |
|
| 71 | + return \STDOUT; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Utils::tryFopen('php://output', 'w'); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Chooses and creates a default handler to use based on the environment. |
|
| 79 | + * |
|
| 80 | + * The returned handler is not wrapped by any default middlewares. |
|
| 81 | + * |
|
| 82 | + * @return callable(\OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface, array): \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface Returns the best handler for the given system. |
|
| 83 | + * |
|
| 84 | + * @throws \RuntimeException if no viable Handler is available. |
|
| 85 | + */ |
|
| 86 | + public static function chooseHandler(): callable |
|
| 87 | + { |
|
| 88 | + $handler = null; |
|
| 89 | + |
|
| 90 | + if (\defined('CURLOPT_CUSTOMREQUEST')) { |
|
| 91 | + if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) { |
|
| 92 | + $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); |
|
| 93 | + } elseif (\function_exists('curl_exec')) { |
|
| 94 | + $handler = new CurlHandler(); |
|
| 95 | + } elseif (\function_exists('curl_multi_exec')) { |
|
| 96 | + $handler = new CurlMultiHandler(); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + if (\ini_get('allow_url_fopen')) { |
|
| 101 | + $handler = $handler |
|
| 102 | + ? Proxy::wrapStreaming($handler, new StreamHandler()) |
|
| 103 | + : new StreamHandler(); |
|
| 104 | + } elseif (!$handler) { |
|
| 105 | + throw new \RuntimeException('OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.'); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return $handler; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Get the default User-Agent string to use with Guzzle. |
|
| 113 | + */ |
|
| 114 | + public static function defaultUserAgent(): string |
|
| 115 | + { |
|
| 116 | + return sprintf('GuzzleHttp/%d', ClientInterface::MAJOR_VERSION); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Returns the default cacert bundle for the current system. |
|
| 121 | + * |
|
| 122 | + * First, the openssl.cafile and curl.cainfo php.ini settings are checked. |
|
| 123 | + * If those settings are not configured, then the common locations for |
|
| 124 | + * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X |
|
| 125 | + * and Windows are checked. If any of these file locations are found on |
|
| 126 | + * disk, they will be utilized. |
|
| 127 | + * |
|
| 128 | + * Note: the result of this function is cached for subsequent calls. |
|
| 129 | + * |
|
| 130 | + * @throws \RuntimeException if no bundle can be found. |
|
| 131 | + * |
|
| 132 | + * @deprecated Utils::defaultCaBundle will be removed in guzzlehttp/guzzle:8.0. This method is not needed in PHP 5.6+. |
|
| 133 | + */ |
|
| 134 | + public static function defaultCaBundle(): string |
|
| 135 | + { |
|
| 136 | + static $cached = null; |
|
| 137 | + static $cafiles = [ |
|
| 138 | + // Red Hat, CentOS, Fedora (provided by the ca-certificates package) |
|
| 139 | + '/etc/pki/tls/certs/ca-bundle.crt', |
|
| 140 | + // Ubuntu, Debian (provided by the ca-certificates package) |
|
| 141 | + '/etc/ssl/certs/ca-certificates.crt', |
|
| 142 | + // FreeBSD (provided by the ca_root_nss package) |
|
| 143 | + '/usr/local/share/certs/ca-root-nss.crt', |
|
| 144 | + // SLES 12 (provided by the ca-certificates package) |
|
| 145 | + '/var/lib/ca-certificates/ca-bundle.pem', |
|
| 146 | + // OS X provided by homebrew (using the default path) |
|
| 147 | + '/usr/local/etc/openssl/cert.pem', |
|
| 148 | + // Google app engine |
|
| 149 | + '/etc/ca-certificates.crt', |
|
| 150 | + // Windows? |
|
| 151 | + 'C:\\windows\\system32\\curl-ca-bundle.crt', |
|
| 152 | + 'C:\\windows\\curl-ca-bundle.crt', |
|
| 153 | + ]; |
|
| 154 | + |
|
| 155 | + if ($cached) { |
|
| 156 | + return $cached; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if ($ca = \ini_get('openssl.cafile')) { |
|
| 160 | + return $cached = $ca; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + if ($ca = \ini_get('curl.cainfo')) { |
|
| 164 | + return $cached = $ca; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + foreach ($cafiles as $filename) { |
|
| 168 | + if (\file_exists($filename)) { |
|
| 169 | + return $cached = $filename; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + throw new \RuntimeException( |
|
| 174 | + <<< EOT |
|
| 175 | 175 | No system CA bundle could be found in any of the the common system locations. |
| 176 | 176 | PHP versions earlier than 5.6 are not properly configured to use the system's |
| 177 | 177 | CA bundle by default. In order to verify peer certificates, you will need to |
@@ -184,201 +184,201 @@ discard block |
||
| 184 | 184 | to the file, allowing you to omit the 'verify' request option. See |
| 185 | 185 | https://curl.haxx.se/docs/sslcerts.html for more information. |
| 186 | 186 | EOT |
| 187 | - ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Creates an associative array of lowercase header names to the actual |
|
| 192 | - * header casing. |
|
| 193 | - */ |
|
| 194 | - public static function normalizeHeaderKeys(array $headers): array |
|
| 195 | - { |
|
| 196 | - $result = []; |
|
| 197 | - foreach (\array_keys($headers) as $key) { |
|
| 198 | - $result[\strtolower($key)] = $key; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return $result; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Returns true if the provided host matches any of the no proxy areas. |
|
| 206 | - * |
|
| 207 | - * This method will strip a port from the host if it is present. Each pattern |
|
| 208 | - * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a |
|
| 209 | - * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" == |
|
| 210 | - * "baz.foo.com", but ".foo.com" != "foo.com"). |
|
| 211 | - * |
|
| 212 | - * Areas are matched in the following cases: |
|
| 213 | - * 1. "*" (without quotes) always matches any hosts. |
|
| 214 | - * 2. An exact match. |
|
| 215 | - * 3. The area starts with "." and the area is the last part of the host. e.g. |
|
| 216 | - * '.mit.edu' will match any host that ends with '.mit.edu'. |
|
| 217 | - * |
|
| 218 | - * @param string $host Host to check against the patterns. |
|
| 219 | - * @param string[] $noProxyArray An array of host patterns. |
|
| 220 | - * |
|
| 221 | - * @throws InvalidArgumentException |
|
| 222 | - */ |
|
| 223 | - public static function isHostInNoProxy(string $host, array $noProxyArray): bool |
|
| 224 | - { |
|
| 225 | - if (\strlen($host) === 0) { |
|
| 226 | - throw new InvalidArgumentException('Empty host provided'); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // Strip port if present. |
|
| 230 | - [$host] = \explode(':', $host, 2); |
|
| 231 | - |
|
| 232 | - foreach ($noProxyArray as $area) { |
|
| 233 | - // Always match on wildcards. |
|
| 234 | - if ($area === '*') { |
|
| 235 | - return true; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - if (empty($area)) { |
|
| 239 | - // Don't match on empty values. |
|
| 240 | - continue; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - if ($area === $host) { |
|
| 244 | - // Exact matches. |
|
| 245 | - return true; |
|
| 246 | - } |
|
| 247 | - // Special match if the area when prefixed with ".". Remove any |
|
| 248 | - // existing leading "." and add a new leading ".". |
|
| 249 | - $area = '.'.\ltrim($area, '.'); |
|
| 250 | - if (\substr($host, -\strlen($area)) === $area) { |
|
| 251 | - return true; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Wrapper for json_decode that throws when an error occurs. |
|
| 260 | - * |
|
| 261 | - * @param string $json JSON data to parse |
|
| 262 | - * @param bool $assoc When true, returned objects will be converted |
|
| 263 | - * into associative arrays. |
|
| 264 | - * @param int $depth User specified recursion depth. |
|
| 265 | - * @param int $options Bitmask of JSON decode options. |
|
| 266 | - * |
|
| 267 | - * @return object|array|string|int|float|bool|null |
|
| 268 | - * |
|
| 269 | - * @throws InvalidArgumentException if the JSON cannot be decoded. |
|
| 270 | - * |
|
| 271 | - * @see https://www.php.net/manual/en/function.json-decode.php |
|
| 272 | - */ |
|
| 273 | - public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
|
| 274 | - { |
|
| 275 | - $data = \json_decode($json, $assoc, $depth, $options); |
|
| 276 | - if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 277 | - throw new InvalidArgumentException('json_decode error: '.\json_last_error_msg()); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return $data; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Wrapper for JSON encoding that throws when an error occurs. |
|
| 285 | - * |
|
| 286 | - * @param mixed $value The value being encoded |
|
| 287 | - * @param int $options JSON encode option bitmask |
|
| 288 | - * @param int $depth Set the maximum depth. Must be greater than zero. |
|
| 289 | - * |
|
| 290 | - * @throws InvalidArgumentException if the JSON cannot be encoded. |
|
| 291 | - * |
|
| 292 | - * @see https://www.php.net/manual/en/function.json-encode.php |
|
| 293 | - */ |
|
| 294 | - public static function jsonEncode($value, int $options = 0, int $depth = 512): string |
|
| 295 | - { |
|
| 296 | - $json = \json_encode($value, $options, $depth); |
|
| 297 | - if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 298 | - throw new InvalidArgumentException('json_encode error: '.\json_last_error_msg()); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** @var string */ |
|
| 302 | - return $json; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Wrapper for the hrtime() or microtime() functions |
|
| 307 | - * (depending on the PHP version, one of the two is used) |
|
| 308 | - * |
|
| 309 | - * @return float UNIX timestamp |
|
| 310 | - * |
|
| 311 | - * @internal |
|
| 312 | - */ |
|
| 313 | - public static function currentTime(): float |
|
| 314 | - { |
|
| 315 | - return (float) \function_exists('hrtime') ? \hrtime(true) / 1e9 : \microtime(true); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * @throws InvalidArgumentException |
|
| 320 | - * |
|
| 321 | - * @internal |
|
| 322 | - */ |
|
| 323 | - public static function idnUriConvert(UriInterface $uri, int $options = 0): UriInterface |
|
| 324 | - { |
|
| 325 | - if ($uri->getHost()) { |
|
| 326 | - $asciiHost = self::idnToAsci($uri->getHost(), $options, $info); |
|
| 327 | - if ($asciiHost === false) { |
|
| 328 | - $errorBitSet = $info['errors'] ?? 0; |
|
| 329 | - |
|
| 330 | - $errorConstants = array_filter(array_keys(get_defined_constants()), static function (string $name): bool { |
|
| 331 | - return substr($name, 0, 11) === 'IDNA_ERROR_'; |
|
| 332 | - }); |
|
| 333 | - |
|
| 334 | - $errors = []; |
|
| 335 | - foreach ($errorConstants as $errorConstant) { |
|
| 336 | - if ($errorBitSet & constant($errorConstant)) { |
|
| 337 | - $errors[] = $errorConstant; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $errorMessage = 'IDN conversion failed'; |
|
| 342 | - if ($errors) { |
|
| 343 | - $errorMessage .= ' (errors: '.implode(', ', $errors).')'; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - throw new InvalidArgumentException($errorMessage); |
|
| 347 | - } |
|
| 348 | - if ($uri->getHost() !== $asciiHost) { |
|
| 349 | - // Replace URI only if the ASCII version is different |
|
| 350 | - $uri = $uri->withHost($asciiHost); |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - return $uri; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * @internal |
|
| 359 | - */ |
|
| 360 | - public static function getenv(string $name): ?string |
|
| 361 | - { |
|
| 362 | - if (isset($_SERVER[$name])) { |
|
| 363 | - return (string) $_SERVER[$name]; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== false && $value !== null) { |
|
| 367 | - return (string) $value; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return null; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * @return string|false |
|
| 375 | - */ |
|
| 376 | - private static function idnToAsci(string $domain, int $options, ?array &$info = []) |
|
| 377 | - { |
|
| 378 | - if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) { |
|
| 379 | - return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - throw new \Error('ext-idn or symfony/polyfill-intl-idn not loaded or too old'); |
|
| 383 | - } |
|
| 187 | + ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Creates an associative array of lowercase header names to the actual |
|
| 192 | + * header casing. |
|
| 193 | + */ |
|
| 194 | + public static function normalizeHeaderKeys(array $headers): array |
|
| 195 | + { |
|
| 196 | + $result = []; |
|
| 197 | + foreach (\array_keys($headers) as $key) { |
|
| 198 | + $result[\strtolower($key)] = $key; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return $result; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Returns true if the provided host matches any of the no proxy areas. |
|
| 206 | + * |
|
| 207 | + * This method will strip a port from the host if it is present. Each pattern |
|
| 208 | + * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a |
|
| 209 | + * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" == |
|
| 210 | + * "baz.foo.com", but ".foo.com" != "foo.com"). |
|
| 211 | + * |
|
| 212 | + * Areas are matched in the following cases: |
|
| 213 | + * 1. "*" (without quotes) always matches any hosts. |
|
| 214 | + * 2. An exact match. |
|
| 215 | + * 3. The area starts with "." and the area is the last part of the host. e.g. |
|
| 216 | + * '.mit.edu' will match any host that ends with '.mit.edu'. |
|
| 217 | + * |
|
| 218 | + * @param string $host Host to check against the patterns. |
|
| 219 | + * @param string[] $noProxyArray An array of host patterns. |
|
| 220 | + * |
|
| 221 | + * @throws InvalidArgumentException |
|
| 222 | + */ |
|
| 223 | + public static function isHostInNoProxy(string $host, array $noProxyArray): bool |
|
| 224 | + { |
|
| 225 | + if (\strlen($host) === 0) { |
|
| 226 | + throw new InvalidArgumentException('Empty host provided'); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // Strip port if present. |
|
| 230 | + [$host] = \explode(':', $host, 2); |
|
| 231 | + |
|
| 232 | + foreach ($noProxyArray as $area) { |
|
| 233 | + // Always match on wildcards. |
|
| 234 | + if ($area === '*') { |
|
| 235 | + return true; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + if (empty($area)) { |
|
| 239 | + // Don't match on empty values. |
|
| 240 | + continue; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + if ($area === $host) { |
|
| 244 | + // Exact matches. |
|
| 245 | + return true; |
|
| 246 | + } |
|
| 247 | + // Special match if the area when prefixed with ".". Remove any |
|
| 248 | + // existing leading "." and add a new leading ".". |
|
| 249 | + $area = '.'.\ltrim($area, '.'); |
|
| 250 | + if (\substr($host, -\strlen($area)) === $area) { |
|
| 251 | + return true; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Wrapper for json_decode that throws when an error occurs. |
|
| 260 | + * |
|
| 261 | + * @param string $json JSON data to parse |
|
| 262 | + * @param bool $assoc When true, returned objects will be converted |
|
| 263 | + * into associative arrays. |
|
| 264 | + * @param int $depth User specified recursion depth. |
|
| 265 | + * @param int $options Bitmask of JSON decode options. |
|
| 266 | + * |
|
| 267 | + * @return object|array|string|int|float|bool|null |
|
| 268 | + * |
|
| 269 | + * @throws InvalidArgumentException if the JSON cannot be decoded. |
|
| 270 | + * |
|
| 271 | + * @see https://www.php.net/manual/en/function.json-decode.php |
|
| 272 | + */ |
|
| 273 | + public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
|
| 274 | + { |
|
| 275 | + $data = \json_decode($json, $assoc, $depth, $options); |
|
| 276 | + if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 277 | + throw new InvalidArgumentException('json_decode error: '.\json_last_error_msg()); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return $data; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Wrapper for JSON encoding that throws when an error occurs. |
|
| 285 | + * |
|
| 286 | + * @param mixed $value The value being encoded |
|
| 287 | + * @param int $options JSON encode option bitmask |
|
| 288 | + * @param int $depth Set the maximum depth. Must be greater than zero. |
|
| 289 | + * |
|
| 290 | + * @throws InvalidArgumentException if the JSON cannot be encoded. |
|
| 291 | + * |
|
| 292 | + * @see https://www.php.net/manual/en/function.json-encode.php |
|
| 293 | + */ |
|
| 294 | + public static function jsonEncode($value, int $options = 0, int $depth = 512): string |
|
| 295 | + { |
|
| 296 | + $json = \json_encode($value, $options, $depth); |
|
| 297 | + if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
| 298 | + throw new InvalidArgumentException('json_encode error: '.\json_last_error_msg()); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** @var string */ |
|
| 302 | + return $json; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Wrapper for the hrtime() or microtime() functions |
|
| 307 | + * (depending on the PHP version, one of the two is used) |
|
| 308 | + * |
|
| 309 | + * @return float UNIX timestamp |
|
| 310 | + * |
|
| 311 | + * @internal |
|
| 312 | + */ |
|
| 313 | + public static function currentTime(): float |
|
| 314 | + { |
|
| 315 | + return (float) \function_exists('hrtime') ? \hrtime(true) / 1e9 : \microtime(true); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * @throws InvalidArgumentException |
|
| 320 | + * |
|
| 321 | + * @internal |
|
| 322 | + */ |
|
| 323 | + public static function idnUriConvert(UriInterface $uri, int $options = 0): UriInterface |
|
| 324 | + { |
|
| 325 | + if ($uri->getHost()) { |
|
| 326 | + $asciiHost = self::idnToAsci($uri->getHost(), $options, $info); |
|
| 327 | + if ($asciiHost === false) { |
|
| 328 | + $errorBitSet = $info['errors'] ?? 0; |
|
| 329 | + |
|
| 330 | + $errorConstants = array_filter(array_keys(get_defined_constants()), static function (string $name): bool { |
|
| 331 | + return substr($name, 0, 11) === 'IDNA_ERROR_'; |
|
| 332 | + }); |
|
| 333 | + |
|
| 334 | + $errors = []; |
|
| 335 | + foreach ($errorConstants as $errorConstant) { |
|
| 336 | + if ($errorBitSet & constant($errorConstant)) { |
|
| 337 | + $errors[] = $errorConstant; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $errorMessage = 'IDN conversion failed'; |
|
| 342 | + if ($errors) { |
|
| 343 | + $errorMessage .= ' (errors: '.implode(', ', $errors).')'; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + throw new InvalidArgumentException($errorMessage); |
|
| 347 | + } |
|
| 348 | + if ($uri->getHost() !== $asciiHost) { |
|
| 349 | + // Replace URI only if the ASCII version is different |
|
| 350 | + $uri = $uri->withHost($asciiHost); |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + return $uri; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * @internal |
|
| 359 | + */ |
|
| 360 | + public static function getenv(string $name): ?string |
|
| 361 | + { |
|
| 362 | + if (isset($_SERVER[$name])) { |
|
| 363 | + return (string) $_SERVER[$name]; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== false && $value !== null) { |
|
| 367 | + return (string) $value; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return null; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * @return string|false |
|
| 375 | + */ |
|
| 376 | + private static function idnToAsci(string $domain, int $options, ?array &$info = []) |
|
| 377 | + { |
|
| 378 | + if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) { |
|
| 379 | + return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + throw new \Error('ext-idn or symfony/polyfill-intl-idn not loaded or too old'); |
|
| 383 | + } |
|
| 384 | 384 | } |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | */ |
| 313 | 313 | public static function currentTime(): float |
| 314 | 314 | { |
| 315 | - return (float) \function_exists('hrtime') ? \hrtime(true) / 1e9 : \microtime(true); |
|
| 315 | + return (float)\function_exists('hrtime') ? \hrtime(true) / 1e9 : \microtime(true); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | /** |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | if ($asciiHost === false) { |
| 328 | 328 | $errorBitSet = $info['errors'] ?? 0; |
| 329 | 329 | |
| 330 | - $errorConstants = array_filter(array_keys(get_defined_constants()), static function (string $name): bool { |
|
| 330 | + $errorConstants = array_filter(array_keys(get_defined_constants()), static function(string $name): bool { |
|
| 331 | 331 | return substr($name, 0, 11) === 'IDNA_ERROR_'; |
| 332 | 332 | }); |
| 333 | 333 | |
@@ -360,11 +360,11 @@ discard block |
||
| 360 | 360 | public static function getenv(string $name): ?string |
| 361 | 361 | { |
| 362 | 362 | if (isset($_SERVER[$name])) { |
| 363 | - return (string) $_SERVER[$name]; |
|
| 363 | + return (string)$_SERVER[$name]; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== false && $value !== null) { |
| 367 | - return (string) $value; |
|
| 367 | + return (string)$value; |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | return null; |
@@ -9,8 +9,7 @@ |
||
| 9 | 9 | use OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Handler\StreamHandler; |
| 10 | 10 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\UriInterface; |
| 11 | 11 | |
| 12 | -final class Utils |
|
| 13 | -{ |
|
| 12 | +final class Utils { |
|
| 14 | 13 | /** |
| 15 | 14 | * Debug function used to describe the provided value type and class. |
| 16 | 15 | * |
@@ -19,210 +19,210 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class RedirectMiddleware |
| 21 | 21 | { |
| 22 | - public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
|
| 23 | - |
|
| 24 | - public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - public static $defaultSettings = [ |
|
| 30 | - 'max' => 5, |
|
| 31 | - 'protocols' => ['http', 'https'], |
|
| 32 | - 'strict' => false, |
|
| 33 | - 'referer' => false, |
|
| 34 | - 'track_redirects' => false, |
|
| 35 | - ]; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 39 | - */ |
|
| 40 | - private $nextHandler; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 44 | - */ |
|
| 45 | - public function __construct(callable $nextHandler) |
|
| 46 | - { |
|
| 47 | - $this->nextHandler = $nextHandler; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 51 | - { |
|
| 52 | - $fn = $this->nextHandler; |
|
| 53 | - |
|
| 54 | - if (empty($options['allow_redirects'])) { |
|
| 55 | - return $fn($request, $options); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - if ($options['allow_redirects'] === true) { |
|
| 59 | - $options['allow_redirects'] = self::$defaultSettings; |
|
| 60 | - } elseif (!\is_array($options['allow_redirects'])) { |
|
| 61 | - throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); |
|
| 62 | - } else { |
|
| 63 | - // Merge the default settings with the provided settings |
|
| 64 | - $options['allow_redirects'] += self::$defaultSettings; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - if (empty($options['allow_redirects']['max'])) { |
|
| 68 | - return $fn($request, $options); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return $fn($request, $options) |
|
| 72 | - ->then(function (ResponseInterface $response) use ($request, $options) { |
|
| 73 | - return $this->checkRedirect($request, $options, $response); |
|
| 74 | - }); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @return ResponseInterface|PromiseInterface |
|
| 79 | - */ |
|
| 80 | - public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
|
| 81 | - { |
|
| 82 | - if (\strpos((string) $response->getStatusCode(), '3') !== 0 |
|
| 83 | - || !$response->hasHeader('Location') |
|
| 84 | - ) { |
|
| 85 | - return $response; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $this->guardMax($request, $response, $options); |
|
| 89 | - $nextRequest = $this->modifyRequest($request, $options, $response); |
|
| 90 | - |
|
| 91 | - // If authorization is handled by curl, unset it if URI is cross-origin. |
|
| 92 | - if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $nextRequest->getUri()) && defined('\CURLOPT_HTTPAUTH')) { |
|
| 93 | - unset( |
|
| 94 | - $options['curl'][\CURLOPT_HTTPAUTH], |
|
| 95 | - $options['curl'][\CURLOPT_USERPWD] |
|
| 96 | - ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if (isset($options['allow_redirects']['on_redirect'])) { |
|
| 100 | - ($options['allow_redirects']['on_redirect'])( |
|
| 101 | - $request, |
|
| 102 | - $response, |
|
| 103 | - $nextRequest->getUri() |
|
| 104 | - ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $promise = $this($nextRequest, $options); |
|
| 108 | - |
|
| 109 | - // Add headers to be able to track history of redirects. |
|
| 110 | - if (!empty($options['allow_redirects']['track_redirects'])) { |
|
| 111 | - return $this->withTracking( |
|
| 112 | - $promise, |
|
| 113 | - (string) $nextRequest->getUri(), |
|
| 114 | - $response->getStatusCode() |
|
| 115 | - ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return $promise; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Enable tracking on promise. |
|
| 123 | - */ |
|
| 124 | - private function withTracking(PromiseInterface $promise, string $uri, int $statusCode): PromiseInterface |
|
| 125 | - { |
|
| 126 | - return $promise->then( |
|
| 127 | - static function (ResponseInterface $response) use ($uri, $statusCode) { |
|
| 128 | - // Note that we are pushing to the front of the list as this |
|
| 129 | - // would be an earlier response than what is currently present |
|
| 130 | - // in the history header. |
|
| 131 | - $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
|
| 132 | - $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
|
| 133 | - \array_unshift($historyHeader, $uri); |
|
| 134 | - \array_unshift($statusHeader, (string) $statusCode); |
|
| 135 | - |
|
| 136 | - return $response->withHeader(self::HISTORY_HEADER, $historyHeader) |
|
| 137 | - ->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
|
| 138 | - } |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Check for too many redirects. |
|
| 144 | - * |
|
| 145 | - * @throws TooManyRedirectsException Too many redirects. |
|
| 146 | - */ |
|
| 147 | - private function guardMax(RequestInterface $request, ResponseInterface $response, array &$options): void |
|
| 148 | - { |
|
| 149 | - $current = $options['__redirect_count'] |
|
| 150 | - ?? 0; |
|
| 151 | - $options['__redirect_count'] = $current + 1; |
|
| 152 | - $max = $options['allow_redirects']['max']; |
|
| 153 | - |
|
| 154 | - if ($options['__redirect_count'] > $max) { |
|
| 155 | - throw new TooManyRedirectsException("Will not follow more than {$max} redirects", $request, $response); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response): RequestInterface |
|
| 160 | - { |
|
| 161 | - // Request modifications to apply. |
|
| 162 | - $modify = []; |
|
| 163 | - $protocols = $options['allow_redirects']['protocols']; |
|
| 164 | - |
|
| 165 | - // Use a GET request if this is an entity enclosing request and we are |
|
| 166 | - // not forcing RFC compliance, but rather emulating what all browsers |
|
| 167 | - // would do. |
|
| 168 | - $statusCode = $response->getStatusCode(); |
|
| 169 | - if ($statusCode == 303 |
|
| 170 | - || ($statusCode <= 302 && !$options['allow_redirects']['strict']) |
|
| 171 | - ) { |
|
| 172 | - $safeMethods = ['GET', 'HEAD', 'OPTIONS']; |
|
| 173 | - $requestMethod = $request->getMethod(); |
|
| 174 | - |
|
| 175 | - $modify['method'] = in_array($requestMethod, $safeMethods) ? $requestMethod : 'GET'; |
|
| 176 | - $modify['body'] = ''; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $uri = self::redirectUri($request, $response, $protocols); |
|
| 180 | - if (isset($options['idn_conversion']) && ($options['idn_conversion'] !== false)) { |
|
| 181 | - $idnOptions = ($options['idn_conversion'] === true) ? \IDNA_DEFAULT : $options['idn_conversion']; |
|
| 182 | - $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $modify['uri'] = $uri; |
|
| 186 | - Psr7\Message::rewindBody($request); |
|
| 187 | - |
|
| 188 | - // Add the Referer header if it is told to do so and only |
|
| 189 | - // add the header if we are not redirecting from https to http. |
|
| 190 | - if ($options['allow_redirects']['referer'] |
|
| 191 | - && $modify['uri']->getScheme() === $request->getUri()->getScheme() |
|
| 192 | - ) { |
|
| 193 | - $uri = $request->getUri()->withUserInfo(''); |
|
| 194 | - $modify['set_headers']['Referer'] = (string) $uri; |
|
| 195 | - } else { |
|
| 196 | - $modify['remove_headers'][] = 'Referer'; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - // Remove Authorization and Cookie headers if URI is cross-origin. |
|
| 200 | - if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $modify['uri'])) { |
|
| 201 | - $modify['remove_headers'][] = 'Authorization'; |
|
| 202 | - $modify['remove_headers'][] = 'Cookie'; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return Psr7\Utils::modifyRequest($request, $modify); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Set the appropriate URL on the request based on the location header. |
|
| 210 | - */ |
|
| 211 | - private static function redirectUri( |
|
| 212 | - RequestInterface $request, |
|
| 213 | - ResponseInterface $response, |
|
| 214 | - array $protocols |
|
| 215 | - ): UriInterface { |
|
| 216 | - $location = Psr7\UriResolver::resolve( |
|
| 217 | - $request->getUri(), |
|
| 218 | - new Psr7\Uri($response->getHeaderLine('Location')) |
|
| 219 | - ); |
|
| 220 | - |
|
| 221 | - // Ensure that the redirect URI is allowed based on the protocols. |
|
| 222 | - if (!\in_array($location->getScheme(), $protocols)) { |
|
| 223 | - throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols)), $request, $response); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - return $location; |
|
| 227 | - } |
|
| 22 | + public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
|
| 23 | + |
|
| 24 | + public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + public static $defaultSettings = [ |
|
| 30 | + 'max' => 5, |
|
| 31 | + 'protocols' => ['http', 'https'], |
|
| 32 | + 'strict' => false, |
|
| 33 | + 'referer' => false, |
|
| 34 | + 'track_redirects' => false, |
|
| 35 | + ]; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 39 | + */ |
|
| 40 | + private $nextHandler; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 44 | + */ |
|
| 45 | + public function __construct(callable $nextHandler) |
|
| 46 | + { |
|
| 47 | + $this->nextHandler = $nextHandler; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 51 | + { |
|
| 52 | + $fn = $this->nextHandler; |
|
| 53 | + |
|
| 54 | + if (empty($options['allow_redirects'])) { |
|
| 55 | + return $fn($request, $options); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + if ($options['allow_redirects'] === true) { |
|
| 59 | + $options['allow_redirects'] = self::$defaultSettings; |
|
| 60 | + } elseif (!\is_array($options['allow_redirects'])) { |
|
| 61 | + throw new \InvalidArgumentException('allow_redirects must be true, false, or array'); |
|
| 62 | + } else { |
|
| 63 | + // Merge the default settings with the provided settings |
|
| 64 | + $options['allow_redirects'] += self::$defaultSettings; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + if (empty($options['allow_redirects']['max'])) { |
|
| 68 | + return $fn($request, $options); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return $fn($request, $options) |
|
| 72 | + ->then(function (ResponseInterface $response) use ($request, $options) { |
|
| 73 | + return $this->checkRedirect($request, $options, $response); |
|
| 74 | + }); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @return ResponseInterface|PromiseInterface |
|
| 79 | + */ |
|
| 80 | + public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
|
| 81 | + { |
|
| 82 | + if (\strpos((string) $response->getStatusCode(), '3') !== 0 |
|
| 83 | + || !$response->hasHeader('Location') |
|
| 84 | + ) { |
|
| 85 | + return $response; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $this->guardMax($request, $response, $options); |
|
| 89 | + $nextRequest = $this->modifyRequest($request, $options, $response); |
|
| 90 | + |
|
| 91 | + // If authorization is handled by curl, unset it if URI is cross-origin. |
|
| 92 | + if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $nextRequest->getUri()) && defined('\CURLOPT_HTTPAUTH')) { |
|
| 93 | + unset( |
|
| 94 | + $options['curl'][\CURLOPT_HTTPAUTH], |
|
| 95 | + $options['curl'][\CURLOPT_USERPWD] |
|
| 96 | + ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if (isset($options['allow_redirects']['on_redirect'])) { |
|
| 100 | + ($options['allow_redirects']['on_redirect'])( |
|
| 101 | + $request, |
|
| 102 | + $response, |
|
| 103 | + $nextRequest->getUri() |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $promise = $this($nextRequest, $options); |
|
| 108 | + |
|
| 109 | + // Add headers to be able to track history of redirects. |
|
| 110 | + if (!empty($options['allow_redirects']['track_redirects'])) { |
|
| 111 | + return $this->withTracking( |
|
| 112 | + $promise, |
|
| 113 | + (string) $nextRequest->getUri(), |
|
| 114 | + $response->getStatusCode() |
|
| 115 | + ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return $promise; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Enable tracking on promise. |
|
| 123 | + */ |
|
| 124 | + private function withTracking(PromiseInterface $promise, string $uri, int $statusCode): PromiseInterface |
|
| 125 | + { |
|
| 126 | + return $promise->then( |
|
| 127 | + static function (ResponseInterface $response) use ($uri, $statusCode) { |
|
| 128 | + // Note that we are pushing to the front of the list as this |
|
| 129 | + // would be an earlier response than what is currently present |
|
| 130 | + // in the history header. |
|
| 131 | + $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
|
| 132 | + $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
|
| 133 | + \array_unshift($historyHeader, $uri); |
|
| 134 | + \array_unshift($statusHeader, (string) $statusCode); |
|
| 135 | + |
|
| 136 | + return $response->withHeader(self::HISTORY_HEADER, $historyHeader) |
|
| 137 | + ->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
|
| 138 | + } |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Check for too many redirects. |
|
| 144 | + * |
|
| 145 | + * @throws TooManyRedirectsException Too many redirects. |
|
| 146 | + */ |
|
| 147 | + private function guardMax(RequestInterface $request, ResponseInterface $response, array &$options): void |
|
| 148 | + { |
|
| 149 | + $current = $options['__redirect_count'] |
|
| 150 | + ?? 0; |
|
| 151 | + $options['__redirect_count'] = $current + 1; |
|
| 152 | + $max = $options['allow_redirects']['max']; |
|
| 153 | + |
|
| 154 | + if ($options['__redirect_count'] > $max) { |
|
| 155 | + throw new TooManyRedirectsException("Will not follow more than {$max} redirects", $request, $response); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response): RequestInterface |
|
| 160 | + { |
|
| 161 | + // Request modifications to apply. |
|
| 162 | + $modify = []; |
|
| 163 | + $protocols = $options['allow_redirects']['protocols']; |
|
| 164 | + |
|
| 165 | + // Use a GET request if this is an entity enclosing request and we are |
|
| 166 | + // not forcing RFC compliance, but rather emulating what all browsers |
|
| 167 | + // would do. |
|
| 168 | + $statusCode = $response->getStatusCode(); |
|
| 169 | + if ($statusCode == 303 |
|
| 170 | + || ($statusCode <= 302 && !$options['allow_redirects']['strict']) |
|
| 171 | + ) { |
|
| 172 | + $safeMethods = ['GET', 'HEAD', 'OPTIONS']; |
|
| 173 | + $requestMethod = $request->getMethod(); |
|
| 174 | + |
|
| 175 | + $modify['method'] = in_array($requestMethod, $safeMethods) ? $requestMethod : 'GET'; |
|
| 176 | + $modify['body'] = ''; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $uri = self::redirectUri($request, $response, $protocols); |
|
| 180 | + if (isset($options['idn_conversion']) && ($options['idn_conversion'] !== false)) { |
|
| 181 | + $idnOptions = ($options['idn_conversion'] === true) ? \IDNA_DEFAULT : $options['idn_conversion']; |
|
| 182 | + $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $modify['uri'] = $uri; |
|
| 186 | + Psr7\Message::rewindBody($request); |
|
| 187 | + |
|
| 188 | + // Add the Referer header if it is told to do so and only |
|
| 189 | + // add the header if we are not redirecting from https to http. |
|
| 190 | + if ($options['allow_redirects']['referer'] |
|
| 191 | + && $modify['uri']->getScheme() === $request->getUri()->getScheme() |
|
| 192 | + ) { |
|
| 193 | + $uri = $request->getUri()->withUserInfo(''); |
|
| 194 | + $modify['set_headers']['Referer'] = (string) $uri; |
|
| 195 | + } else { |
|
| 196 | + $modify['remove_headers'][] = 'Referer'; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + // Remove Authorization and Cookie headers if URI is cross-origin. |
|
| 200 | + if (Psr7\UriComparator::isCrossOrigin($request->getUri(), $modify['uri'])) { |
|
| 201 | + $modify['remove_headers'][] = 'Authorization'; |
|
| 202 | + $modify['remove_headers'][] = 'Cookie'; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return Psr7\Utils::modifyRequest($request, $modify); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Set the appropriate URL on the request based on the location header. |
|
| 210 | + */ |
|
| 211 | + private static function redirectUri( |
|
| 212 | + RequestInterface $request, |
|
| 213 | + ResponseInterface $response, |
|
| 214 | + array $protocols |
|
| 215 | + ): UriInterface { |
|
| 216 | + $location = Psr7\UriResolver::resolve( |
|
| 217 | + $request->getUri(), |
|
| 218 | + new Psr7\Uri($response->getHeaderLine('Location')) |
|
| 219 | + ); |
|
| 220 | + |
|
| 221 | + // Ensure that the redirect URI is allowed based on the protocols. |
|
| 222 | + if (!\in_array($location->getScheme(), $protocols)) { |
|
| 223 | + throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols)), $request, $response); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + return $location; |
|
| 227 | + } |
|
| 228 | 228 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return $fn($request, $options) |
| 72 | - ->then(function (ResponseInterface $response) use ($request, $options) { |
|
| 72 | + ->then(function(ResponseInterface $response) use ($request, $options) { |
|
| 73 | 73 | return $this->checkRedirect($request, $options, $response); |
| 74 | 74 | }); |
| 75 | 75 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | public function checkRedirect(RequestInterface $request, array $options, ResponseInterface $response) |
| 81 | 81 | { |
| 82 | - if (\strpos((string) $response->getStatusCode(), '3') !== 0 |
|
| 82 | + if (\strpos((string)$response->getStatusCode(), '3') !== 0 |
|
| 83 | 83 | || !$response->hasHeader('Location') |
| 84 | 84 | ) { |
| 85 | 85 | return $response; |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | if (!empty($options['allow_redirects']['track_redirects'])) { |
| 111 | 111 | return $this->withTracking( |
| 112 | 112 | $promise, |
| 113 | - (string) $nextRequest->getUri(), |
|
| 113 | + (string)$nextRequest->getUri(), |
|
| 114 | 114 | $response->getStatusCode() |
| 115 | 115 | ); |
| 116 | 116 | } |
@@ -124,14 +124,14 @@ discard block |
||
| 124 | 124 | private function withTracking(PromiseInterface $promise, string $uri, int $statusCode): PromiseInterface |
| 125 | 125 | { |
| 126 | 126 | return $promise->then( |
| 127 | - static function (ResponseInterface $response) use ($uri, $statusCode) { |
|
| 127 | + static function(ResponseInterface $response) use ($uri, $statusCode) { |
|
| 128 | 128 | // Note that we are pushing to the front of the list as this |
| 129 | 129 | // would be an earlier response than what is currently present |
| 130 | 130 | // in the history header. |
| 131 | 131 | $historyHeader = $response->getHeader(self::HISTORY_HEADER); |
| 132 | 132 | $statusHeader = $response->getHeader(self::STATUS_HISTORY_HEADER); |
| 133 | 133 | \array_unshift($historyHeader, $uri); |
| 134 | - \array_unshift($statusHeader, (string) $statusCode); |
|
| 134 | + \array_unshift($statusHeader, (string)$statusCode); |
|
| 135 | 135 | |
| 136 | 136 | return $response->withHeader(self::HISTORY_HEADER, $historyHeader) |
| 137 | 137 | ->withHeader(self::STATUS_HISTORY_HEADER, $statusHeader); |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | && $modify['uri']->getScheme() === $request->getUri()->getScheme() |
| 192 | 192 | ) { |
| 193 | 193 | $uri = $request->getUri()->withUserInfo(''); |
| 194 | - $modify['set_headers']['Referer'] = (string) $uri; |
|
| 194 | + $modify['set_headers']['Referer'] = (string)$uri; |
|
| 195 | 195 | } else { |
| 196 | 196 | $modify['remove_headers'][] = 'Referer'; |
| 197 | 197 | } |
@@ -17,8 +17,7 @@ |
||
| 17 | 17 | * |
| 18 | 18 | * @final |
| 19 | 19 | */ |
| 20 | -class RedirectMiddleware |
|
| 21 | -{ |
|
| 20 | +class RedirectMiddleware { |
|
| 22 | 21 | public const HISTORY_HEADER = 'X-Guzzle-Redirect-History'; |
| 23 | 22 | |
| 24 | 23 | public const STATUS_HISTORY_HEADER = 'X-Guzzle-Redirect-Status-History'; |
@@ -13,93 +13,93 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class PrepareBodyMiddleware |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 18 | - */ |
|
| 19 | - private $nextHandler; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 23 | - */ |
|
| 24 | - public function __construct(callable $nextHandler) |
|
| 25 | - { |
|
| 26 | - $this->nextHandler = $nextHandler; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 30 | - { |
|
| 31 | - $fn = $this->nextHandler; |
|
| 32 | - |
|
| 33 | - // Don't do anything if the request has no body. |
|
| 34 | - if ($request->getBody()->getSize() === 0) { |
|
| 35 | - return $fn($request, $options); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - $modify = []; |
|
| 39 | - |
|
| 40 | - // Add a default content-type if possible. |
|
| 41 | - if (!$request->hasHeader('Content-Type')) { |
|
| 42 | - if ($uri = $request->getBody()->getMetadata('uri')) { |
|
| 43 | - if (is_string($uri) && $type = Psr7\MimeType::fromFilename($uri)) { |
|
| 44 | - $modify['set_headers']['Content-Type'] = $type; |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - // Add a default content-length or transfer-encoding header. |
|
| 50 | - if (!$request->hasHeader('Content-Length') |
|
| 51 | - && !$request->hasHeader('Transfer-Encoding') |
|
| 52 | - ) { |
|
| 53 | - $size = $request->getBody()->getSize(); |
|
| 54 | - if ($size !== null) { |
|
| 55 | - $modify['set_headers']['Content-Length'] = $size; |
|
| 56 | - } else { |
|
| 57 | - $modify['set_headers']['Transfer-Encoding'] = 'chunked'; |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // Add the expect header if needed. |
|
| 62 | - $this->addExpectHeader($request, $options, $modify); |
|
| 63 | - |
|
| 64 | - return $fn(Psr7\Utils::modifyRequest($request, $modify), $options); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Add expect header |
|
| 69 | - */ |
|
| 70 | - private function addExpectHeader(RequestInterface $request, array $options, array &$modify): void |
|
| 71 | - { |
|
| 72 | - // Determine if the Expect header should be used |
|
| 73 | - if ($request->hasHeader('Expect')) { |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $expect = $options['expect'] ?? null; |
|
| 78 | - |
|
| 79 | - // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0 |
|
| 80 | - if ($expect === false || $request->getProtocolVersion() < 1.1) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // The expect header is unconditionally enabled |
|
| 85 | - if ($expect === true) { |
|
| 86 | - $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 87 | - |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // By default, send the expect header when the payload is > 1mb |
|
| 92 | - if ($expect === null) { |
|
| 93 | - $expect = 1048576; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Always add if the body cannot be rewound, the size cannot be |
|
| 97 | - // determined, or the size is greater than the cutoff threshold |
|
| 98 | - $body = $request->getBody(); |
|
| 99 | - $size = $body->getSize(); |
|
| 100 | - |
|
| 101 | - if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { |
|
| 102 | - $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 103 | - } |
|
| 104 | - } |
|
| 16 | + /** |
|
| 17 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 18 | + */ |
|
| 19 | + private $nextHandler; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 23 | + */ |
|
| 24 | + public function __construct(callable $nextHandler) |
|
| 25 | + { |
|
| 26 | + $this->nextHandler = $nextHandler; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 30 | + { |
|
| 31 | + $fn = $this->nextHandler; |
|
| 32 | + |
|
| 33 | + // Don't do anything if the request has no body. |
|
| 34 | + if ($request->getBody()->getSize() === 0) { |
|
| 35 | + return $fn($request, $options); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + $modify = []; |
|
| 39 | + |
|
| 40 | + // Add a default content-type if possible. |
|
| 41 | + if (!$request->hasHeader('Content-Type')) { |
|
| 42 | + if ($uri = $request->getBody()->getMetadata('uri')) { |
|
| 43 | + if (is_string($uri) && $type = Psr7\MimeType::fromFilename($uri)) { |
|
| 44 | + $modify['set_headers']['Content-Type'] = $type; |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + // Add a default content-length or transfer-encoding header. |
|
| 50 | + if (!$request->hasHeader('Content-Length') |
|
| 51 | + && !$request->hasHeader('Transfer-Encoding') |
|
| 52 | + ) { |
|
| 53 | + $size = $request->getBody()->getSize(); |
|
| 54 | + if ($size !== null) { |
|
| 55 | + $modify['set_headers']['Content-Length'] = $size; |
|
| 56 | + } else { |
|
| 57 | + $modify['set_headers']['Transfer-Encoding'] = 'chunked'; |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // Add the expect header if needed. |
|
| 62 | + $this->addExpectHeader($request, $options, $modify); |
|
| 63 | + |
|
| 64 | + return $fn(Psr7\Utils::modifyRequest($request, $modify), $options); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Add expect header |
|
| 69 | + */ |
|
| 70 | + private function addExpectHeader(RequestInterface $request, array $options, array &$modify): void |
|
| 71 | + { |
|
| 72 | + // Determine if the Expect header should be used |
|
| 73 | + if ($request->hasHeader('Expect')) { |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $expect = $options['expect'] ?? null; |
|
| 78 | + |
|
| 79 | + // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0 |
|
| 80 | + if ($expect === false || $request->getProtocolVersion() < 1.1) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // The expect header is unconditionally enabled |
|
| 85 | + if ($expect === true) { |
|
| 86 | + $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 87 | + |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // By default, send the expect header when the payload is > 1mb |
|
| 92 | + if ($expect === null) { |
|
| 93 | + $expect = 1048576; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Always add if the body cannot be rewound, the size cannot be |
|
| 97 | + // determined, or the size is greater than the cutoff threshold |
|
| 98 | + $body = $request->getBody(); |
|
| 99 | + $size = $body->getSize(); |
|
| 100 | + |
|
| 101 | + if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { |
|
| 102 | + $modify['set_headers']['Expect'] = '100-Continue'; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -98,7 +98,7 @@ |
||
| 98 | 98 | $body = $request->getBody(); |
| 99 | 99 | $size = $body->getSize(); |
| 100 | 100 | |
| 101 | - if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { |
|
| 101 | + if ($size === null || $size >= (int)$expect || !$body->isSeekable()) { |
|
| 102 | 102 | $modify['set_headers']['Expect'] = '100-Continue'; |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -11,8 +11,7 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @final |
| 13 | 13 | */ |
| 14 | -class PrepareBodyMiddleware |
|
| 15 | -{ |
|
| 14 | +class PrepareBodyMiddleware { |
|
| 16 | 15 | /** |
| 17 | 16 | * @var callable(RequestInterface, array): PromiseInterface |
| 18 | 17 | */ |
@@ -12,230 +12,230 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | trait ClientTrait |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Create and send an HTTP request. |
|
| 17 | - * |
|
| 18 | - * Use an absolute path to override the base path of the client, or a |
|
| 19 | - * relative path to append to the base path of the client. The URL can |
|
| 20 | - * contain the query string as well. |
|
| 21 | - * |
|
| 22 | - * @param string $method HTTP method. |
|
| 23 | - * @param string|UriInterface $uri URI object or string. |
|
| 24 | - * @param array $options Request options to apply. |
|
| 25 | - * |
|
| 26 | - * @throws GuzzleException |
|
| 27 | - */ |
|
| 28 | - abstract public function request(string $method, $uri, array $options = []): ResponseInterface; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Create and send an HTTP GET request. |
|
| 32 | - * |
|
| 33 | - * Use an absolute path to override the base path of the client, or a |
|
| 34 | - * relative path to append to the base path of the client. The URL can |
|
| 35 | - * contain the query string as well. |
|
| 36 | - * |
|
| 37 | - * @param string|UriInterface $uri URI object or string. |
|
| 38 | - * @param array $options Request options to apply. |
|
| 39 | - * |
|
| 40 | - * @throws GuzzleException |
|
| 41 | - */ |
|
| 42 | - public function get($uri, array $options = []): ResponseInterface |
|
| 43 | - { |
|
| 44 | - return $this->request('GET', $uri, $options); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Create and send an HTTP HEAD request. |
|
| 49 | - * |
|
| 50 | - * Use an absolute path to override the base path of the client, or a |
|
| 51 | - * relative path to append to the base path of the client. The URL can |
|
| 52 | - * contain the query string as well. |
|
| 53 | - * |
|
| 54 | - * @param string|UriInterface $uri URI object or string. |
|
| 55 | - * @param array $options Request options to apply. |
|
| 56 | - * |
|
| 57 | - * @throws GuzzleException |
|
| 58 | - */ |
|
| 59 | - public function head($uri, array $options = []): ResponseInterface |
|
| 60 | - { |
|
| 61 | - return $this->request('HEAD', $uri, $options); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Create and send an HTTP PUT request. |
|
| 66 | - * |
|
| 67 | - * Use an absolute path to override the base path of the client, or a |
|
| 68 | - * relative path to append to the base path of the client. The URL can |
|
| 69 | - * contain the query string as well. |
|
| 70 | - * |
|
| 71 | - * @param string|UriInterface $uri URI object or string. |
|
| 72 | - * @param array $options Request options to apply. |
|
| 73 | - * |
|
| 74 | - * @throws GuzzleException |
|
| 75 | - */ |
|
| 76 | - public function put($uri, array $options = []): ResponseInterface |
|
| 77 | - { |
|
| 78 | - return $this->request('PUT', $uri, $options); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Create and send an HTTP POST request. |
|
| 83 | - * |
|
| 84 | - * Use an absolute path to override the base path of the client, or a |
|
| 85 | - * relative path to append to the base path of the client. The URL can |
|
| 86 | - * contain the query string as well. |
|
| 87 | - * |
|
| 88 | - * @param string|UriInterface $uri URI object or string. |
|
| 89 | - * @param array $options Request options to apply. |
|
| 90 | - * |
|
| 91 | - * @throws GuzzleException |
|
| 92 | - */ |
|
| 93 | - public function post($uri, array $options = []): ResponseInterface |
|
| 94 | - { |
|
| 95 | - return $this->request('POST', $uri, $options); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Create and send an HTTP PATCH request. |
|
| 100 | - * |
|
| 101 | - * Use an absolute path to override the base path of the client, or a |
|
| 102 | - * relative path to append to the base path of the client. The URL can |
|
| 103 | - * contain the query string as well. |
|
| 104 | - * |
|
| 105 | - * @param string|UriInterface $uri URI object or string. |
|
| 106 | - * @param array $options Request options to apply. |
|
| 107 | - * |
|
| 108 | - * @throws GuzzleException |
|
| 109 | - */ |
|
| 110 | - public function patch($uri, array $options = []): ResponseInterface |
|
| 111 | - { |
|
| 112 | - return $this->request('PATCH', $uri, $options); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Create and send an HTTP DELETE request. |
|
| 117 | - * |
|
| 118 | - * Use an absolute path to override the base path of the client, or a |
|
| 119 | - * relative path to append to the base path of the client. The URL can |
|
| 120 | - * contain the query string as well. |
|
| 121 | - * |
|
| 122 | - * @param string|UriInterface $uri URI object or string. |
|
| 123 | - * @param array $options Request options to apply. |
|
| 124 | - * |
|
| 125 | - * @throws GuzzleException |
|
| 126 | - */ |
|
| 127 | - public function delete($uri, array $options = []): ResponseInterface |
|
| 128 | - { |
|
| 129 | - return $this->request('DELETE', $uri, $options); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Create and send an asynchronous HTTP request. |
|
| 134 | - * |
|
| 135 | - * Use an absolute path to override the base path of the client, or a |
|
| 136 | - * relative path to append to the base path of the client. The URL can |
|
| 137 | - * contain the query string as well. Use an array to provide a URL |
|
| 138 | - * template and additional variables to use in the URL template expansion. |
|
| 139 | - * |
|
| 140 | - * @param string $method HTTP method |
|
| 141 | - * @param string|UriInterface $uri URI object or string. |
|
| 142 | - * @param array $options Request options to apply. |
|
| 143 | - */ |
|
| 144 | - abstract public function requestAsync(string $method, $uri, array $options = []): PromiseInterface; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Create and send an asynchronous HTTP GET request. |
|
| 148 | - * |
|
| 149 | - * Use an absolute path to override the base path of the client, or a |
|
| 150 | - * relative path to append to the base path of the client. The URL can |
|
| 151 | - * contain the query string as well. Use an array to provide a URL |
|
| 152 | - * template and additional variables to use in the URL template expansion. |
|
| 153 | - * |
|
| 154 | - * @param string|UriInterface $uri URI object or string. |
|
| 155 | - * @param array $options Request options to apply. |
|
| 156 | - */ |
|
| 157 | - public function getAsync($uri, array $options = []): PromiseInterface |
|
| 158 | - { |
|
| 159 | - return $this->requestAsync('GET', $uri, $options); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Create and send an asynchronous HTTP HEAD request. |
|
| 164 | - * |
|
| 165 | - * Use an absolute path to override the base path of the client, or a |
|
| 166 | - * relative path to append to the base path of the client. The URL can |
|
| 167 | - * contain the query string as well. Use an array to provide a URL |
|
| 168 | - * template and additional variables to use in the URL template expansion. |
|
| 169 | - * |
|
| 170 | - * @param string|UriInterface $uri URI object or string. |
|
| 171 | - * @param array $options Request options to apply. |
|
| 172 | - */ |
|
| 173 | - public function headAsync($uri, array $options = []): PromiseInterface |
|
| 174 | - { |
|
| 175 | - return $this->requestAsync('HEAD', $uri, $options); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Create and send an asynchronous HTTP PUT request. |
|
| 180 | - * |
|
| 181 | - * Use an absolute path to override the base path of the client, or a |
|
| 182 | - * relative path to append to the base path of the client. The URL can |
|
| 183 | - * contain the query string as well. Use an array to provide a URL |
|
| 184 | - * template and additional variables to use in the URL template expansion. |
|
| 185 | - * |
|
| 186 | - * @param string|UriInterface $uri URI object or string. |
|
| 187 | - * @param array $options Request options to apply. |
|
| 188 | - */ |
|
| 189 | - public function putAsync($uri, array $options = []): PromiseInterface |
|
| 190 | - { |
|
| 191 | - return $this->requestAsync('PUT', $uri, $options); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Create and send an asynchronous HTTP POST request. |
|
| 196 | - * |
|
| 197 | - * Use an absolute path to override the base path of the client, or a |
|
| 198 | - * relative path to append to the base path of the client. The URL can |
|
| 199 | - * contain the query string as well. Use an array to provide a URL |
|
| 200 | - * template and additional variables to use in the URL template expansion. |
|
| 201 | - * |
|
| 202 | - * @param string|UriInterface $uri URI object or string. |
|
| 203 | - * @param array $options Request options to apply. |
|
| 204 | - */ |
|
| 205 | - public function postAsync($uri, array $options = []): PromiseInterface |
|
| 206 | - { |
|
| 207 | - return $this->requestAsync('POST', $uri, $options); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Create and send an asynchronous HTTP PATCH request. |
|
| 212 | - * |
|
| 213 | - * Use an absolute path to override the base path of the client, or a |
|
| 214 | - * relative path to append to the base path of the client. The URL can |
|
| 215 | - * contain the query string as well. Use an array to provide a URL |
|
| 216 | - * template and additional variables to use in the URL template expansion. |
|
| 217 | - * |
|
| 218 | - * @param string|UriInterface $uri URI object or string. |
|
| 219 | - * @param array $options Request options to apply. |
|
| 220 | - */ |
|
| 221 | - public function patchAsync($uri, array $options = []): PromiseInterface |
|
| 222 | - { |
|
| 223 | - return $this->requestAsync('PATCH', $uri, $options); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Create and send an asynchronous HTTP DELETE request. |
|
| 228 | - * |
|
| 229 | - * Use an absolute path to override the base path of the client, or a |
|
| 230 | - * relative path to append to the base path of the client. The URL can |
|
| 231 | - * contain the query string as well. Use an array to provide a URL |
|
| 232 | - * template and additional variables to use in the URL template expansion. |
|
| 233 | - * |
|
| 234 | - * @param string|UriInterface $uri URI object or string. |
|
| 235 | - * @param array $options Request options to apply. |
|
| 236 | - */ |
|
| 237 | - public function deleteAsync($uri, array $options = []): PromiseInterface |
|
| 238 | - { |
|
| 239 | - return $this->requestAsync('DELETE', $uri, $options); |
|
| 240 | - } |
|
| 15 | + /** |
|
| 16 | + * Create and send an HTTP request. |
|
| 17 | + * |
|
| 18 | + * Use an absolute path to override the base path of the client, or a |
|
| 19 | + * relative path to append to the base path of the client. The URL can |
|
| 20 | + * contain the query string as well. |
|
| 21 | + * |
|
| 22 | + * @param string $method HTTP method. |
|
| 23 | + * @param string|UriInterface $uri URI object or string. |
|
| 24 | + * @param array $options Request options to apply. |
|
| 25 | + * |
|
| 26 | + * @throws GuzzleException |
|
| 27 | + */ |
|
| 28 | + abstract public function request(string $method, $uri, array $options = []): ResponseInterface; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Create and send an HTTP GET request. |
|
| 32 | + * |
|
| 33 | + * Use an absolute path to override the base path of the client, or a |
|
| 34 | + * relative path to append to the base path of the client. The URL can |
|
| 35 | + * contain the query string as well. |
|
| 36 | + * |
|
| 37 | + * @param string|UriInterface $uri URI object or string. |
|
| 38 | + * @param array $options Request options to apply. |
|
| 39 | + * |
|
| 40 | + * @throws GuzzleException |
|
| 41 | + */ |
|
| 42 | + public function get($uri, array $options = []): ResponseInterface |
|
| 43 | + { |
|
| 44 | + return $this->request('GET', $uri, $options); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Create and send an HTTP HEAD request. |
|
| 49 | + * |
|
| 50 | + * Use an absolute path to override the base path of the client, or a |
|
| 51 | + * relative path to append to the base path of the client. The URL can |
|
| 52 | + * contain the query string as well. |
|
| 53 | + * |
|
| 54 | + * @param string|UriInterface $uri URI object or string. |
|
| 55 | + * @param array $options Request options to apply. |
|
| 56 | + * |
|
| 57 | + * @throws GuzzleException |
|
| 58 | + */ |
|
| 59 | + public function head($uri, array $options = []): ResponseInterface |
|
| 60 | + { |
|
| 61 | + return $this->request('HEAD', $uri, $options); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Create and send an HTTP PUT request. |
|
| 66 | + * |
|
| 67 | + * Use an absolute path to override the base path of the client, or a |
|
| 68 | + * relative path to append to the base path of the client. The URL can |
|
| 69 | + * contain the query string as well. |
|
| 70 | + * |
|
| 71 | + * @param string|UriInterface $uri URI object or string. |
|
| 72 | + * @param array $options Request options to apply. |
|
| 73 | + * |
|
| 74 | + * @throws GuzzleException |
|
| 75 | + */ |
|
| 76 | + public function put($uri, array $options = []): ResponseInterface |
|
| 77 | + { |
|
| 78 | + return $this->request('PUT', $uri, $options); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Create and send an HTTP POST request. |
|
| 83 | + * |
|
| 84 | + * Use an absolute path to override the base path of the client, or a |
|
| 85 | + * relative path to append to the base path of the client. The URL can |
|
| 86 | + * contain the query string as well. |
|
| 87 | + * |
|
| 88 | + * @param string|UriInterface $uri URI object or string. |
|
| 89 | + * @param array $options Request options to apply. |
|
| 90 | + * |
|
| 91 | + * @throws GuzzleException |
|
| 92 | + */ |
|
| 93 | + public function post($uri, array $options = []): ResponseInterface |
|
| 94 | + { |
|
| 95 | + return $this->request('POST', $uri, $options); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Create and send an HTTP PATCH request. |
|
| 100 | + * |
|
| 101 | + * Use an absolute path to override the base path of the client, or a |
|
| 102 | + * relative path to append to the base path of the client. The URL can |
|
| 103 | + * contain the query string as well. |
|
| 104 | + * |
|
| 105 | + * @param string|UriInterface $uri URI object or string. |
|
| 106 | + * @param array $options Request options to apply. |
|
| 107 | + * |
|
| 108 | + * @throws GuzzleException |
|
| 109 | + */ |
|
| 110 | + public function patch($uri, array $options = []): ResponseInterface |
|
| 111 | + { |
|
| 112 | + return $this->request('PATCH', $uri, $options); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Create and send an HTTP DELETE request. |
|
| 117 | + * |
|
| 118 | + * Use an absolute path to override the base path of the client, or a |
|
| 119 | + * relative path to append to the base path of the client. The URL can |
|
| 120 | + * contain the query string as well. |
|
| 121 | + * |
|
| 122 | + * @param string|UriInterface $uri URI object or string. |
|
| 123 | + * @param array $options Request options to apply. |
|
| 124 | + * |
|
| 125 | + * @throws GuzzleException |
|
| 126 | + */ |
|
| 127 | + public function delete($uri, array $options = []): ResponseInterface |
|
| 128 | + { |
|
| 129 | + return $this->request('DELETE', $uri, $options); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Create and send an asynchronous HTTP request. |
|
| 134 | + * |
|
| 135 | + * Use an absolute path to override the base path of the client, or a |
|
| 136 | + * relative path to append to the base path of the client. The URL can |
|
| 137 | + * contain the query string as well. Use an array to provide a URL |
|
| 138 | + * template and additional variables to use in the URL template expansion. |
|
| 139 | + * |
|
| 140 | + * @param string $method HTTP method |
|
| 141 | + * @param string|UriInterface $uri URI object or string. |
|
| 142 | + * @param array $options Request options to apply. |
|
| 143 | + */ |
|
| 144 | + abstract public function requestAsync(string $method, $uri, array $options = []): PromiseInterface; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Create and send an asynchronous HTTP GET request. |
|
| 148 | + * |
|
| 149 | + * Use an absolute path to override the base path of the client, or a |
|
| 150 | + * relative path to append to the base path of the client. The URL can |
|
| 151 | + * contain the query string as well. Use an array to provide a URL |
|
| 152 | + * template and additional variables to use in the URL template expansion. |
|
| 153 | + * |
|
| 154 | + * @param string|UriInterface $uri URI object or string. |
|
| 155 | + * @param array $options Request options to apply. |
|
| 156 | + */ |
|
| 157 | + public function getAsync($uri, array $options = []): PromiseInterface |
|
| 158 | + { |
|
| 159 | + return $this->requestAsync('GET', $uri, $options); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Create and send an asynchronous HTTP HEAD request. |
|
| 164 | + * |
|
| 165 | + * Use an absolute path to override the base path of the client, or a |
|
| 166 | + * relative path to append to the base path of the client. The URL can |
|
| 167 | + * contain the query string as well. Use an array to provide a URL |
|
| 168 | + * template and additional variables to use in the URL template expansion. |
|
| 169 | + * |
|
| 170 | + * @param string|UriInterface $uri URI object or string. |
|
| 171 | + * @param array $options Request options to apply. |
|
| 172 | + */ |
|
| 173 | + public function headAsync($uri, array $options = []): PromiseInterface |
|
| 174 | + { |
|
| 175 | + return $this->requestAsync('HEAD', $uri, $options); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Create and send an asynchronous HTTP PUT request. |
|
| 180 | + * |
|
| 181 | + * Use an absolute path to override the base path of the client, or a |
|
| 182 | + * relative path to append to the base path of the client. The URL can |
|
| 183 | + * contain the query string as well. Use an array to provide a URL |
|
| 184 | + * template and additional variables to use in the URL template expansion. |
|
| 185 | + * |
|
| 186 | + * @param string|UriInterface $uri URI object or string. |
|
| 187 | + * @param array $options Request options to apply. |
|
| 188 | + */ |
|
| 189 | + public function putAsync($uri, array $options = []): PromiseInterface |
|
| 190 | + { |
|
| 191 | + return $this->requestAsync('PUT', $uri, $options); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Create and send an asynchronous HTTP POST request. |
|
| 196 | + * |
|
| 197 | + * Use an absolute path to override the base path of the client, or a |
|
| 198 | + * relative path to append to the base path of the client. The URL can |
|
| 199 | + * contain the query string as well. Use an array to provide a URL |
|
| 200 | + * template and additional variables to use in the URL template expansion. |
|
| 201 | + * |
|
| 202 | + * @param string|UriInterface $uri URI object or string. |
|
| 203 | + * @param array $options Request options to apply. |
|
| 204 | + */ |
|
| 205 | + public function postAsync($uri, array $options = []): PromiseInterface |
|
| 206 | + { |
|
| 207 | + return $this->requestAsync('POST', $uri, $options); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Create and send an asynchronous HTTP PATCH request. |
|
| 212 | + * |
|
| 213 | + * Use an absolute path to override the base path of the client, or a |
|
| 214 | + * relative path to append to the base path of the client. The URL can |
|
| 215 | + * contain the query string as well. Use an array to provide a URL |
|
| 216 | + * template and additional variables to use in the URL template expansion. |
|
| 217 | + * |
|
| 218 | + * @param string|UriInterface $uri URI object or string. |
|
| 219 | + * @param array $options Request options to apply. |
|
| 220 | + */ |
|
| 221 | + public function patchAsync($uri, array $options = []): PromiseInterface |
|
| 222 | + { |
|
| 223 | + return $this->requestAsync('PATCH', $uri, $options); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Create and send an asynchronous HTTP DELETE request. |
|
| 228 | + * |
|
| 229 | + * Use an absolute path to override the base path of the client, or a |
|
| 230 | + * relative path to append to the base path of the client. The URL can |
|
| 231 | + * contain the query string as well. Use an array to provide a URL |
|
| 232 | + * template and additional variables to use in the URL template expansion. |
|
| 233 | + * |
|
| 234 | + * @param string|UriInterface $uri URI object or string. |
|
| 235 | + * @param array $options Request options to apply. |
|
| 236 | + */ |
|
| 237 | + public function deleteAsync($uri, array $options = []): PromiseInterface |
|
| 238 | + { |
|
| 239 | + return $this->requestAsync('DELETE', $uri, $options); |
|
| 240 | + } |
|
| 241 | 241 | } |
@@ -10,8 +10,7 @@ |
||
| 10 | 10 | /** |
| 11 | 11 | * Client interface for sending HTTP requests. |
| 12 | 12 | */ |
| 13 | -trait ClientTrait |
|
| 14 | -{ |
|
| 13 | +trait ClientTrait { |
|
| 15 | 14 | /** |
| 16 | 15 | * Create and send an HTTP request. |
| 17 | 16 | * |