Skip to content

Commit

Permalink
Refactor fetchers to have initial file using prestissimo if available.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentTorregrosa committed Dec 9, 2017
1 parent 7ec4159 commit 027cc50
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 67 deletions.
38 changes: 22 additions & 16 deletions src/FileFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,36 @@ class FileFetcher {
protected $filenames;
protected $fs;

public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], IOInterface $io, $progress = TRUE) {
public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE) {
$this->remoteFilesystem = $remoteFilesystem;
$this->io = $io;
$this->source = $source;
$this->filenames = $filenames;
$this->fs = new Filesystem();
$this->progress = $progress;
}

public function fetch($version, $destination) {
array_walk($this->filenames, function ($filename) use ($version, $destination) {
$url = $this->getUri($filename, $version);
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
if ($this->progress) {
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename, $this->progress);
// Used to put a new line because the remote file system does not put
// one.
$this->io->writeError('');
}
else {
$this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename, $this->progress);
public function fetch($version, $destination, $erase) {
foreach ($this->filenames as $sourceFilename => $filename) {
$target = "$destination/$filename";
if ($erase || !file_exists($target)) {
$url = $this->getUri($sourceFilename, $version);
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
if ($this->progress) {
$this->io->writeError(" - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
// Used to put a new line because the remote file system does not put
// one.
$this->io->writeError('');
}
else {
$this->remoteFilesystem->copy($url, $url, $target, $this->progress);
}
}
});
}
}

public function setFilenames(array $filenames) {
$this->filenames = $filenames;
}

protected function getUri($filename, $version) {
Expand Down
9 changes: 5 additions & 4 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ public function downloadScaffold() {

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

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

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

// Call post-scaffold scripts.
$dispatcher->dispatch(self::POST_DRUPAL_SCAFFOLD_CMD);
Expand Down
32 changes: 0 additions & 32 deletions src/InitialFileFetcher.php

This file was deleted.

26 changes: 15 additions & 11 deletions src/PrestissimoFileFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,30 @@ class PrestissimoFileFetcher extends FileFetcher {
*/
protected $config;

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

public function fetch($version, $destination) {
public function fetch($version, $destination, $erase) {
if (class_exists(CurlMulti::class)) {
$this->fetchWithPrestissimo($version, $destination);
$this->fetchWithPrestissimo($version, $destination, $erase);
return;
}
parent::fetch($version, $destination);
parent::fetch($version, $destination, $erase);
}

protected function fetchWithPrestissimo($version, $destination) {
protected function fetchWithPrestissimo($version, $destination, $erase) {
$requests = [];
array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) {
$url = $this->getUri($filename, $version);
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
$requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config);
});

foreach ($this->filenames as $sourceFilename => $filename) {
$target = "$destination/$filename";
if ($erase || !file_exists($target)) {
$url = $this->getUri($sourceFilename, $version);
$this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
$requests[] = new CopyRequest($url, $target, false, $this->io, $this->config);
}
}

$successCnt = $failureCnt = 0;
$totalCnt = count($requests);
Expand Down
15 changes: 11 additions & 4 deletions tests/FetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,22 @@ protected function ensureDirectoryExistsAndClear($directory) {
}

public function testFetch() {
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['.htaccess', 'sites/default/default.settings.php'], new NullIO());
$fetcher->fetch('8.1.1', $this->tmpDir);
$fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO());
$fetcher->setFilenames([
'.htaccess' => '.htaccess',
'sites/default/default.settings.php' => 'sites/default/default.settings.php',
]);
$fetcher->fetch('8.1.1', $this->tmpDir, true);
$this->assertFileExists($this->tmpDir . '/.htaccess');
$this->assertFileExists($this->tmpDir . '/sites/default/default.settings.php');
}

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

0 comments on commit 027cc50

Please sign in to comment.