Skip to content

Commit 027cc50

Browse files
Refactor fetchers to have initial file using prestissimo if available.
1 parent 7ec4159 commit 027cc50

File tree

5 files changed

+53
-67
lines changed

5 files changed

+53
-67
lines changed

src/FileFetcher.php

Lines changed: 22 additions & 16 deletions
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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ public function downloadScaffold() {
136136

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

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

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

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

src/InitialFileFetcher.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/PrestissimoFileFetcher.php

Lines changed: 15 additions & 11 deletions
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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,22 @@ 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
}
7175

7276
public function testInitialFetch() {
73-
$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());
74-
$fetcher->fetch('8.1.1', $this->tmpDir);
77+
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
78+
$fetcher->setFilenames([
79+
'sites/default/default.settings.php' => 'sites/default/settings.php',
80+
]);
81+
$fetcher->fetch('8.1.1', $this->tmpDir, false);
7582
$this->assertFileExists($this->tmpDir . '/sites/default/settings.php');
7683
}
7784
}

0 commit comments

Comments
 (0)