Skip to content

Commit 1374e10

Browse files
authored
Use Prestissimo to download files if available (#60)
2 parents 3ad465a + 24a88fc commit 1374e10

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

src/Handler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Composer\Composer;
1111
use Composer\DependencyResolver\Operation\InstallOperation;
1212
use Composer\DependencyResolver\Operation\UpdateOperation;
13+
use Composer\EventDispatcher\EventDispatcher;
1314
use Composer\IO\IOInterface;
1415
use Composer\Package\PackageInterface;
15-
use Composer\EventDispatcher\EventDispatcher;
1616
use Composer\Util\Filesystem;
1717
use Composer\Util\RemoteFilesystem;
1818
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
@@ -113,7 +113,7 @@ public function downloadScaffold() {
113113

114114
$remoteFs = new RemoteFilesystem($this->io);
115115

116-
$fetcher = new FileFetcher($remoteFs, $options['source'], $files);
116+
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->composer->getConfig());
117117
$fetcher->fetch($version, $webroot);
118118

119119
$initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial());

src/InitialFileFetcher.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
namespace DrupalComposer\DrupalScaffold;
99

10-
use Composer\Util\Filesystem;
11-
use Composer\Util\RemoteFilesystem;
12-
1310
class InitialFileFetcher extends FileFetcher {
11+
1412
public function fetch($version, $destination) {
1513
array_walk($this->filenames, function ($filename, $sourceFilename) use ($version, $destination) {
1614
$target = "$destination/$filename";
@@ -21,4 +19,5 @@ public function fetch($version, $destination) {
2119
}
2220
});
2321
}
22+
2423
}

src/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
use Composer\Composer;
1010
use Composer\EventDispatcher\EventSubscriberInterface;
11+
use Composer\Installer\PackageEvent;
1112
use Composer\Installer\PackageEvents;
1213
use Composer\IO\IOInterface;
1314
use Composer\Plugin\PluginInterface;
14-
use Composer\Installer\PackageEvent;
1515
use Composer\Script\ScriptEvents;
1616

1717
/**

src/PrestissimoFileFetcher.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \DrupalComposer\DrupalScaffold\FileFetcher.
6+
*/
7+
8+
namespace DrupalComposer\DrupalScaffold;
9+
10+
use Composer\Config;
11+
use Composer\IO\IOInterface;
12+
use Hirak\Prestissimo\CopyRequest;
13+
use Hirak\Prestissimo\CurlMulti;
14+
15+
class PrestissimoFileFetcher extends FileFetcher {
16+
17+
/**
18+
* @var \Composer\IO\IOInterface
19+
*/
20+
protected $io;
21+
22+
/**
23+
* @var \Composer\Config
24+
*/
25+
protected $config;
26+
27+
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, Config $config) {
28+
parent::__construct($remoteFilesystem, $source, $filenames);
29+
$this->io = $io;
30+
$this->config = $config;
31+
}
32+
33+
public function fetch($version, $destination) {
34+
if (class_exists(CurlMulti::class)) {
35+
$this->fetchWithPrestissimo($version, $destination);
36+
return;
37+
}
38+
parent::fetch($version, $destination);
39+
}
40+
41+
protected function fetchWithPrestissimo($version, $destination) {
42+
$requests = [];
43+
array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) {
44+
$url = $this->getUri($filename, $version);
45+
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
46+
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
47+
});
48+
49+
$successCnt = $failureCnt = 0;
50+
$totalCnt = count($requests);
51+
52+
$multi = new CurlMulti;
53+
$multi->setRequests($requests);
54+
do {
55+
$multi->setupEventLoop();
56+
$multi->wait();
57+
$result = $multi->getFinishedResults();
58+
$successCnt += $result['successCnt'];
59+
$failureCnt += $result['failureCnt'];
60+
foreach ($result['urls'] as $url) {
61+
$this->io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, \Composer\IO\IOInterface::VERBOSE);
62+
}
63+
} while ($multi->remain());
64+
}
65+
66+
}

0 commit comments

Comments
 (0)