From 1883832ddc75feab6d56c3dc0877807592ec74ae Mon Sep 17 00:00:00 2001 From: Lucas Hedding Date: Tue, 14 Apr 2020 15:48:18 -0600 Subject: [PATCH] Provides a post download event (#8655) --- src/Composer/Downloader/FileDownloader.php | 8 +- src/Composer/Plugin/PluginEvents.php | 10 +++ src/Composer/Plugin/PostFileDownloadEvent.php | 85 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/Composer/Plugin/PostFileDownloadEvent.php diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index d13d9f105..77a310888 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -21,6 +21,7 @@ use Composer\Package\Comparer\Comparer; use Composer\Package\PackageInterface; use Composer\Package\Version\VersionParser; use Composer\Plugin\PluginEvents; +use Composer\Plugin\PostFileDownloadEvent; use Composer\Plugin\PreFileDownloadEvent; use Composer\EventDispatcher\EventDispatcher; use Composer\Util\Filesystem; @@ -140,7 +141,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface ->then($accept, $reject); } - return $result->then(function ($result) use ($fileName, $checksum, $url) { + return $result->then(function ($result) use ($fileName, $checksum, $url, $eventDispatcher) { // in case of retry, the first call's Promise chain finally calls this twice at the end, // once with $result being the returned $fileName from $accept, and then once for every // failed request with a null result, which can be skipped. @@ -157,6 +158,11 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url['base'].')'); } + if ($eventDispatcher) { + $postFileDownloadEvent = new PostFileDownloadEvent(PluginEvents::POST_FILE_DOWNLOAD, $fileName, $checksum, $url['processed']); + $eventDispatcher->dispatch($postFileDownloadEvent->getName(), $postFileDownloadEvent); + } + return $fileName; }); }; diff --git a/src/Composer/Plugin/PluginEvents.php b/src/Composer/Plugin/PluginEvents.php index 39cac6798..ff476ba40 100644 --- a/src/Composer/Plugin/PluginEvents.php +++ b/src/Composer/Plugin/PluginEvents.php @@ -49,6 +49,16 @@ class PluginEvents */ const PRE_FILE_DOWNLOAD = 'pre-file-download'; + /** + * The POST_FILE_DOWNLOAD event occurs after downloading a package dist file + * + * The event listener method receives a + * Composer\Plugin\PostFileDownloadEvent instance. + * + * @var string + */ + const POST_FILE_DOWNLOAD = 'post-file-download'; + /** * The PRE_COMMAND_RUN event occurs before a command is executed and lets you modify the input arguments/options * diff --git a/src/Composer/Plugin/PostFileDownloadEvent.php b/src/Composer/Plugin/PostFileDownloadEvent.php new file mode 100644 index 000000000..081e64507 --- /dev/null +++ b/src/Composer/Plugin/PostFileDownloadEvent.php @@ -0,0 +1,85 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Plugin; + +use Composer\EventDispatcher\Event; +use Composer\Util\RemoteFilesystem; + +/** + * The post file download event. + * + * @author Nils Adermann + */ +class PostFileDownloadEvent extends Event +{ + + /** + * @var string + */ + private $fileName; + + /** + * @var string|null + */ + private $checksum; + + /** + * @var string + */ + private $url; + + /** + * Constructor. + * + * @param string $name The event name + * @param string $fileName The file name + * @param string|null $checksum The checksum + * @param string $url The processed url + */ + public function __construct($name, $fileName, $checksum, $url) + { + parent::__construct($name); + $this->fileName = $fileName; + $this->checksum = $checksum; + $this->url = $url; + } + + /** + * Retrieves the target file name location. + * + * @return string + */ + public function getFileName() + { + return $this->fileName; + } + + /** + * Gets the checksum. + * + * @return string|null + */ + public function getChecksum() { + return $this->checksum; + } + + /** + * Gets the processed URL. + * + * @return string + */ + public function getUrl() { + return $this->url; + } + +}