From 5f0862556da62d479a6c5e00add9c1584a940f43 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 7 Jun 2023 15:40:13 +0300 Subject: [PATCH 1/2] Upgrade Guzzle Promises Function API to Static API Guzzle Promises: A static API was first introduced in 1.4.0, in order to mitigate problems with functions conflicting between global and local copies of the package. The function API was removed in 2.0.0. https://github.com/guzzle/promises#upgrading-from-function-api --- src/Ganesha/GuzzleMiddleware.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Ganesha/GuzzleMiddleware.php b/src/Ganesha/GuzzleMiddleware.php index ede16f4..55c9936 100644 --- a/src/Ganesha/GuzzleMiddleware.php +++ b/src/Ganesha/GuzzleMiddleware.php @@ -46,7 +46,7 @@ public function __invoke(callable $handler): \Closure $serviceName = $this->serviceNameExtractor->extract($request, $options); if (!$this->ganesha->isAvailable($serviceName)) { - return \GuzzleHttp\Promise\rejection_for( + return \GuzzleHttp\Promise\Create::rejectionFor( new RejectedException( sprintf('"%s" is not available', $serviceName) ) @@ -62,11 +62,11 @@ function ($value) use ($serviceName) { } else { $this->ganesha->success($serviceName); } - return \GuzzleHttp\Promise\promise_for($value); + return \GuzzleHttp\Promise\Create::promiseFor($value); }, function ($reason) use ($serviceName) { $this->ganesha->failure($serviceName); - return \GuzzleHttp\Promise\rejection_for($reason); + return \GuzzleHttp\Promise\Create::rejectionFor($reason); } ); }; From 2b7d59b3cea03d2a02028ccb4842a45b42feb111 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Thu, 15 Jun 2023 09:11:24 +0900 Subject: [PATCH 2/2] We need to support both the static and function API of `guzzle/promises` for the time being --- src/Ganesha/GuzzleMiddleware.php | 37 +++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Ganesha/GuzzleMiddleware.php b/src/Ganesha/GuzzleMiddleware.php index 55c9936..1821c36 100644 --- a/src/Ganesha/GuzzleMiddleware.php +++ b/src/Ganesha/GuzzleMiddleware.php @@ -26,6 +26,20 @@ class GuzzleMiddleware */ private $failureDetector; + /** + * Function name to be used for returning a rejected promise. + * + * @var string + */ + private string $rejectionForFunction; + + /** + * Function name to be used for returning a promise. + * + * @var string + */ + private string $promiseForFunction; + public function __construct( Ganesha $ganesha, ServiceNameExtractorInterface $serviceNameExtractor = null, @@ -34,6 +48,16 @@ public function __construct( $this->ganesha = $ganesha; $this->serviceNameExtractor = $serviceNameExtractor ?: new ServiceNameExtractor(); $this->failureDetector = $failureDetector ?: new AlwaysSuccessFailureDetector(); + + // We need to support both the static and function API of `guzzle/promises` for the time being. + // https://github.com/guzzle/promises#upgrading-from-function-api + if (class_exists('\GuzzleHttp\Promise\Create')) { + $this->rejectionForFunction = '\GuzzleHttp\Promise\Create::rejectionFor'; + $this->promiseForFunction = '\GuzzleHttp\Promise\Create::promiseFor'; + } else { + $this->rejectionForFunction = '\GuzzleHttp\Promise\rejection_for'; + $this->promiseForFunction = '\GuzzleHttp\Promise\promise_for'; + } } /** @@ -46,7 +70,8 @@ public function __invoke(callable $handler): \Closure $serviceName = $this->serviceNameExtractor->extract($request, $options); if (!$this->ganesha->isAvailable($serviceName)) { - return \GuzzleHttp\Promise\Create::rejectionFor( + return call_user_func( + $this->rejectionForFunction, new RejectedException( sprintf('"%s" is not available', $serviceName) ) @@ -62,11 +87,17 @@ function ($value) use ($serviceName) { } else { $this->ganesha->success($serviceName); } - return \GuzzleHttp\Promise\Create::promiseFor($value); + return call_user_func( + $this->promiseForFunction, + $value + ); }, function ($reason) use ($serviceName) { $this->ganesha->failure($serviceName); - return \GuzzleHttp\Promise\Create::rejectionFor($reason); + return call_user_func( + $this->rejectionForFunction, + $reason + ); } ); };