Skip to content

Commit acd0b50

Browse files
bug #831 Shorten Symfony Recipe URL Cache Keys (cjunge-work)
This PR was merged into the 1.x branch. Discussion ---------- Shorten Symfony Recipe URL Cache Keys Fixes #830. This change replaces the simple generated composer filesystem cache key with a slightly shorter one only for the Symfony Recipes repos. `/home/cameron/.cache/composer/repo/flex/https---api.github.com-repos-symfony-recipes-contrib-contents-friendsofsymfony.elastica-bundle.5.0.json-ref-flex-main` becomes `/home/cameron/.cache/composer/repo/flex/sf-recipes-contrib-friendsofsymfony.elastica-bundle.5.0.json-ref-flex-main` This won't fix other recipe files hosted on long paths. Commits ------- 1f472eb Shorten Symfony Recipe URL Cache Keys
2 parents 5a4f5a9 + 1f472eb commit acd0b50

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)