Skip to content

Commit

Permalink
Provides a post download event to fix #8654
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas D Hedding committed Apr 9, 2020
1 parent a7ad186 commit 2e78244
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Composer/Downloader/FileDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -140,7 +141,7 @@ public function download(PackageInterface $package, $path, PackageInterface $pre
->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.
Expand All @@ -157,6 +158,11 @@ public function download(PackageInterface $package, $path, PackageInterface $pre
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);
$eventDispatcher->dispatch($postFileDownloadEvent->getName(), $postFileDownloadEvent);
}

return $fileName;
});
};
Expand Down
10 changes: 10 additions & 0 deletions src/Composer/Plugin/PluginEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class PluginEvents
*/
const PRE_FILE_DOWNLOAD = 'pre-file-download';

/**
* The POST_FILE_DOWNLOAD event occurs after downloading a 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
*
Expand Down
87 changes: 87 additions & 0 deletions src/Composer/Plugin/PostFileDownloadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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
*/
private $checksum;

/**
* @var array
*/
private $url;

/**
* Constructor.
*
* @param string $name The event name
* @param string $fileName The file name
* @param string $checksum The checksum
* @param array $url The 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
*/
public function getChecksum() {
return $this->checksum;
}

/**
* Gets the URL.
*
* The URL array has keys of base, processed and cacheKey.
*
* @return array
*/
public function getUrl() {
return $this->url;
}

}

0 comments on commit 2e78244

Please sign in to comment.