Skip to content

Commit 1f472eb

Browse files
Cameron Jungenicolas-grekas
Cameron Junge
authored andcommitted
Shorten Symfony Recipe URL Cache Keys
1 parent 0170279 commit 1f472eb

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/Downloader.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private function get(array $urls, bool $isRecipe = false, int $try = 3): array
271271
$options = [];
272272

273273
foreach ($urls as $url) {
274-
$cacheKey = preg_replace('{[^a-z0-9.]}i', '-', $url);
274+
$cacheKey = self::generateCacheKey($url);
275275
$headers = [];
276276

277277
if (preg_match('{^https?://api\.github\.com/}', $url)) {
@@ -300,7 +300,7 @@ private function get(array $urls, bool $isRecipe = false, int $try = 3): array
300300
foreach ($urls as $url) {
301301
$jobs[] = $this->rfs->add($url, $options[$url])->then(function (ComposerResponse $response) use ($url, &$responses) {
302302
if (200 === $response->getStatusCode()) {
303-
$cacheKey = preg_replace('{[^a-z0-9.]}i', '-', $url);
303+
$cacheKey = self::generateCacheKey($url);
304304
$responses[$url] = $this->parseJson($response->getBody(), $url, $cacheKey, $response->getHeaders())->getBody();
305305
}
306306
}, function (\Exception $e) use ($url, &$retries) {
@@ -314,7 +314,7 @@ private function get(array $urls, bool $isRecipe = false, int $try = 3): array
314314
}
315315
$this->rfs->download($urls, function ($url) use ($options, &$responses, &$retries, &$error) {
316316
try {
317-
$cacheKey = preg_replace('{[^a-z0-9.]}i', '-', $url);
317+
$cacheKey = self::generateCacheKey($url);
318318
$origin = method_exists($this->rfs, 'getOrigin') ? $this->rfs::getOrigin($url) : parse_url($url, \PHP_URL_HOST);
319319
$json = $this->rfs->getContents($origin, $url, false, $options[$url]);
320320
if (200 === $this->rfs->findStatusCode($this->rfs->getLastHeaders())) {
@@ -412,4 +412,11 @@ private function initialize()
412412
$this->endpoints[$endpoint] = $config;
413413
}
414414
}
415+
416+
private static function generateCacheKey(string $url): string
417+
{
418+
$url = preg_replace('{^https://api.github.com/repos/([^/]++/[^/]++)/contents/}', '$1/', $url);
419+
420+
return preg_replace('{[^a-z0-9.]}i', '-', $url);
421+
}
415422
}

0 commit comments

Comments
 (0)