Browse Source

Provides a post download event (#8655)

Lucas Hedding 5 years ago
parent
commit
1883832ddc

+ 7 - 1
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;
             });
         };

+ 10 - 0
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
      *

+ 85 - 0
src/Composer/Plugin/PostFileDownloadEvent.php

@@ -0,0 +1,85 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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 <naderman@naderman.de>
+ */
+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;
+    }
+
+}