Updated GithubRepository.php to use RuntimeCache for http requests

This commit is contained in:
Netkas 2023-10-03 19:08:37 -04:00
parent 09977f9f11
commit b689be3bd5
No known key found for this signature in database
GPG key ID: 5DAF58535614062B

View file

@ -38,6 +38,7 @@
use ncc\Objects\Vault\Password\AccessToken; use ncc\Objects\Vault\Password\AccessToken;
use ncc\Objects\Vault\Password\UsernamePassword; use ncc\Objects\Vault\Password\UsernamePassword;
use ncc\Utilities\Console; use ncc\Utilities\Console;
use ncc\Utilities\RuntimeCache;
use RuntimeException; use RuntimeException;
class GithubRepository implements RepositoryInterface class GithubRepository implements RepositoryInterface
@ -81,8 +82,14 @@
*/ */
private static function getTags(RepositoryConfiguration $repository, string $group, string $project, ?AuthenticationInterface $authentication = null): array private static function getTags(RepositoryConfiguration $repository, string $group, string $project, ?AuthenticationInterface $authentication = null): array
{ {
$curl = curl_init();
$endpoint = sprintf('%s://%s/repos/%s/%s/tags', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project); $endpoint = sprintf('%s://%s/repos/%s/%s/tags', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project);
if(RuntimeCache::exists($endpoint))
{
return RuntimeCache::get($endpoint);
}
$curl = curl_init($endpoint);
$headers = [ $headers = [
'Accept: application/vnd.github+json', 'Accept: application/vnd.github+json',
'X-GitHub-Api-Version: 2022-11-28', 'X-GitHub-Api-Version: 2022-11-28',
@ -94,10 +101,11 @@
$headers = self::injectAuthentication($authentication, $curl, $headers); $headers = self::injectAuthentication($authentication, $curl, $headers);
} }
curl_setopt($curl, CURLOPT_URL, $endpoint); curl_setopt_array($curl, [
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); CURLOPT_RETURNTRANSFER => true,
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, HttpRequestType::GET); CURLOPT_CUSTOMREQUEST => HttpRequestType::GET,
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); CURLOPT_HTTPHEADER => $headers
]);
$results = []; $results = [];
Console::outDebug(sprintf('Fetching tags for %s/%s from %s', $group, $project, $endpoint)); Console::outDebug(sprintf('Fetching tags for %s/%s from %s', $group, $project, $endpoint));
@ -109,6 +117,7 @@
} }
} }
RuntimeCache::set($endpoint, $results);
return $results; return $results;
} }
@ -156,8 +165,14 @@
$tag = self::getLatestTag($repository, $group, $project, $authentication); $tag = self::getLatestTag($repository, $group, $project, $authentication);
} }
$curl = curl_init();
$endpoint = sprintf('%s://%s/repos/%s/%s/zipball/refs/tags/%s', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project, $tag); $endpoint = sprintf('%s://%s/repos/%s/%s/zipball/refs/tags/%s', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project, $tag);
if(RuntimeCache::exists($endpoint))
{
return RuntimeCache::get($endpoint);
}
$curl = curl_init($endpoint);
$headers = [ $headers = [
'User-Agent: ncc' 'User-Agent: ncc'
]; ];
@ -167,12 +182,13 @@
$headers = self::injectAuthentication($authentication, $curl, $headers); $headers = self::injectAuthentication($authentication, $curl, $headers);
} }
curl_setopt($curl, CURLOPT_URL, $endpoint); curl_setopt_array($curl, [
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); CURLOPT_RETURNTRANSFER => true,
curl_setopt($curl, CURLOPT_NOBODY, true); CURLOPT_NOBODY => true,
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, HttpRequestType::GET); CURLOPT_CUSTOMREQUEST => HttpRequestType::GET,
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); CURLOPT_HTTPHEADER => $headers,
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); CURLOPT_FOLLOWLOCATION => true
]);
Console::outDebug(sprintf('Fetching tag archive for %s/%s/%s from %s', $group, $project, $tag, $endpoint)); Console::outDebug(sprintf('Fetching tag archive for %s/%s/%s from %s', $group, $project, $tag, $endpoint));
@ -190,7 +206,11 @@
throw new NetworkException(sprintf('Server responded with HTTP code %s when getting tag archive for %s/%s/%s', $http_code, $group, $project, $tag)); throw new NetworkException(sprintf('Server responded with HTTP code %s when getting tag archive for %s/%s/%s', $http_code, $group, $project, $tag));
} }
return new RepositoryResult(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL), RepositoryResultType::SOURCE, $tag); $result = new RepositoryResult(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL), RepositoryResultType::SOURCE, $tag);
curl_close($curl);
RuntimeCache::set($endpoint, $result);
return $result;
} }
/** /**
@ -207,8 +227,14 @@
*/ */
private static function getReleases(RepositoryConfiguration $repository, string $group, string $project, ?AuthenticationInterface $authentication=null): array private static function getReleases(RepositoryConfiguration $repository, string $group, string $project, ?AuthenticationInterface $authentication=null): array
{ {
$curl = curl_init();
$endpoint = sprintf('%s://%s/repos/%s/%s/releases', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project); $endpoint = sprintf('%s://%s/repos/%s/%s/releases', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project);
if(RuntimeCache::exists($endpoint))
{
return RuntimeCache::get($endpoint);
}
$curl = curl_init($endpoint);
$headers = [ $headers = [
'Accept: application/vnd.github+json', 'Accept: application/vnd.github+json',
'X-GitHub-Api-Version: 2022-11-28', 'X-GitHub-Api-Version: 2022-11-28',
@ -220,10 +246,11 @@
$headers = self::injectAuthentication($authentication, $curl, $headers); $headers = self::injectAuthentication($authentication, $curl, $headers);
} }
curl_setopt($curl, CURLOPT_URL, $endpoint); curl_setopt_array($curl, [
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); CURLOPT_RETURNTRANSFER => true,
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, HttpRequestType::GET); CURLOPT_CUSTOMREQUEST => HttpRequestType::GET,
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); CURLOPT_HTTPHEADER => $headers
]);
Console::outDebug(sprintf('Fetching releases for %s/%s from %s', $group, $project, $endpoint)); Console::outDebug(sprintf('Fetching releases for %s/%s from %s', $group, $project, $endpoint));
@ -283,8 +310,14 @@
$release = self::getLatestRelease($repository, $group, $project, $authentication); $release = self::getLatestRelease($repository, $group, $project, $authentication);
} }
$curl = curl_init();
$endpoint = sprintf('%s://%s/repos/%s/%s/releases/tags/%s', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project, $release); $endpoint = sprintf('%s://%s/repos/%s/%s/releases/tags/%s', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project, $release);
if(RuntimeCache::exists($endpoint))
{
return RuntimeCache::get($endpoint);
}
$curl = curl_init($endpoint);
$headers = [ $headers = [
'Accept: application/vnd.github+json', 'Accept: application/vnd.github+json',
'X-GitHub-Api-Version: 2022-11-28', 'X-GitHub-Api-Version: 2022-11-28',
@ -296,10 +329,11 @@
$headers = self::injectAuthentication($authentication, $curl, $headers); $headers = self::injectAuthentication($authentication, $curl, $headers);
} }
curl_setopt($curl, CURLOPT_URL, $endpoint); curl_setopt_array($curl, [
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); CURLOPT_RETURNTRANSFER => true,
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, HttpRequestType::GET); CURLOPT_CUSTOMREQUEST => HttpRequestType::GET,
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); CURLOPT_HTTPHEADER => $headers
]);
Console::outDebug(sprintf('Fetching release package for %s/%s/%s from %s', $group, $project, $release, $endpoint)); Console::outDebug(sprintf('Fetching release package for %s/%s/%s from %s', $group, $project, $release, $endpoint));
$response = self::processHttpResponse($curl, $group, $project); $response = self::processHttpResponse($curl, $group, $project);
@ -313,7 +347,10 @@
{ {
if(preg_match('/\.ncc$/', $asset['name']) === 1) if(preg_match('/\.ncc$/', $asset['name']) === 1)
{ {
return new RepositoryResult($asset['browser_download_url'], RepositoryResultType::PACKAGE, $release); $result = new RepositoryResult($asset['browser_download_url'], RepositoryResultType::PACKAGE, $release);
RuntimeCache::set($endpoint, $result);
return $result;
} }
} }
@ -342,8 +379,14 @@
$release = self::getLatestRelease($repository, $group, $project, $authentication); $release = self::getLatestRelease($repository, $group, $project, $authentication);
} }
$curl = curl_init();
$endpoint = sprintf('%s://%s/repos/%s/%s/releases/tags/%s', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project, $release); $endpoint = sprintf('%s://%s/repos/%s/%s/releases/tags/%s', ($repository->isSsl() ? 'https' : 'http'), $repository->getHost(), $group, $project, $release);
if(RuntimeCache::exists($endpoint))
{
return RuntimeCache::get($endpoint);
}
$curl = curl_init($endpoint);
$headers = [ $headers = [
'Accept: application/vnd.github+json', 'Accept: application/vnd.github+json',
'X-GitHub-Api-Version: 2022-11-28', 'X-GitHub-Api-Version: 2022-11-28',
@ -355,10 +398,11 @@
$headers = self::injectAuthentication($authentication, $curl, $headers); $headers = self::injectAuthentication($authentication, $curl, $headers);
} }
curl_setopt($curl, CURLOPT_URL, $endpoint); curl_setopt_array($curl, [
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); CURLOPT_RETURNTRANSFER => true,
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, HttpRequestType::GET); CURLOPT_CUSTOMREQUEST => HttpRequestType::GET,
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); CURLOPT_HTTPHEADER => $headers
]);
Console::outDebug(sprintf('Fetching release archive for %s/%s/%s from %s', $group, $project, $release, $endpoint)); Console::outDebug(sprintf('Fetching release archive for %s/%s/%s from %s', $group, $project, $release, $endpoint));
@ -366,15 +410,19 @@
if(isset($response['zipball_url'])) if(isset($response['zipball_url']))
{ {
return new RepositoryResult($response['zipball_url'], RepositoryResultType::SOURCE, $release); $result = new RepositoryResult($response['zipball_url'], RepositoryResultType::SOURCE, $release);
} }
elseif(isset($response['tarball_url']))
if(isset($response['tarball_url']))
{ {
return new RepositoryResult($response['tarball_url'], RepositoryResultType::SOURCE, $release); $result = new RepositoryResult($response['tarball_url'], RepositoryResultType::SOURCE, $release);
}
else
{
throw new NetworkException(sprintf('Failed to get release archive for %s/%s/%s: No archive found', $group, $project, $release));
} }
throw new NetworkException(sprintf('Failed to get release archive for %s/%s/%s: No archive found', $group, $project, $release)); RuntimeCache::set($endpoint, $result);
return $result;
} }
/** /**