From 3f243d37c30d2cdeb378b483c8172d4ef5e100a8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 29 Oct 2021 22:19:03 +0200 Subject: [PATCH] Add temporary metadata for download size tracking --- src/Composer/Downloader/FileDownloader.php | 8 ++++++++ src/Composer/Installer/InstallationManager.php | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index 88a8da385..feefe6421 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -57,6 +57,13 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface protected $eventDispatcher; /** @var ProcessExecutor */ protected $process; + /** + * @var array + * @private + * @internal + */ + public static $downloadMetadata = array(); + /** * @private this is only public for php 5.3 support in closures * @@ -218,6 +225,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface $accept = function ($response) use ($cache, $package, $fileName, $self, &$urls) { $url = reset($urls); $cacheKey = $url['cacheKey']; + FileDownloader::$downloadMetadata[$package->getName()] = @filesize($fileName) ?: $response->getHeader('Content-Length') ?: '?'; if ($cache && !$cache->isReadOnly()) { $self->lastCacheWrites[$package->getName()] = $cacheKey; diff --git a/src/Composer/Installer/InstallationManager.php b/src/Composer/Installer/InstallationManager.php index 3448a96a6..6429e1b56 100644 --- a/src/Composer/Installer/InstallationManager.php +++ b/src/Composer/Installer/InstallationManager.php @@ -23,6 +23,7 @@ use Composer\DependencyResolver\Operation\UpdateOperation; use Composer\DependencyResolver\Operation\UninstallOperation; use Composer\DependencyResolver\Operation\MarkAliasInstalledOperation; use Composer\DependencyResolver\Operation\MarkAliasUninstalledOperation; +use Composer\Downloader\FileDownloader; use Composer\EventDispatcher\EventDispatcher; use Composer\Util\Loop; use Composer\Util\Platform; @@ -65,6 +66,7 @@ class InstallationManager public function reset() { $this->notifiablePackages = array(); + FileDownloader::$downloadMetadata = array(); } /** @@ -662,10 +664,18 @@ class InstallationManager $postData = array('downloads' => array()); foreach ($packages as $package) { - $postData['downloads'][] = array( + $packageNotification = array( 'name' => $package->getPrettyName(), 'version' => $package->getVersion(), ); + if (strpos($repoUrl, 'packagist.org/') !== false) { + if (isset(FileDownloader::$downloadMetadata[$package->getName()])) { + $packageNotification['downloaded'] = FileDownloader::$downloadMetadata[$package->getName()]; + } else { + $packageNotification['downloaded'] = false; + } + } + $postData['downloads'][] = $packageNotification; } $opts = array(