Обновление клиента (apps, 3rdparty, install)
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2020 PHP HTTP Team <team@php-http.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Adapter\Guzzle7;
|
||||
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Utils;
|
||||
use Http\Client\HttpAsyncClient;
|
||||
use Http\Client\HttpClient;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* HTTP Adapter for Guzzle 7.
|
||||
*
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
final class Client implements HttpClient, HttpAsyncClient
|
||||
{
|
||||
/**
|
||||
* @var ClientInterface
|
||||
*/
|
||||
private $guzzle;
|
||||
|
||||
public function __construct(?ClientInterface $guzzle = null)
|
||||
{
|
||||
if (!$guzzle) {
|
||||
$guzzle = self::buildClient();
|
||||
}
|
||||
|
||||
$this->guzzle = $guzzle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create the Guzzle 7 adapter with custom Guzzle configuration.
|
||||
*/
|
||||
public static function createWithConfig(array $config): Client
|
||||
{
|
||||
return new self(self::buildClient($config));
|
||||
}
|
||||
|
||||
public function sendRequest(RequestInterface $request): ResponseInterface
|
||||
{
|
||||
return $this->sendAsyncRequest($request)->wait();
|
||||
}
|
||||
|
||||
public function sendAsyncRequest(RequestInterface $request)
|
||||
{
|
||||
$promise = $this->guzzle->sendAsync($request);
|
||||
|
||||
return new Promise($promise, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the Guzzle client instance.
|
||||
*/
|
||||
private static function buildClient(array $config = []): GuzzleClient
|
||||
{
|
||||
$handlerStack = new HandlerStack(Utils::chooseHandler());
|
||||
$handlerStack->push(Middleware::prepareBody(), 'prepare_body');
|
||||
$config = array_merge(['handler' => $handlerStack], $config);
|
||||
|
||||
return new GuzzleClient($config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Adapter\Guzzle7\Exception;
|
||||
|
||||
use Http\Client\Exception;
|
||||
|
||||
final class UnexpectedValueException extends \UnexpectedValueException implements Exception
|
||||
{
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Adapter\Guzzle7;
|
||||
|
||||
use GuzzleHttp\Exception as GuzzleExceptions;
|
||||
use GuzzleHttp\Promise\PromiseInterface;
|
||||
use Http\Adapter\Guzzle7\Exception\UnexpectedValueException;
|
||||
use Http\Client\Exception as HttplugException;
|
||||
use Http\Promise\Promise as HttpPromise;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Wrapper around Guzzle promises.
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
final class Promise implements HttpPromise
|
||||
{
|
||||
/**
|
||||
* @var PromiseInterface
|
||||
*/
|
||||
private $promise;
|
||||
|
||||
/**
|
||||
* @var string State of the promise
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* @var HttplugException
|
||||
*/
|
||||
private $exception;
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
public function __construct(PromiseInterface $promise, RequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->state = self::PENDING;
|
||||
$this->promise = $promise->then(function ($response) {
|
||||
$this->response = $response;
|
||||
$this->state = self::FULFILLED;
|
||||
|
||||
return $response;
|
||||
}, function ($reason) {
|
||||
$this->state = self::REJECTED;
|
||||
|
||||
if ($reason instanceof HttplugException) {
|
||||
$this->exception = $reason;
|
||||
} elseif ($reason instanceof GuzzleExceptions\GuzzleException) {
|
||||
$this->exception = $this->handleException($reason);
|
||||
} elseif ($reason instanceof \Throwable) {
|
||||
$this->exception = new HttplugException\TransferException('Invalid exception returned from Guzzle7', 0, $reason);
|
||||
} else {
|
||||
$this->exception = new UnexpectedValueException('Reason returned from Guzzle7 must be an Exception');
|
||||
}
|
||||
|
||||
throw $this->exception;
|
||||
});
|
||||
}
|
||||
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
{
|
||||
return new static($this->promise->then($onFulfilled, $onRejected), $this->request);
|
||||
}
|
||||
|
||||
public function getState()
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
public function wait($unwrap = true)
|
||||
{
|
||||
$this->promise->wait(false);
|
||||
|
||||
if ($unwrap) {
|
||||
if (self::REJECTED === $this->getState()) {
|
||||
throw $this->exception;
|
||||
}
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Guzzle exception into an Httplug exception.
|
||||
*
|
||||
* @return HttplugException
|
||||
*/
|
||||
private function handleException(GuzzleExceptions\GuzzleException $exception)
|
||||
{
|
||||
if ($exception instanceof GuzzleExceptions\ConnectException) {
|
||||
return new HttplugException\NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
|
||||
}
|
||||
|
||||
if ($exception instanceof GuzzleExceptions\RequestException) {
|
||||
// Make sure we have a response for the HttpException
|
||||
if ($exception->hasResponse()) {
|
||||
return new HttplugException\HttpException(
|
||||
$exception->getMessage(),
|
||||
$exception->getRequest(),
|
||||
$exception->getResponse(),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
|
||||
return new HttplugException\RequestException($exception->getMessage(), $exception->getRequest(), $exception);
|
||||
}
|
||||
|
||||
return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
|
||||
}
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
Copyright (c) 2014 Eric GELOEN <geloen.eric@gmail.com>
|
||||
Copyright (c) 2015 PHP HTTP Team <team@php-http.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"version": "1.0",
|
||||
"name": "php-http/httplug",
|
||||
"binding-types": {
|
||||
"Http\\Client\\HttpAsyncClient": {
|
||||
"description": "Async HTTP Client"
|
||||
},
|
||||
"Http\\Client\\HttpClient": {
|
||||
"description": "HTTP Client"
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
|
||||
use Psr\Http\Client\ClientExceptionInterface as PsrClientException;
|
||||
|
||||
/**
|
||||
* Every HTTP Client related Exception must implement this interface.
|
||||
*
|
||||
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
|
||||
*/
|
||||
interface Exception extends PsrClientException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Exception;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Thrown when a response was received but the request itself failed.
|
||||
*
|
||||
* In addition to the request, this exception always provides access to the response object.
|
||||
*
|
||||
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
|
||||
*/
|
||||
class HttpException extends RequestException
|
||||
{
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function __construct(
|
||||
$message,
|
||||
RequestInterface $request,
|
||||
ResponseInterface $response,
|
||||
?\Exception $previous = null
|
||||
) {
|
||||
parent::__construct($message, $request, $previous);
|
||||
|
||||
$this->response = $response;
|
||||
$this->code = $response->getStatusCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response.
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a new exception with a normalized error message.
|
||||
*/
|
||||
public static function create(
|
||||
RequestInterface $request,
|
||||
ResponseInterface $response,
|
||||
?\Exception $previous = null
|
||||
) {
|
||||
$message = sprintf(
|
||||
'[url] %s [http method] %s [status code] %s [reason phrase] %s',
|
||||
$request->getRequestTarget(),
|
||||
$request->getMethod(),
|
||||
$response->getStatusCode(),
|
||||
$response->getReasonPhrase()
|
||||
);
|
||||
|
||||
return new static($message, $request, $response, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Exception;
|
||||
|
||||
use Psr\Http\Client\NetworkExceptionInterface as PsrNetworkException;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Thrown when the request cannot be completed because of network issues.
|
||||
*
|
||||
* There is no response object as this exception is thrown when no response has been received.
|
||||
*
|
||||
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
|
||||
*/
|
||||
class NetworkException extends TransferException implements PsrNetworkException
|
||||
{
|
||||
use RequestAwareTrait;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function __construct($message, RequestInterface $request, ?\Exception $previous = null)
|
||||
{
|
||||
$this->setRequest($request);
|
||||
|
||||
parent::__construct($message, 0, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Exception;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
trait RequestAwareTrait
|
||||
{
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
private function setRequest(RequestInterface $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function getRequest(): RequestInterface
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Exception;
|
||||
|
||||
use Psr\Http\Client\RequestExceptionInterface as PsrRequestException;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Exception for when a request failed, providing access to the failed request.
|
||||
*
|
||||
* This could be due to an invalid request, or one of the extending exceptions
|
||||
* for network errors or HTTP error responses.
|
||||
*
|
||||
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
|
||||
*/
|
||||
class RequestException extends TransferException implements PsrRequestException
|
||||
{
|
||||
use RequestAwareTrait;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function __construct($message, RequestInterface $request, ?\Exception $previous = null)
|
||||
{
|
||||
$this->setRequest($request);
|
||||
|
||||
parent::__construct($message, 0, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Exception;
|
||||
|
||||
use Http\Client\Exception;
|
||||
|
||||
/**
|
||||
* Base exception for transfer related exceptions.
|
||||
*
|
||||
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
|
||||
*/
|
||||
class TransferException extends \RuntimeException implements Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
|
||||
use Http\Promise\Promise;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Sends a PSR-7 Request in an asynchronous way by returning a Promise.
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
interface HttpAsyncClient
|
||||
{
|
||||
/**
|
||||
* Sends a PSR-7 request in an asynchronous way.
|
||||
*
|
||||
* Exceptions related to processing the request are available from the returned Promise.
|
||||
*
|
||||
* @return Promise resolves a PSR-7 Response or fails with an Http\Client\Exception
|
||||
*
|
||||
* @throws \Exception If processing the request is impossible (eg. bad configuration).
|
||||
*/
|
||||
public function sendAsyncRequest(RequestInterface $request);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Provide the Httplug HttpClient interface for BC.
|
||||
* You should typehint Psr\Http\Client\ClientInterface in new code
|
||||
*
|
||||
* @deprecated since version 2.4, use Psr\Http\Client\ClientInterface instead; see https://www.php-fig.org/psr/psr-18/
|
||||
*/
|
||||
interface HttpClient extends ClientInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Promise;
|
||||
|
||||
use Http\Client\Exception;
|
||||
use Http\Promise\Promise;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
final class HttpFulfilledPromise implements Promise
|
||||
{
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
private $response;
|
||||
|
||||
public function __construct(ResponseInterface $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
{
|
||||
if (null === $onFulfilled) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
return new self($onFulfilled($this->response));
|
||||
} catch (Exception $e) {
|
||||
return new HttpRejectedPromise($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getState()
|
||||
{
|
||||
return Promise::FULFILLED;
|
||||
}
|
||||
|
||||
public function wait($unwrap = true)
|
||||
{
|
||||
if ($unwrap) {
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client\Promise;
|
||||
|
||||
use Http\Client\Exception;
|
||||
use Http\Promise\Promise;
|
||||
|
||||
final class HttpRejectedPromise implements Promise
|
||||
{
|
||||
/**
|
||||
* @var Exception
|
||||
*/
|
||||
private $exception;
|
||||
|
||||
public function __construct(Exception $exception)
|
||||
{
|
||||
$this->exception = $exception;
|
||||
}
|
||||
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
{
|
||||
if (null === $onRejected) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $onRejected($this->exception);
|
||||
if ($result instanceof Promise) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return new HttpFulfilledPromise($result);
|
||||
} catch (Exception $e) {
|
||||
return new self($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getState()
|
||||
{
|
||||
return Promise::REJECTED;
|
||||
}
|
||||
|
||||
public function wait($unwrap = true)
|
||||
{
|
||||
if ($unwrap) {
|
||||
throw $this->exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2015-2016 PHP HTTP Team <team@php-http.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Promise;
|
||||
|
||||
/**
|
||||
* A promise already fulfilled.
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
final class FulfilledPromise implements Promise
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $result;
|
||||
|
||||
/**
|
||||
* @param mixed $result
|
||||
*/
|
||||
public function __construct($result)
|
||||
{
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
{
|
||||
if (null === $onFulfilled) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
return new self($onFulfilled($this->result));
|
||||
} catch (\Exception $e) {
|
||||
return new RejectedPromise($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getState()
|
||||
{
|
||||
return Promise::FULFILLED;
|
||||
}
|
||||
|
||||
public function wait($unwrap = true)
|
||||
{
|
||||
if ($unwrap) {
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Promise;
|
||||
|
||||
/**
|
||||
* Promise represents a value that may not be available yet, but will be resolved at some point in future.
|
||||
* It acts like a proxy to the actual value.
|
||||
*
|
||||
* This interface is an extension of the promises/a+ specification.
|
||||
*
|
||||
* @see https://promisesaplus.com/
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
|
||||
*/
|
||||
interface Promise
|
||||
{
|
||||
/**
|
||||
* Promise has not been fulfilled or rejected.
|
||||
*/
|
||||
const PENDING = 'pending';
|
||||
|
||||
/**
|
||||
* Promise has been fulfilled.
|
||||
*/
|
||||
const FULFILLED = 'fulfilled';
|
||||
|
||||
/**
|
||||
* Promise has been rejected.
|
||||
*/
|
||||
const REJECTED = 'rejected';
|
||||
|
||||
/**
|
||||
* Adds behavior for when the promise is resolved or rejected (response will be available, or error happens).
|
||||
*
|
||||
* If you do not care about one of the cases, you can set the corresponding callable to null
|
||||
* The callback will be called when the value arrived and never more than once.
|
||||
*
|
||||
* @param callable|null $onFulfilled called when a response will be available
|
||||
* @param callable|null $onRejected called when an exception occurs
|
||||
*
|
||||
* @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
|
||||
*/
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null);
|
||||
|
||||
/**
|
||||
* Returns the state of the promise, one of PENDING, FULFILLED or REJECTED.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getState();
|
||||
|
||||
/**
|
||||
* Wait for the promise to be fulfilled or rejected.
|
||||
*
|
||||
* When this method returns, the request has been resolved and if callables have been
|
||||
* specified, the appropriate one has terminated.
|
||||
*
|
||||
* When $unwrap is true (the default), the response is returned, or the exception thrown
|
||||
* on failure. Otherwise, nothing is returned or thrown.
|
||||
*
|
||||
* @param bool $unwrap Whether to return resolved value / throw reason or not
|
||||
*
|
||||
* @return ($unwrap is true ? mixed : null) Resolved value, null if $unwrap is set to false
|
||||
*
|
||||
* @throws \Throwable the rejection reason if $unwrap is set to true and the request failed
|
||||
*/
|
||||
public function wait($unwrap = true);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Promise;
|
||||
|
||||
/**
|
||||
* A rejected promise.
|
||||
*
|
||||
* @author Joel Wurtz <joel.wurtz@gmail.com>
|
||||
*/
|
||||
final class RejectedPromise implements Promise
|
||||
{
|
||||
/**
|
||||
* @var \Throwable
|
||||
*/
|
||||
private $exception;
|
||||
|
||||
public function __construct(\Throwable $exception)
|
||||
{
|
||||
$this->exception = $exception;
|
||||
}
|
||||
|
||||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
|
||||
{
|
||||
if (null === $onRejected) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
return new FulfilledPromise($onRejected($this->exception));
|
||||
} catch (\Exception $e) {
|
||||
return new self($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getState()
|
||||
{
|
||||
return Promise::REJECTED;
|
||||
}
|
||||
|
||||
public function wait($unwrap = true)
|
||||
{
|
||||
if ($unwrap) {
|
||||
throw $this->exception;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user