From 13bdf8553a8a4a8ca369d95d75ac1ccdc207c30e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A4nz=20Friederes?= Date: Thu, 11 Jun 2020 21:53:31 +0200 Subject: [PATCH 1/3] Add setProcessedUrl method to PreFileDownloadEvent --- src/Composer/Downloader/FileDownloader.php | 1 + src/Composer/Plugin/PreFileDownloadEvent.php | 12 +++++++++++- src/Composer/Repository/ComposerRepository.php | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index d7c4bcabe..14ea40220 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -127,6 +127,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface if ($eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed']); $eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); + $url['processed'] = $preFileDownloadEvent->getProcessedUrl(); } $checksum = $package->getDistSha1Checksum(); diff --git a/src/Composer/Plugin/PreFileDownloadEvent.php b/src/Composer/Plugin/PreFileDownloadEvent.php index c2751da02..878ab851f 100644 --- a/src/Composer/Plugin/PreFileDownloadEvent.php +++ b/src/Composer/Plugin/PreFileDownloadEvent.php @@ -55,7 +55,7 @@ class PreFileDownloadEvent extends Event } /** - * Retrieves the processed URL this remote filesystem will be used for + * Retrieves the processed URL that will be downloaded * * @return string */ @@ -63,4 +63,14 @@ class PreFileDownloadEvent extends Event { return $this->processedUrl; } + + /** + * Sets the processed URL that will be downloaded + * + * @return string + */ + public function setProcessedUrl($processedUrl) + { + $this->processedUrl = $processedUrl; + } } diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 8848452f2..bc2844df9 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -1015,6 +1015,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito if ($this->eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); + $filename = $preFileDownloadEvent->getProcessedUrl(); } $response = $this->httpDownloader->get($filename, $this->options); @@ -1101,6 +1102,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito if ($this->eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); + $filename = $preFileDownloadEvent->getProcessedUrl(); } $options = $this->options; @@ -1167,6 +1169,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito if ($this->eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); + $filename = $preFileDownloadEvent->getProcessedUrl(); } $options = $lastModifiedTime ? array('http' => array('header' => array('If-Modified-Since: '.$lastModifiedTime))) : array(); From 907367ff438481356ce86a1a27e297b02fd6c7f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A4nz=20Friederes?= Date: Mon, 15 Jun 2020 21:28:27 +0200 Subject: [PATCH 2/3] Fix PHPDoc issue --- src/Composer/Plugin/PreFileDownloadEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Plugin/PreFileDownloadEvent.php b/src/Composer/Plugin/PreFileDownloadEvent.php index 878ab851f..2ae1e5380 100644 --- a/src/Composer/Plugin/PreFileDownloadEvent.php +++ b/src/Composer/Plugin/PreFileDownloadEvent.php @@ -67,7 +67,7 @@ class PreFileDownloadEvent extends Event /** * Sets the processed URL that will be downloaded * - * @return string + * @param string $processedUrl New processed URL */ public function setProcessedUrl($processedUrl) { From 5c13c974286969e4c5298e74c1a3ee66ef6e2bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A4nz=20Friederes?= Date: Mon, 15 Jun 2020 21:43:41 +0200 Subject: [PATCH 3/3] Implement type and context properties in PreFileDownloadEvent --- src/Composer/Downloader/FileDownloader.php | 2 +- src/Composer/Plugin/PreFileDownloadEvent.php | 43 +++++++++++++++++-- .../Repository/ComposerRepository.php | 6 +-- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index 14ea40220..5ea4bdfe8 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -125,7 +125,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface $url = reset($urls); if ($eventDispatcher) { - $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed']); + $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed'], 'package', $package); $eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); $url['processed'] = $preFileDownloadEvent->getProcessedUrl(); } diff --git a/src/Composer/Plugin/PreFileDownloadEvent.php b/src/Composer/Plugin/PreFileDownloadEvent.php index 2ae1e5380..03efe3ab2 100644 --- a/src/Composer/Plugin/PreFileDownloadEvent.php +++ b/src/Composer/Plugin/PreFileDownloadEvent.php @@ -32,18 +32,32 @@ class PreFileDownloadEvent extends Event */ private $processedUrl; + /** + * @var string + */ + private $type; + + /** + * @var mixed + */ + private $context; + /** * Constructor. * - * @param string $name The event name - * @param HttpDownloader $httpDownloader - * @param string $processedUrl + * @param string $name The event name + * @param HttpDownloader $httpDownloader + * @param string $processedUrl + * @param string $type + * @param mixed $context */ - public function __construct($name, HttpDownloader $httpDownloader, $processedUrl) + public function __construct($name, HttpDownloader $httpDownloader, $processedUrl, $type, $context = null) { parent::__construct($name); $this->httpDownloader = $httpDownloader; $this->processedUrl = $processedUrl; + $this->type = $type; + $this->context = $context; } /** @@ -73,4 +87,25 @@ class PreFileDownloadEvent extends Event { $this->processedUrl = $processedUrl; } + + /** + * Returns the type of this download (package, metadata) + * + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Returns the context of this download, if any. + * If this download is of type package, the package object is returned. + * + * @return mixed + */ + public function getContext() + { + return $this->context; + } } diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index bc2844df9..6a2aba6ea 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -1013,7 +1013,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito while ($retries--) { try { if ($this->eventDispatcher) { - $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); + $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename, 'metadata'); $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); $filename = $preFileDownloadEvent->getProcessedUrl(); } @@ -1100,7 +1100,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito while ($retries--) { try { if ($this->eventDispatcher) { - $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); + $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename, 'metadata'); $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); $filename = $preFileDownloadEvent->getProcessedUrl(); } @@ -1167,7 +1167,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $httpDownloader = $this->httpDownloader; if ($this->eventDispatcher) { - $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename); + $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename, 'metadata'); $this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); $filename = $preFileDownloadEvent->getProcessedUrl(); }