From 57e1ce6cdb4309603c4f8f891253f16efa54b40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A4nz=20Friederes?= Date: Wed, 16 Sep 2020 10:54:14 +0200 Subject: [PATCH] Change the file download cache key with the processed URL, implement custom cache key --- src/Composer/Downloader/FileDownloader.php | 18 ++++++++------ src/Composer/Plugin/PreFileDownloadEvent.php | 25 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index 9fa498af8..403a4ac08 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -113,6 +113,10 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface $urls[$index] = array( 'base' => $url, 'processed' => $processedUrl, + // we use the complete download url here to avoid conflicting entries + // from different packages, which would potentially allow a given package + // in a third party repo to pre-populate the cache for the same package in + // packagist for example. 'cacheKey' => $this->getCacheKey($package, $processedUrl) ); } @@ -136,6 +140,11 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface if ($eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed'], 'package', $package); $eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); + if ($preFileDownloadEvent->getCustomCacheKey() !== null) { + $url['cacheKey'] = $this->getCacheKey($package, $preFileDownloadEvent->getCustomCacheKey()); + } else if ($preFileDownloadEvent->getProcessedUrl() !== $url['processed']) { + $url['cacheKey'] = $this->getCacheKey($package, $preFileDownloadEvent->getProcessedUrl()); + } $url['processed'] = $preFileDownloadEvent->getProcessedUrl(); } @@ -398,14 +407,9 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface return $url; } - private function getCacheKey(PackageInterface $package, $processedUrl) + private function getCacheKey(PackageInterface $package, $key) { - // we use the complete download url here to avoid conflicting entries - // from different packages, which would potentially allow a given package - // in a third party repo to pre-populate the cache for the same package in - // packagist for example. - $cacheKey = sha1($processedUrl); - + $cacheKey = sha1($key); return $package->getName().'/'.$cacheKey.'.'.$package->getDistType(); } diff --git a/src/Composer/Plugin/PreFileDownloadEvent.php b/src/Composer/Plugin/PreFileDownloadEvent.php index f7b523b94..318558c20 100644 --- a/src/Composer/Plugin/PreFileDownloadEvent.php +++ b/src/Composer/Plugin/PreFileDownloadEvent.php @@ -32,6 +32,11 @@ class PreFileDownloadEvent extends Event */ private $processedUrl; + /** + * @var string + */ + private $customCacheKey; + /** * @var string */ @@ -88,6 +93,26 @@ class PreFileDownloadEvent extends Event $this->processedUrl = $processedUrl; } + /** + * Retrieves a custom package cache key for this download. + * + * @return string + */ + public function getCustomCacheKey() + { + return $this->customCacheKey; + } + + /** + * Sets a custom package cache key for this download. + * + * @param string $customCacheKey New cache key + */ + public function setCustomCacheKey($customCacheKey) + { + $this->customCacheKey = $customCacheKey; + } + /** * Returns the type of this download (package, metadata). *