Skip to content

Commit 3f1a173

Browse files
Refactor fetchers to have initial file using prestissimo if available.
1 parent 02d1eab commit 3f1a173

File tree

5 files changed

+60
-70
lines changed

5 files changed

+60
-70
lines changed

src/FileFetcher.php

+22-16
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,36 @@ class FileFetcher {
3434
protected $filenames;
3535
protected $fs;
3636

37-
public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], IOInterface $io, $progress = TRUE) {
37+
public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE) {
3838
$this->remoteFilesystem = $remoteFilesystem;
3939
$this->io = $io;
4040
$this->source = $source;
41-
$this->filenames = $filenames;
4241
$this->fs = new Filesystem();
4342
$this->progress = $progress;
4443
}
4544

46-
public function fetch($version, $destination) {
47-
array_walk($this->filenames, function ($filename) use ($version, $destination) {
48-
$url = $this->getUri($filename, $version);
49-
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
50-
if ($this->progress) {
51-
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
52-
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename, $this->progress);
53-
// Used to put a new line because the remote file system does not put
54-
// one.
55-
$this->io->writeError('');
56-
}
57-
else {
58-
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename, $this->progress);
45+
public function fetch($version, $destination, $erase) {
46+
foreach ($this->filenames as $sourceFilename => $filename) {
47+
$target = "$destination/$filename";
48+
if ($erase || !file_exists($target)) {
49+
$url = $this->getUri($sourceFilename, $version);
50+
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
51+
if ($this->progress) {
52+
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
53+
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
54+
// Used to put a new line because the remote file system does not put
55+
// one.
56+
$this->io->writeError('');
57+
}
58+
else {
59+
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
60+
}
5961
}
60-
});
62+
}
63+
}
64+
65+
public function setFilenames(array $filenames) {
66+
$this->filenames = $filenames;
6167
}
6268

6369
protected function getUri($filename, $version) {

src/Handler.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,12 @@ public function downloadScaffold() {
135135

136136
$remoteFs = new RemoteFilesystem($this->io);
137137

138-
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->progress, $this->composer->getConfig());
139-
$fetcher->fetch($version, $webroot);
138+
$fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $this->io, $this->progress, $this->composer->getConfig());
139+
$fetcher->setFilenames(array_combine($files, $files));
140+
$fetcher->fetch($version, $webroot, true);
140141

141-
$initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial(), $this->io, $this->progress);
142-
$initialFileFetcher->fetch($version, $webroot);
142+
$fetcher->setFilenames($this->getInitial());
143+
$fetcher->fetch($version, $webroot, false);
143144

144145
// Call post-scaffold scripts.
145146
$dispatcher->dispatch(self::POST_DRUPAL_SCAFFOLD_CMD);

src/InitialFileFetcher.php

-32
This file was deleted.

src/PrestissimoFileFetcher.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,30 @@ class PrestissimoFileFetcher extends FileFetcher {
1919
*/
2020
protected $config;
2121

22-
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, $progress = TRUE, Config $config) {
23-
parent::__construct($remoteFilesystem, $source, $filenames, $io, $progress);
22+
public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE, Config $config) {
23+
parent::__construct($remoteFilesystem, $source, $io, $progress);
2424
$this->config = $config;
2525
}
2626

27-
public function fetch($version, $destination) {
27+
public function fetch($version, $destination, $erase) {
2828
if (class_exists(CurlMulti::class)) {
29-
$this->fetchWithPrestissimo($version, $destination);
29+
$this->fetchWithPrestissimo($version, $destination, $erase);
3030
return;
3131
}
32-
parent::fetch($version, $destination);
32+
parent::fetch($version, $destination, $erase);
3333
}
3434

35-
protected function fetchWithPrestissimo($version, $destination) {
35+
protected function fetchWithPrestissimo($version, $destination, $erase) {
3636
$requests = [];
37-
array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) {
38-
$url = $this->getUri($filename, $version);
39-
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
40-
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
41-
});
37+
38+
foreach ($this->filenames as $sourceFilename => $filename) {
39+
$target = "$destination/$filename";
40+
if ($erase || !file_exists($target)) {
41+
$url = $this->getUri($sourceFilename, $version);
42+
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
43+
$requests[] = new CopyRequest($url, $target, false, $this->io, $this->config);
44+
}
45+
}
4246

4347
$successCnt = $failureCnt = 0;
4448
$totalCnt = count($requests);

tests/FetcherTest.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ protected function ensureDirectoryExistsAndClear($directory) {
6363
}
6464

6565
public function testFetch() {
66-
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['.htaccess', 'sites/default/default.settings.php'], new NullIO());
67-
$fetcher->fetch('8.1.1', $this->tmpDir);
66+
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
67+
$fetcher->setFilenames([
68+
'.htaccess' => '.htaccess',
69+
'sites/default/default.settings.php' => 'sites/default/default.settings.php',
70+
]);
71+
$fetcher->fetch('8.1.1', $this->tmpDir, true);
6872
$this->assertFileExists($this->tmpDir . '/.htaccess');
6973
$this->assertFileExists($this->tmpDir . '/sites/default/default.settings.php');
7074
}
@@ -73,10 +77,14 @@ public function testFetch() {
7377
* Tests version specific files.
7478
*/
7579
public function testFetchVersionSpecific() {
76-
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['.eslintrc', '.eslintrc.json'], new NullIO());
80+
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
81+
$fetcher->setFilenames([
82+
'.eslintrc' => '.eslintrc',
83+
'.eslintrc.json' => '.eslintrc.json',
84+
]);
7785

7886
$this->setExpectedException(TransportException::class);
79-
$fetcher->fetch('8.2.x', $this->tmpDir);
87+
$fetcher->fetch('8.2.x', $this->tmpDir, true);
8088

8189
$this->assertFileExists($this->tmpDir . '/.eslintrc');
8290
$this->assertFileNotExists($this->tmpDir . '/.eslintrc.json');
@@ -85,15 +93,18 @@ public function testFetchVersionSpecific() {
8593
@unlink($this->tmpDir . '/.eslintrc');
8694

8795
$this->setExpectedException(TransportException::class);
88-
$fetcher->fetch('8.3.x', $this->tmpDir);
96+
$fetcher->fetch('8.3.x', $this->tmpDir, true);
8997

9098
$this->assertFileExists($this->tmpDir . '/.eslintrc.json');
9199
$this->assertFileNotExists($this->tmpDir . '/.eslintrc');
92100
}
93101

94102
public function testInitialFetch() {
95-
$fetcher = new InitialFileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['sites/default/default.settings.php' => 'sites/default/settings.php'], new NullIO());
96-
$fetcher->fetch('8.1.1', $this->tmpDir);
103+
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
104+
$fetcher->setFilenames([
105+
'sites/default/default.settings.php' => 'sites/default/settings.php',
106+
]);
107+
$fetcher->fetch('8.1.1', $this->tmpDir, false);
97108
$this->assertFileExists($this->tmpDir . '/sites/default/settings.php');
98109
}
99110
}

0 commit comments

Comments
 (0)