1
0
Fork 0

Merge pull request #9220 from ffraenz/pr

Composer 2: Adapt file download cache keys to changed processed URLs
pull/9278/head
Jordi Boggiano 2020-10-12 16:55:35 +02:00 committed by GitHub
commit a1b781a302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View File

@ -106,6 +106,12 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
throw new \InvalidArgumentException('The given package is missing url information'); throw new \InvalidArgumentException('The given package is missing url information');
} }
$cacheKeyGenerator = function (PackageInterface $package, $key) {
$cacheKey = sha1($key);
return $package->getName().'/'.$cacheKey.'.'.$package->getDistType();
};
$retries = 3; $retries = 3;
$urls = $package->getDistUrls(); $urls = $package->getDistUrls();
foreach ($urls as $index => $url) { foreach ($urls as $index => $url) {
@ -113,7 +119,11 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
$urls[$index] = array( $urls[$index] = array(
'base' => $url, 'base' => $url,
'processed' => $processedUrl, 'processed' => $processedUrl,
'cacheKey' => $this->getCacheKey($package, $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' => $cacheKeyGenerator($package, $processedUrl)
); );
} }
@ -130,12 +140,17 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
$accept = null; $accept = null;
$reject = null; $reject = null;
$download = function () use ($io, $output, $httpDownloader, $cache, $eventDispatcher, $package, $fileName, &$urls, &$accept, &$reject) { $download = function () use ($io, $output, $httpDownloader, $cache, $cacheKeyGenerator, $eventDispatcher, $package, $fileName, &$urls, &$accept, &$reject) {
$url = reset($urls); $url = reset($urls);
if ($eventDispatcher) { if ($eventDispatcher) {
$preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed'], 'package', $package); $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed'], 'package', $package);
$eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); $eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
if ($preFileDownloadEvent->getCustomCacheKey() !== null) {
$url['cacheKey'] = $cacheKeyGenerator($package, $preFileDownloadEvent->getCustomCacheKey());
} else if ($preFileDownloadEvent->getProcessedUrl() !== $url['processed']) {
$url['cacheKey'] = $cacheKeyGenerator($package, $preFileDownloadEvent->getProcessedUrl());
}
$url['processed'] = $preFileDownloadEvent->getProcessedUrl(); $url['processed'] = $preFileDownloadEvent->getProcessedUrl();
} }
@ -398,17 +413,6 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
return $url; return $url;
} }
private function getCacheKey(PackageInterface $package, $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 = sha1($processedUrl);
return $package->getName().'/'.$cacheKey.'.'.$package->getDistType();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* @throws \RuntimeException * @throws \RuntimeException

View File

@ -32,6 +32,11 @@ class PreFileDownloadEvent extends Event
*/ */
private $processedUrl; private $processedUrl;
/**
* @var string|null
*/
private $customCacheKey;
/** /**
* @var string * @var string
*/ */
@ -88,6 +93,26 @@ class PreFileDownloadEvent extends Event
$this->processedUrl = $processedUrl; $this->processedUrl = $processedUrl;
} }
/**
* Retrieves a custom package cache key for this download.
*
* @return string|null
*/
public function getCustomCacheKey()
{
return $this->customCacheKey;
}
/**
* Sets a custom package cache key for this download.
*
* @param string|null $customCacheKey New cache key
*/
public function setCustomCacheKey($customCacheKey)
{
$this->customCacheKey = $customCacheKey;
}
/** /**
* Returns the type of this download (package, metadata). * Returns the type of this download (package, metadata).
* *