From 928d8d12480625f3a03879f1a2876f88b5cf6ce1 Mon Sep 17 00:00:00 2001 From: yaozm Date: Wed, 10 Jul 2024 10:45:55 +0800 Subject: [PATCH] refactor(utils): update Utils references - Updated references to Utils class to use fully qualified namespace - Replaced usage of Utils class with Support\Utils namespace - Updated method calls to use fully qualified namespace for Utils class --- src/Foundation/Client.php | 6 +- src/Foundation/Concerns/AsMultipart.php | 2 +- src/Foundation/Concerns/HasHttpClient.php | 14 +- src/Foundation/Support/Utils.php | 160 +++++++++++----------- 4 files changed, 85 insertions(+), 97 deletions(-) diff --git a/src/Foundation/Client.php b/src/Foundation/Client.php index 05a2344..ff71a13 100644 --- a/src/Foundation/Client.php +++ b/src/Foundation/Client.php @@ -20,9 +20,9 @@ use Guanguans\Notify\Foundation\Concerns\HasHttpClient; use Guanguans\Notify\Foundation\Contracts\Authenticator; use Guanguans\Notify\Foundation\Contracts\Message; +use Guanguans\Notify\Foundation\Support\Utils; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Promise\PromiseInterface; -use GuzzleHttp\Promise\Utils; use GuzzleHttp\RequestOptions; use Psr\Http\Message\ResponseInterface; @@ -70,7 +70,7 @@ public function sendAsync(Message $message): PromiseInterface return $this->getHttpClient()->requestAsync( $message->toHttpMethod(), $message->toHttpUri(), - $this->normalizeHttpOptions($this->authenticator->applyToOptions($message->toHttpOptions())), + Utils::normalizeHttpOptions($this->authenticator->applyToOptions($message->toHttpOptions())), ); } @@ -89,7 +89,7 @@ public function pool(iterable $messages): array { // return Utils::settle($promises)->wait(); /** @noinspection PhpParamsInspection */ - return Utils::unwrap( + return \GuzzleHttp\Promise\Utils::unwrap( (function (iterable $messages): \Generator { foreach ($messages as $key => $message) { yield $key => $this->sendAsync($message); diff --git a/src/Foundation/Concerns/AsMultipart.php b/src/Foundation/Concerns/AsMultipart.php index 9a22700..970467b 100644 --- a/src/Foundation/Concerns/AsMultipart.php +++ b/src/Foundation/Concerns/AsMultipart.php @@ -21,7 +21,7 @@ trait AsMultipart { /** - * @see \Guanguans\Notify\Foundation\Client::normalizeHttpOptions() + * @see \Guanguans\Notify\Foundation\Support\Utils::normalizeHttpOptions() * @see \Guanguans\Notify\Foundation\Client::send() */ public function toHttpOptions(): array diff --git a/src/Foundation/Concerns/HasHttpClient.php b/src/Foundation/Concerns/HasHttpClient.php index 2db6962..576c3e6 100644 --- a/src/Foundation/Concerns/HasHttpClient.php +++ b/src/Foundation/Concerns/HasHttpClient.php @@ -123,7 +123,7 @@ public function setHttpClientResolver(callable $httpClientResolver): self public function getHttpClientResolver(): callable { - return $this->httpClientResolver ??= fn (): Client => new Client($this->normalizeHttpOptions( + return $this->httpClientResolver ??= fn (): Client => new Client(Utils::normalizeHttpOptions( Utils::mergeHttpOptions($this->defaultHttpOptions(), $this->getHttpOptions()), )); } @@ -170,18 +170,6 @@ public function defaultHttpOptions(): array ]; } - public function normalizeHttpOptions(array $options): array - { - if (isset($options[RequestOptions::MULTIPART])) { - $options[RequestOptions::MULTIPART] = Utils::multipartFor( - $options[RequestOptions::MULTIPART], - MULTIPART_TRY_OPEN_FILE, - ); - } - - return $options; - } - /** * @param null|list $queue */ diff --git a/src/Foundation/Support/Utils.php b/src/Foundation/Support/Utils.php index 0f33e29..daca78d 100644 --- a/src/Foundation/Support/Utils.php +++ b/src/Foundation/Support/Utils.php @@ -27,6 +27,86 @@ */ class Utils { + /** + * Replace the given options with the current request options. + */ + public static function mergeHttpOptions(array $originalOptions, array ...$options): array + { + return array_replace_recursive( + array_merge_recursive($originalOptions, Arr::only($options, [ + RequestOptions::COOKIES, + RequestOptions::FORM_PARAMS, + RequestOptions::HEADERS, + RequestOptions::JSON, + RequestOptions::MULTIPART, + RequestOptions::QUERY, + ])), + ...$options, + ); + } + + public static function normalizeHttpOptions(array $httpOptions, int $options = MULTIPART_TRY_OPEN_FILE): array + { + if (isset($httpOptions[RequestOptions::MULTIPART])) { + $httpOptions[RequestOptions::MULTIPART] = self::multipartFor( + $httpOptions[RequestOptions::MULTIPART], + $options, + ); + } + + return $httpOptions; + } + + /** + * Retrieves the HTTP options constants. + * + * @return array + */ + public static function httpOptionConstants(): array + { + $constants = (new \ReflectionClass(RequestOptions::class))->getConstants() + [ + // '_CONDITIONAL' => '_conditional', + 'BASE_URI' => 'base_uri', + 'CURL' => 'curl', + ]; + + asort($constants); + + return $constants; + } + + /** + * Return an array of defined properties for the given object. + * + * @psalm-suppress InvalidScope + * + * @param class-string|Message $object + * + * @throws \ReflectionException + * + * @return list + */ + public static function definedFor($object): array + { + if (\is_string($object)) { + $reflectionClass = new \ReflectionClass($object); + + $properties = $reflectionClass->getDefaultProperties(); + + return array_unique(array_merge( + $properties['defined'] ?? [], + $properties['required'] ?? [] + )); + } + + return array_unique( + (fn (): array => array_merge( + $this->defined ?? [], + $this->required ?? [], + ))->call($object) + ); + } + /** * Convert a form array into a multipart array. */ @@ -106,86 +186,6 @@ static function (array $part) use ($contentsNormalizer, $options): array { ); } - /** - * Return an array of defined properties for the given object. - * - * @psalm-suppress InvalidScope - * - * @param class-string|Message $object - * - * @throws \ReflectionException - * - * @return list - */ - public static function definedFor($object): array - { - if (\is_string($object)) { - $reflectionClass = new \ReflectionClass($object); - - $properties = $reflectionClass->getDefaultProperties(); - - return array_unique(array_merge( - $properties['defined'] ?? [], - $properties['required'] ?? [] - )); - } - - return array_unique( - (fn (): array => array_merge( - $this->defined ?? [], - $this->required ?? [], - ))->call($object) - ); - } - - /** - * Retrieves the HTTP options constants. - * - * @return array - */ - public static function httpOptionConstants(): array - { - $constants = (new \ReflectionClass(RequestOptions::class))->getConstants() + [ - // '_CONDITIONAL' => '_conditional', - 'BASE_URI' => 'base_uri', - 'CURL' => 'curl', - ]; - - asort($constants); - - return $constants; - } - - /** - * Replace the given options with the current request options. - */ - public static function mergeHttpOptions(array $originalOptions, array ...$options): array - { - return array_replace_recursive( - array_merge_recursive($originalOptions, Arr::only($options, [ - RequestOptions::COOKIES, - RequestOptions::FORM_PARAMS, - RequestOptions::HEADERS, - RequestOptions::JSON, - RequestOptions::MULTIPART, - RequestOptions::QUERY, - ])), - ...$options, - ); - } - - public static function normalizeHttpOptions(array $httpOptions, int $options = MULTIPART_TRY_OPEN_FILE): array - { - if (isset($httpOptions[RequestOptions::MULTIPART])) { - $httpOptions[RequestOptions::MULTIPART] = self::multipartFor( - $httpOptions[RequestOptions::MULTIPART], - $options, - ); - } - - return $httpOptions; - } - /** * @param array $agents */