Skip to content

Commit 9e00b9e

Browse files
committed
Replace PrestissimoFileFetcher with Guzzle
1 parent 80c7d27 commit 9e00b9e

5 files changed

+86
-63
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"require": {
77
"php": ">=5.4.5",
88
"composer-plugin-api": "^1.0.0",
9-
"composer/semver": "^1.4"
9+
"composer/semver": "^1.4",
10+
"guzzlehttp/guzzle": "^6.2.1"
1011
},
1112
"autoload": {
1213
"psr-4": {

src/DrupalScaffoldCommand.php

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ protected function configure() {
2727
* {@inheritdoc}
2828
*/
2929
protected function execute(InputInterface $input, OutputInterface $output) {
30+
$vendorDir = $this->getComposer()->getConfig()->get('vendor-dir');
31+
require $vendorDir . '/autoload.php';
32+
3033
$handler = new Handler($this->getComposer(), $this->getIO());
3134
$handler->downloadScaffold();
3235
// Generate the autoload.php file after generating the scaffold files.

src/GuzzleFileFetcher.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace DrupalComposer\DrupalScaffold;
4+
5+
use Composer\Util\RemoteFilesystem;
6+
use Composer\Config;
7+
use Composer\IO\IOInterface;
8+
use GuzzleHttp\Promise;
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\Pool;
11+
use GuzzleHttp\Psr7\Request;
12+
13+
/**
14+
* Extends the default FileFetcher and uses Guzzle for
15+
* parallel downloads.
16+
*/
17+
class GuzzleFileFetcher extends FileFetcher {
18+
19+
/**
20+
* @var \Composer\Config
21+
*/
22+
protected $config;
23+
24+
/**
25+
* Constructs this RollingCurlFileFetcher object.
26+
*/
27+
public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE, Config $config) {
28+
parent::__construct($remoteFilesystem, $source, $io, $progress);
29+
$this->config = $config;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function fetch($version, $destination, $override) {
36+
$client = new Client();
37+
$requests = [];
38+
$metadata = [];
39+
40+
foreach ($this->filenames as $sourceFilename => $filename) {
41+
$target = "$destination/$filename";
42+
if ($override || !file_exists($target)) {
43+
$url = $this->getUri($sourceFilename, $version);
44+
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
45+
$requests[] = new Request('GET', $url);
46+
$metadata[] = [
47+
'url' => $url,
48+
'target' => $target,
49+
'filename' => $filename,
50+
];
51+
}
52+
}
53+
54+
$pool = new Pool($client, $requests, [
55+
'concurrency' => 5,
56+
'fulfilled' => function ($response, $index) use ($metadata) {
57+
$this->io->writeError(" - <info>{$metadata[$index]['filename']}</info> (<comment>{$metadata[$index]['url']}</comment>): ", TRUE);
58+
file_put_contents($metadata[$index]['target'], (string) $response->getBody());
59+
},
60+
'rejected' => function ($reason, $index) {
61+
throw $reason;
62+
},
63+
]);
64+
65+
$promise = $pool->promise();
66+
$promise->wait();
67+
}
68+
}

src/Handler.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Composer\Semver\Semver;
1515
use Composer\Util\Filesystem;
1616
use Composer\Util\RemoteFilesystem;
17+
use GuzzleHttp\ClientInterface;
1718
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
1819

1920
/**
@@ -69,6 +70,7 @@ protected function manualLoad() {
6970
'CommandProvider',
7071
'DrupalScaffoldCommand',
7172
'FileFetcher',
73+
'GuzzleFileFetcher',
7274
'PrestissimoFileFetcher',
7375
];
7476

@@ -157,8 +159,9 @@ public function downloadScaffold() {
157159
$version = $this->getDrupalCoreVersion($drupalCorePackage);
158160

159161
$remoteFs = new RemoteFilesystem($this->io);
162+
$fetcherClass = $this->getFetcherClass();
160163

161-
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $this->io, $this->progress, $this->composer->getConfig());
164+
$fetcher = new $fetcherClass($remoteFs, $options['source'], $this->io, $this->progress, $this->composer->getConfig());
162165
$fetcher->setFilenames(array_combine($files, $files));
163166
$fetcher->fetch($version, $webroot, TRUE);
164167

@@ -408,4 +411,11 @@ protected function getInitialDefault() {
408411
return [];
409412
}
410413

414+
protected function getFetcherClass() {
415+
if (interface_exists("\\GuzzleHttp\\ClientInterface") && Semver::satisfies(ClientInterface::VERSION, '^6')) {
416+
return GuzzleFileFetcher::class;
417+
}
418+
return FileFetcher::class;
419+
}
420+
411421
}

src/PrestissimoFileFetcher.php

+2-61
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,9 @@
1111
/**
1212
* Extends the default FileFetcher and uses hirak/prestissimo for parallel
1313
* downloads.
14+
*
15+
* @deprecated Use the GuzzleFileFetcher instead.
1416
*/
1517
class PrestissimoFileFetcher extends FileFetcher {
1618

17-
/**
18-
* @var \Composer\Config
19-
*/
20-
protected $config;
21-
22-
/**
23-
* Constructs this PrestissimoFileFetcher object.
24-
*/
25-
public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE, Config $config) {
26-
parent::__construct($remoteFilesystem, $source, $io, $progress);
27-
$this->config = $config;
28-
}
29-
30-
/**
31-
* {@inheritdoc}
32-
*/
33-
public function fetch($version, $destination, $override) {
34-
if (class_exists(CurlMulti::class)) {
35-
$this->fetchWithPrestissimo($version, $destination, $override);
36-
return;
37-
}
38-
parent::fetch($version, $destination, $override);
39-
}
40-
41-
/**
42-
* Fetch files in parallel.
43-
*/
44-
protected function fetchWithPrestissimo($version, $destination, $override) {
45-
$requests = [];
46-
47-
foreach ($this->filenames as $sourceFilename => $filename) {
48-
$target = "$destination/$filename";
49-
if ($override || !file_exists($target)) {
50-
$url = $this->getUri($sourceFilename, $version);
51-
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
52-
$requests[] = new CopyRequest($url, $target, FALSE, $this->io, $this->config);
53-
}
54-
}
55-
56-
$successCnt = $failureCnt = 0;
57-
$totalCnt = count($requests);
58-
if ($totalCnt == 0) {
59-
return;
60-
}
61-
62-
$multi = new CurlMulti();
63-
$multi->setRequests($requests);
64-
do {
65-
$multi->setupEventLoop();
66-
$multi->wait();
67-
$result = $multi->getFinishedResults();
68-
$successCnt += $result['successCnt'];
69-
$failureCnt += $result['failureCnt'];
70-
if ($this->progress) {
71-
foreach ($result['urls'] as $url) {
72-
$this->io->writeError(" - Downloading <comment>$successCnt</comment>/<comment>$totalCnt</comment>: <info>$url</info>", TRUE);
73-
}
74-
}
75-
} while ($multi->remain());
76-
}
77-
7819
}

0 commit comments

Comments
 (0)