From 3613c54e10f86d486e1003ec14b0ae49817463f2 Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Sat, 9 Nov 2024 21:31:30 +0100 Subject: [PATCH] Allow replacing the guzzle handler This allows setting e.g. Amp\Http\Client\GuzzleAdapter\GuzzleHandlerAdapter, allowing the whole firebase-php environment to transparently run asynchronously. Up to now this required very ugly hacking manipulating private fields directly, like: (fn() => (fn() => (fn() => (fn() => $this->handler = new GuzzleHandlerAdapter)->call($this->config["handler"]))->call($this->client))->call($this->messagingApi))->call($messaging); Which is not exactly ergonomic. --- src/Firebase/Factory.php | 4 +++- src/Firebase/Http/HttpClientOptions.php | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Firebase/Factory.php b/src/Firebase/Factory.php index 5a0db8ee..98e684a8 100644 --- a/src/Firebase/Factory.php +++ b/src/Firebase/Factory.php @@ -22,6 +22,7 @@ use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\MessageFormatter; +use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7\HttpFactory; use GuzzleHttp\Psr7\Utils as GuzzleUtils; use Kreait\Firebase\AppCheck\AppCheckTokenGenerator; @@ -41,6 +42,7 @@ use Kreait\Firebase\Messaging\RequestFactory; use Psr\Cache\CacheItemPoolInterface; use Psr\Clock\ClockInterface; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; @@ -540,7 +542,7 @@ public function createApiClient(?array $config = null, ?array $middlewares = nul $config = [...$this->httpClientOptions->guzzleConfig(), ...$config]; - $handler = HandlerStack::create(); + $handler = HandlerStack::create($this->httpClientOptions->guzzleHandler()); if ($this->httpLogMiddleware) { $handler->push($this->httpLogMiddleware, 'http_logs'); diff --git a/src/Firebase/Http/HttpClientOptions.php b/src/Firebase/Http/HttpClientOptions.php index 5e3dbbb2..44ff547e 100644 --- a/src/Firebase/Http/HttpClientOptions.php +++ b/src/Firebase/Http/HttpClientOptions.php @@ -4,13 +4,18 @@ namespace Kreait\Firebase\Http; +use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\RequestOptions; use Kreait\Firebase\Exception\InvalidArgumentException; +use Psr\Http\Message\RequestInterface; use function is_callable; final class HttpClientOptions { + /** @var callable(RequestInterface, array): PromiseInterface|null */ + private $guzzleHandler = null; + /** * @param array $guzzleConfig * @param list $guzzleMiddlewares @@ -190,4 +195,23 @@ public function withGuzzleMiddlewares(array $middlewares): self return new self($this->guzzleConfig, $newMiddlewares); } + + /** + * The guzzle Handler to be used in Guzzles HandlerStack. + * + * @return callable(RequestInterface, array): PromiseInterface|null + */ + public function guzzleHandler(): callable|null + { + return $this->guzzleHandler; + } + + /** + * @param callable(RequestInterface, array): PromiseInterface $value the guzzle Handler to be used in Guzzles HandlerStack + */ + public function withGuzzleHandler(callable $handler): self + { + $this->guzzleHandler = $handler; + return $this; + } }