Update to laravel 7

This commit is contained in:
KodeStar
2022-03-10 11:54:29 +00:00
parent 61a5a1a8b0
commit f9a19fce91
7170 changed files with 274189 additions and 283773 deletions

View File

@@ -3,16 +3,16 @@
namespace Github\HttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin;
use Http\Client\Common\Plugin\Cache\Generator\HeaderCacheKeyGenerator;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\RequestFactory;
use Http\Message\StreamFactory;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
/**
* A builder that builds the API client.
@@ -25,24 +25,24 @@ class Builder
/**
* The object that sends HTTP messages.
*
* @var HttpClient
* @var ClientInterface
*/
private $httpClient;
/**
* A HTTP client with all our plugins.
*
* @var HttpMethodsClient
* @var HttpMethodsClientInterface
*/
private $pluginClient;
/**
* @var RequestFactory
* @var RequestFactoryInterface
*/
private $requestFactory;
/**
* @var StreamFactory
* @var StreamFactoryInterface
*/
private $streamFactory;
@@ -73,24 +73,24 @@ class Builder
private $headers = [];
/**
* @param HttpClient $httpClient
* @param RequestFactory $requestFactory
* @param StreamFactory $streamFactory
* @param ClientInterface|null $httpClient
* @param RequestFactoryInterface|null $requestFactory
* @param StreamFactoryInterface|null $streamFactory
*/
public function __construct(
HttpClient $httpClient = null,
RequestFactory $requestFactory = null,
StreamFactory $streamFactory = null
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactory = null,
StreamFactoryInterface $streamFactory = null
) {
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
$this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
}
/**
* @return HttpMethodsClient
* @return HttpMethodsClientInterface
*/
public function getHttpClient()
public function getHttpClient(): HttpMethodsClientInterface
{
if ($this->httpClientModified) {
$this->httpClientModified = false;
@@ -102,7 +102,8 @@ class Builder
$this->pluginClient = new HttpMethodsClient(
(new PluginClientFactory())->createClient($this->httpClient, $plugins),
$this->requestFactory
$this->requestFactory,
$this->streamFactory
);
}
@@ -113,8 +114,10 @@ class Builder
* Add a new plugin to the end of the plugin chain.
*
* @param Plugin $plugin
*
* @return void
*/
public function addPlugin(Plugin $plugin)
public function addPlugin(Plugin $plugin): void
{
$this->plugins[] = $plugin;
$this->httpClientModified = true;
@@ -124,8 +127,10 @@ class Builder
* Remove a plugin by its fully qualified class name (FQCN).
*
* @param string $fqcn
*
* @return void
*/
public function removePlugin($fqcn)
public function removePlugin(string $fqcn): void
{
foreach ($this->plugins as $idx => $plugin) {
if ($plugin instanceof $fqcn) {
@@ -137,8 +142,10 @@ class Builder
/**
* Clears used headers.
*
* @return void
*/
public function clearHeaders()
public function clearHeaders(): void
{
$this->headers = [];
@@ -148,8 +155,10 @@ class Builder
/**
* @param array $headers
*
* @return void
*/
public function addHeaders(array $headers)
public function addHeaders(array $headers): void
{
$this->headers = array_merge($this->headers, $headers);
@@ -160,8 +169,10 @@ class Builder
/**
* @param string $header
* @param string $headerValue
*
* @return void
*/
public function addHeaderValue($header, $headerValue)
public function addHeaderValue(string $header, string $headerValue): void
{
if (!isset($this->headers[$header])) {
$this->headers[$header] = $headerValue;
@@ -178,8 +189,10 @@ class Builder
*
* @param CacheItemPoolInterface $cachePool
* @param array $config
*
* @return void
*/
public function addCache(CacheItemPoolInterface $cachePool, array $config = [])
public function addCache(CacheItemPoolInterface $cachePool, array $config = []): void
{
if (!isset($config['cache_key_generator'])) {
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']);
@@ -190,8 +203,10 @@ class Builder
/**
* Remove the cache plugin.
*
* @return void
*/
public function removeCache()
public function removeCache(): void
{
$this->cachePlugin = null;
$this->httpClientModified = true;

View File

@@ -5,7 +5,7 @@ namespace Github\HttpClient\Message;
use Github\Exception\ApiLimitExceedException;
use Psr\Http\Message\ResponseInterface;
class ResponseMediator
final class ResponseMediator
{
/**
* @param ResponseInterface $response
@@ -28,19 +28,21 @@ class ResponseMediator
/**
* @param ResponseInterface $response
*
* @return array|void
* @return array<string,string>
*/
public static function getPagination(ResponseInterface $response)
public static function getPagination(ResponseInterface $response): array
{
if (!$response->hasHeader('Link')) {
return;
$header = self::getHeader($response, 'Link');
if (null === $header) {
return [];
}
$header = self::getHeader($response, 'Link');
$pagination = [];
foreach (explode(',', $header) as $link) {
preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match);
/** @var string[] $match */
if (3 === count($match)) {
$pagination[$match[2]] = $match[1];
}
@@ -52,17 +54,23 @@ class ResponseMediator
/**
* @param ResponseInterface $response
*
* @return null|string
* @return string|null
*/
public static function getApiLimit(ResponseInterface $response)
public static function getApiLimit(ResponseInterface $response): ?string
{
$remainingCalls = self::getHeader($response, 'X-RateLimit-Remaining');
$remainingCallsHeader = self::getHeader($response, 'X-RateLimit-Remaining');
if (null !== $remainingCalls && 1 > $remainingCalls) {
if (null === $remainingCallsHeader) {
return null;
}
$remainingCalls = (int) $remainingCallsHeader;
if (1 > $remainingCalls) {
throw new ApiLimitExceedException($remainingCalls);
}
return $remainingCalls;
return $remainingCallsHeader;
}
/**
@@ -73,7 +81,7 @@ class ResponseMediator
*
* @return string|null
*/
public static function getHeader(ResponseInterface $response, $name)
public static function getHeader(ResponseInterface $response, string $name): ?string
{
$headers = $response->getHeader($name);

View File

@@ -2,9 +2,10 @@
namespace Github\HttpClient\Plugin;
use Github\Client;
use Github\AuthMethod;
use Github\Exception\RuntimeException;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
/**
@@ -12,13 +13,29 @@ use Psr\Http\Message\RequestInterface;
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Authentication implements Plugin
final class Authentication implements Plugin
{
/**
* @var string
*/
private $tokenOrLogin;
/**
* @var string|null
*/
private $password;
/**
* @var string|null
*/
private $method;
public function __construct($tokenOrLogin, $password, $method)
/**
* @param string $tokenOrLogin GitHub private token/username/client ID
* @param string|null $password GitHub password/secret (optionally can contain $method)
* @param string|null $method One of the AUTH_* class constants
*/
public function __construct(string $tokenOrLogin, ?string $password, ?string $method)
{
$this->tokenOrLogin = $tokenOrLogin;
$this->password = $password;
@@ -26,60 +43,29 @@ class Authentication implements Plugin
}
/**
* {@inheritdoc}
* @return Promise
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
switch ($this->method) {
case Client::AUTH_HTTP_PASSWORD:
$request = $request->withHeader(
'Authorization',
sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password))
);
break;
case Client::AUTH_HTTP_TOKEN:
$request = $request->withHeader('Authorization', sprintf('token %s', $this->tokenOrLogin));
break;
case Client::AUTH_URL_CLIENT_ID:
$uri = $request->getUri();
$query = $uri->getQuery();
$parameters = [
'client_id' => $this->tokenOrLogin,
'client_secret' => $this->password,
];
$query .= empty($query) ? '' : '&';
$query .= utf8_encode(http_build_query($parameters, '', '&'));
$uri = $uri->withQuery($query);
$request = $request->withUri($uri);
break;
case Client::AUTH_URL_TOKEN:
$uri = $request->getUri();
$query = $uri->getQuery();
$parameters = ['access_token' => $this->tokenOrLogin];
$query .= empty($query) ? '' : '&';
$query .= utf8_encode(http_build_query($parameters, '', '&'));
$uri = $uri->withQuery($query);
$request = $request->withUri($uri);
break;
case Client::AUTH_JWT:
$request = $request->withHeader('Authorization', sprintf('Bearer %s', $this->tokenOrLogin));
break;
default:
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
break;
}
$request = $request->withHeader(
'Authorization',
$this->getAuthorizationHeader()
);
return $next($request);
}
private function getAuthorizationHeader(): string
{
switch ($this->method) {
case AuthMethod::CLIENT_ID:
return sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password));
case AuthMethod::ACCESS_TOKEN:
return sprintf('token %s', $this->tokenOrLogin);
case AuthMethod::JWT:
return sprintf('Bearer %s', $this->tokenOrLogin);
default:
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
}
}
}

View File

@@ -5,10 +5,12 @@ namespace Github\HttpClient\Plugin;
use Github\Exception\ApiLimitExceedException;
use Github\Exception\ErrorException;
use Github\Exception\RuntimeException;
use Github\Exception\SsoRequiredException;
use Github\Exception\TwoFactorAuthenticationRequiredException;
use Github\Exception\ValidationFailedException;
use Github\HttpClient\Message\ResponseMediator;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
@@ -16,23 +18,25 @@ use Psr\Http\Message\ResponseInterface;
* @author Joseph Bielawski <stloyd@gmail.com>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class GithubExceptionThrower implements Plugin
final class GithubExceptionThrower implements Plugin
{
/**
* {@inheritdoc}
* @return Promise
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
return $next($request)->then(function (ResponseInterface $response) use ($request) {
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
$this->checkGraphqlErrors($response);
return $response;
}
// If error:
$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
if (null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
$limit = ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
if ((429 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
throw new ApiLimitExceedException($limit, $reset);
}
@@ -46,13 +50,13 @@ class GithubExceptionThrower implements Plugin
$content = ResponseMediator::getContent($response);
if (is_array($content) && isset($content['message'])) {
if (400 === $response->getStatusCode()) {
throw new ErrorException($content['message'], 400);
throw new ErrorException(sprintf('%s (%s)', $content['message'], $response->getReasonPhrase()), 400);
}
if (422 === $response->getStatusCode() && isset($content['errors'])) {
$errors = [];
foreach ($content['errors'] as $error) {
switch ($error['code']) {
switch ($error['code'] ?? null) {
case 'missing':
$errors[] = sprintf('The %s %s does not exist, for resource "%s"', $error['field'], $error['value'], $error['resource']);
break;
@@ -74,13 +78,24 @@ class GithubExceptionThrower implements Plugin
break;
default:
$errors[] = $error['message'];
if (is_string($error)) {
$errors[] = $error;
break;
}
if (isset($error['message'])) {
$errors[] = $error['message'];
}
break;
}
}
throw new ValidationFailedException('Validation Failed: '.implode(', ', $errors), 422);
throw new ValidationFailedException(
$errors ? 'Validation Failed: '.implode(', ', $errors) : 'Validation Failed',
422
);
}
}
@@ -95,7 +110,51 @@ class GithubExceptionThrower implements Plugin
throw new RuntimeException(implode(', ', $errors), 502);
}
if ((403 === $response->getStatusCode()) && $response->hasHeader('X-GitHub-SSO') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 'required;')) {
// The header will look something like this:
// required; url=https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4
// So we strip out the first 14 characters, leaving only the URL.
// @see https://developer.github.com/v3/auth/#authenticating-for-saml-sso
$url = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 14);
throw new SsoRequiredException($url);
}
throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
});
}
/**
* The graphql api doesn't return a 5xx http status for errors. Instead it returns a 200 with an error body.
*
* @throws RuntimeException
*/
private function checkGraphqlErrors(ResponseInterface $response): void
{
if ($response->getStatusCode() !== 200) {
return;
}
$content = ResponseMediator::getContent($response);
if (!is_array($content)) {
return;
}
if (!isset($content['errors']) || !is_array($content['errors'])) {
return;
}
$errors = [];
foreach ($content['errors'] as $error) {
if (isset($error['message'])) {
$errors[] = $error['message'];
}
}
if (empty($errors)) {
return;
}
throw new RuntimeException(implode(', ', $errors));
}
}

View File

@@ -3,7 +3,7 @@
namespace Github\HttpClient\Plugin;
use Http\Client\Common\Plugin\Journal;
use Http\Client\Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
@@ -12,27 +12,33 @@ use Psr\Http\Message\ResponseInterface;
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class History implements Journal
final class History implements Journal
{
/**
* @var ResponseInterface
* @var ResponseInterface|null
*/
private $lastResponse;
/**
* @return ResponseInterface|null
*/
public function getLastResponse()
public function getLastResponse(): ?ResponseInterface
{
return $this->lastResponse;
}
public function addSuccess(RequestInterface $request, ResponseInterface $response)
/**
* @return void
*/
public function addSuccess(RequestInterface $request, ResponseInterface $response): void
{
$this->lastResponse = $response;
}
public function addFailure(RequestInterface $request, Exception $exception)
/**
* @return void
*/
public function addFailure(RequestInterface $request, ClientExceptionInterface $exception): void
{
}
}

View File

@@ -3,6 +3,7 @@
namespace Github\HttpClient\Plugin;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
/**
@@ -10,22 +11,29 @@ use Psr\Http\Message\RequestInterface;
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class PathPrepend implements Plugin
final class PathPrepend implements Plugin
{
/**
* @var string
*/
private $path;
/**
* @param string $path
*/
public function __construct($path)
public function __construct(string $path)
{
$this->path = $path;
}
/**
* {@inheritdoc}
* @param RequestInterface $request
* @param callable $next
* @param callable $first
*
* @return Promise
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
$currentPath = $request->getUri()->getPath();
if (strpos($currentPath, $this->path) !== 0) {