diff --git a/src/FileFetcher.php b/src/FileFetcher.php index 3677715..e208eff 100644 --- a/src/FileFetcher.php +++ b/src/FileFetcher.php @@ -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(" - $filename ($url): ", 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(" - $filename ($url): ", 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) { diff --git a/src/Handler.php b/src/Handler.php index e1db55b..5b6cda6 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -135,11 +135,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); diff --git a/src/InitialFileFetcher.php b/src/InitialFileFetcher.php deleted file mode 100644 index 1379e6d..0000000 --- a/src/InitialFileFetcher.php +++ /dev/null @@ -1,32 +0,0 @@ -filenames, function ($filename, $sourceFilename) use ($version, $destination) { - $target = "$destination/$filename"; - if (!file_exists($target)) { - $url = $this->getUri($sourceFilename, $version); - $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); - if ($this->progress) { - $this->io->writeError(" - $filename ($url): ", 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); - } - } - }); - } - -} diff --git a/src/PrestissimoFileFetcher.php b/src/PrestissimoFileFetcher.php index 2b05672..abc501b 100644 --- a/src/PrestissimoFileFetcher.php +++ b/src/PrestissimoFileFetcher.php @@ -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); diff --git a/tests/FetcherTest.php b/tests/FetcherTest.php index 8401aa9..34e1526 100644 --- a/tests/FetcherTest.php +++ b/tests/FetcherTest.php @@ -63,8 +63,12 @@ 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'); } @@ -73,10 +77,14 @@ public function testFetch() { * Tests version specific files. */ public function testFetchVersionSpecific() { - $fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', ['.eslintrc', '.eslintrc.json'], new NullIO()); + $fetcher = new FileFetcher(new RemoteFilesystem(new NullIO()), 'http://cgit.drupalcode.org/drupal/plain/{path}?h={version}', new NullIO()); + $fetcher->setFilenames([ + '.eslintrc' => '.eslintrc', + '.eslintrc.json' => '.eslintrc.json', + ]); $this->setExpectedException(TransportException::class); - $fetcher->fetch('8.2.x', $this->tmpDir); + $fetcher->fetch('8.2.x', $this->tmpDir, true); $this->assertFileExists($this->tmpDir . '/.eslintrc'); $this->assertFileNotExists($this->tmpDir . '/.eslintrc.json'); @@ -85,15 +93,18 @@ public function testFetchVersionSpecific() { @unlink($this->tmpDir . '/.eslintrc'); $this->setExpectedException(TransportException::class); - $fetcher->fetch('8.3.x', $this->tmpDir); + $fetcher->fetch('8.3.x', $this->tmpDir, true); $this->assertFileExists($this->tmpDir . '/.eslintrc.json'); $this->assertFileNotExists($this->tmpDir . '/.eslintrc'); } 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'); } }