From 9a4739915770352e937f757f7d553bed24dbbedb Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Thu, 25 May 2017 17:36:15 +0200 Subject: [PATCH 1/8] Display information when downloading a file. --- src/FileFetcher.php | 15 ++++++++++++++- src/Handler.php | 2 +- src/InitialFileFetcher.php | 6 ++++++ src/PrestissimoFileFetcher.php | 11 ++++------- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/FileFetcher.php b/src/FileFetcher.php index 4435306..7b1d547 100644 --- a/src/FileFetcher.php +++ b/src/FileFetcher.php @@ -7,6 +7,7 @@ namespace DrupalComposer\DrupalScaffold; +use Composer\IO\IOInterface; use Composer\Util\Filesystem; use Composer\Util\RemoteFilesystem; @@ -17,12 +18,18 @@ class FileFetcher { */ protected $remoteFilesystem; + /** + * @var \Composer\IO\IOInterface + */ + protected $io; + protected $source; protected $filenames; protected $fs; - public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = []) { + public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], IOInterface $io) { $this->remoteFilesystem = $remoteFilesystem; + $this->io = $io; $this->source = $source; $this->filenames = $filenames; $this->fs = new Filesystem(); @@ -32,7 +39,13 @@ 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)); + $this->io->write("Going to download the file $filename"); + $this->io->write(" from: $url"); + $this->io->write(" to: $destination/$filename"); $this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename); + // Used to put a new line because the remote file system does not put + // one. + $this->io->write(''); }); } diff --git a/src/Handler.php b/src/Handler.php index 7130732..d18e34e 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -117,7 +117,7 @@ public function downloadScaffold() { $fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->composer->getConfig()); $fetcher->fetch($version, $webroot); - $initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial()); + $initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial(), $this->io); $initialFileFetcher->fetch($version, $webroot); // Call post-scaffold scripts. diff --git a/src/InitialFileFetcher.php b/src/InitialFileFetcher.php index 4b46c70..2027c1c 100644 --- a/src/InitialFileFetcher.php +++ b/src/InitialFileFetcher.php @@ -15,7 +15,13 @@ public function fetch($version, $destination) { if (!file_exists($target)) { $url = $this->getUri($sourceFilename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); + $this->io->write("Going to download the file $filename"); + $this->io->write(" from: $url"); + $this->io->write(" to: $target"); $this->remoteFilesystem->copy($url, $url, $target); + // Used to put a new line because the remote file system does not put + // one. + $this->io->write(''); } }); } diff --git a/src/PrestissimoFileFetcher.php b/src/PrestissimoFileFetcher.php index a99aa1d..9c72880 100644 --- a/src/PrestissimoFileFetcher.php +++ b/src/PrestissimoFileFetcher.php @@ -14,19 +14,13 @@ class PrestissimoFileFetcher extends FileFetcher { - /** - * @var \Composer\IO\IOInterface - */ - protected $io; - /** * @var \Composer\Config */ protected $config; public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, Config $config) { - parent::__construct($remoteFilesystem, $source, $filenames); - $this->io = $io; + parent::__construct($remoteFilesystem, $source, $filenames, $io); $this->config = $config; } @@ -43,7 +37,10 @@ protected function fetchWithPrestissimo($version, $destination) { array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) { $url = $this->getUri($filename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); + $this->io->write("Going to download the file $filename"); + $this->io->write(" from: $url"); $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); + $this->io->write(" to: $destination/$filename"); }); $successCnt = $failureCnt = 0; From 96a172664b79fac76897085a5d383539f4cb4d9d Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Wed, 9 Aug 2017 17:29:38 +0200 Subject: [PATCH 2/8] Fix tests. --- tests/FetcherTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/FetcherTest.php b/tests/FetcherTest.php index 0aa5972..57671da 100644 --- a/tests/FetcherTest.php +++ b/tests/FetcherTest.php @@ -63,14 +63,14 @@ 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']); + $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); $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']); + $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); $this->assertFileExists($this->tmpDir . '/sites/default/settings.php'); } From e424668d2360958b52417f72a4d9824b9ff170c6 Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Wed, 9 Aug 2017 17:30:33 +0200 Subject: [PATCH 3/8] Refactor message on signle lines and using colors. Also take the --no-progress option into account. --- src/FileFetcher.php | 27 +++++++++++++++++++-------- src/Handler.php | 21 +++++++++++++++++++-- src/InitialFileFetcher.php | 17 ++++++++++------- src/Plugin.php | 12 ++++++++++++ src/PrestissimoFileFetcher.php | 24 ++++++++++++++++-------- 5 files changed, 76 insertions(+), 25 deletions(-) diff --git a/src/FileFetcher.php b/src/FileFetcher.php index 7b1d547..3677715 100644 --- a/src/FileFetcher.php +++ b/src/FileFetcher.php @@ -23,29 +23,40 @@ class FileFetcher { */ protected $io; + /** + * @var bool + * + * A boolean indicating if progress should be displayed. + */ + protected $progress; + protected $source; protected $filenames; protected $fs; - public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], IOInterface $io) { + public function __construct(RemoteFilesystem $remoteFilesystem, $source, $filenames = [], 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)); - $this->io->write("Going to download the file $filename"); - $this->io->write(" from: $url"); - $this->io->write(" to: $destination/$filename"); - $this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename); - // Used to put a new line because the remote file system does not put - // one. - $this->io->write(''); + 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); + } }); } diff --git a/src/Handler.php b/src/Handler.php index d18e34e..21b7b67 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -33,6 +33,13 @@ class Handler { */ protected $io; + /** + * @var bool + * + * A boolean indicating if progress should be displayed. + */ + protected $progress; + /** * @var \Composer\Package\PackageInterface */ @@ -47,6 +54,7 @@ class Handler { public function __construct(Composer $composer, IOInterface $io) { $this->composer = $composer; $this->io = $io; + $this->progress = TRUE; } /** @@ -66,6 +74,15 @@ protected function getCorePackage($operation) { return NULL; } + /** + * Get the command options. + * + * @param \Composer\Plugin\CommandEvent $event + */ + public function onCmdBeginsEvent(\Composer\Plugin\CommandEvent $event) { + $this->progress = !($event->getInput()->getOption('no-progress')); + } + /** * Marks scaffolding to be processed after an install or update command. * @@ -114,10 +131,10 @@ public function downloadScaffold() { $remoteFs = new RemoteFilesystem($this->io); - $fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->composer->getConfig()); + $fetcher = new PrestissimoFileFetcher($remoteFs, $options['source'], $files, $this->io, $this->progress, $this->composer->getConfig()); $fetcher->fetch($version, $webroot); - $initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial(), $this->io); + $initialFileFetcher = new InitialFileFetcher($remoteFs, $options['source'], $this->getInitial(), $this->io, $this->progress); $initialFileFetcher->fetch($version, $webroot); // Call post-scaffold scripts. diff --git a/src/InitialFileFetcher.php b/src/InitialFileFetcher.php index 2027c1c..1379e6d 100644 --- a/src/InitialFileFetcher.php +++ b/src/InitialFileFetcher.php @@ -15,13 +15,16 @@ public function fetch($version, $destination) { if (!file_exists($target)) { $url = $this->getUri($sourceFilename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); - $this->io->write("Going to download the file $filename"); - $this->io->write(" from: $url"); - $this->io->write(" to: $target"); - $this->remoteFilesystem->copy($url, $url, $target); - // Used to put a new line because the remote file system does not put - // one. - $this->io->write(''); + 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/Plugin.php b/src/Plugin.php index 73fd2a0..58a06cd 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -11,6 +11,8 @@ use Composer\Installer\PackageEvent; use Composer\Installer\PackageEvents; use Composer\IO\IOInterface; +use Composer\Plugin\CommandEvent; +use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginInterface; use Composer\Script\ScriptEvents; @@ -45,9 +47,19 @@ public static function getSubscribedEvents() { //PackageEvents::POST_PACKAGE_UNINSTALL => 'postPackage', //ScriptEvents::POST_INSTALL_CMD => 'postCmd', ScriptEvents::POST_UPDATE_CMD => 'postCmd', + PluginEvents::COMMAND => 'cmdBegins', ); } + /** + * Command begins event callback. + * + * @param \Composer\Plugin\CommandEvent $event + */ + public function cmdBegins(\Composer\Plugin\CommandEvent $event) { + $this->handler->onCmdBeginsEvent($event); + } + /** * Post package event behaviour. * diff --git a/src/PrestissimoFileFetcher.php b/src/PrestissimoFileFetcher.php index 9c72880..37e2457 100644 --- a/src/PrestissimoFileFetcher.php +++ b/src/PrestissimoFileFetcher.php @@ -19,8 +19,8 @@ class PrestissimoFileFetcher extends FileFetcher { */ protected $config; - public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, Config $config) { - parent::__construct($remoteFilesystem, $source, $filenames, $io); + public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $source, array $filenames = [], IOInterface $io, $progress = TRUE, Config $config) { + parent::__construct($remoteFilesystem, $source, $filenames, $io, $progress); $this->config = $config; } @@ -37,10 +37,16 @@ protected function fetchWithPrestissimo($version, $destination) { array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) { $url = $this->getUri($filename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); - $this->io->write("Going to download the file $filename"); - $this->io->write(" from: $url"); - $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); - $this->io->write(" to: $destination/$filename"); + if ($this->progress) { + $this->io->writeError(" - $filename ($url): ", FALSE); + $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); + // Used to put a new line because the remote file system does not put + // one. + $this->io->writeError(''); + } + else { + $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); + } }); $successCnt = $failureCnt = 0; @@ -54,8 +60,10 @@ protected function fetchWithPrestissimo($version, $destination) { $result = $multi->getFinishedResults(); $successCnt += $result['successCnt']; $failureCnt += $result['failureCnt']; - foreach ($result['urls'] as $url) { - $this->io->writeError(" $successCnt/$totalCnt:\t$url", true, \Composer\IO\IOInterface::VERBOSE); + if ($this->progress) { + foreach ($result['urls'] as $url) { + $this->io->writeError(" $successCnt/$totalCnt:\t$url", true, \Composer\IO\IOInterface::VERBOSE); + } } } while ($multi->remain()); } From 57ccee5c6b6367c5b6232543adfe1e7e7cecff32 Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Sat, 12 Aug 2017 19:06:26 +0200 Subject: [PATCH 4/8] Fix fatal error when executing a command that does not have the no-progress option. --- src/Handler.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Handler.php b/src/Handler.php index 21b7b67..60e8716 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -80,7 +80,12 @@ protected function getCorePackage($operation) { * @param \Composer\Plugin\CommandEvent $event */ public function onCmdBeginsEvent(\Composer\Plugin\CommandEvent $event) { - $this->progress = !($event->getInput()->getOption('no-progress')); + if ($event->getInput()->hasOption('no-progress')) { + $this->progress = !($event->getInput()->getOption('no-progress')); + } + else { + $this->progress = TRUE; + } } /** From d35b2bb7ef3e3e1802933880de5b62af0d02d056 Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Fri, 29 Sep 2017 12:49:43 +0200 Subject: [PATCH 5/8] Prestissimo: do not display twice download infos and display info even if not in verbose mode. --- src/PrestissimoFileFetcher.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/PrestissimoFileFetcher.php b/src/PrestissimoFileFetcher.php index 37e2457..9356ad9 100644 --- a/src/PrestissimoFileFetcher.php +++ b/src/PrestissimoFileFetcher.php @@ -37,16 +37,7 @@ protected function fetchWithPrestissimo($version, $destination) { array_walk($this->filenames, function ($filename) use ($version, $destination, &$requests) { $url = $this->getUri($filename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); - if ($this->progress) { - $this->io->writeError(" - $filename ($url): ", FALSE); - $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); - // Used to put a new line because the remote file system does not put - // one. - $this->io->writeError(''); - } - else { - $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); - } + $requests[] = new CopyRequest($url, $destination . '/' . $filename, false, $this->io, $this->config); }); $successCnt = $failureCnt = 0; @@ -62,7 +53,7 @@ protected function fetchWithPrestissimo($version, $destination) { $failureCnt += $result['failureCnt']; if ($this->progress) { foreach ($result['urls'] as $url) { - $this->io->writeError(" $successCnt/$totalCnt:\t$url", true, \Composer\IO\IOInterface::VERBOSE); + $this->io->writeError(" $successCnt/$totalCnt:\t$url", true); } } } while ($multi->remain()); From 7ec41595f48a87e8e1abc7c0eaf0ffdd4c7f8fb5 Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Fri, 29 Sep 2017 13:18:41 +0200 Subject: [PATCH 6/8] Standardize the output with the Composer output. --- src/PrestissimoFileFetcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PrestissimoFileFetcher.php b/src/PrestissimoFileFetcher.php index 9356ad9..2b05672 100644 --- a/src/PrestissimoFileFetcher.php +++ b/src/PrestissimoFileFetcher.php @@ -53,7 +53,7 @@ protected function fetchWithPrestissimo($version, $destination) { $failureCnt += $result['failureCnt']; if ($this->progress) { foreach ($result['urls'] as $url) { - $this->io->writeError(" $successCnt/$totalCnt:\t$url", true); + $this->io->writeError(" - Downloading $successCnt/$totalCnt: $url", true); } } } while ($multi->remain()); From 027cc5047b8c3882eb30b355eb352b3472becd20 Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Sat, 7 Oct 2017 17:24:27 +0200 Subject: [PATCH 7/8] Refactor fetchers to have initial file using prestissimo if available. --- src/FileFetcher.php | 38 ++++++++++++++++++++-------------- src/Handler.php | 9 ++++---- src/InitialFileFetcher.php | 32 ---------------------------- src/PrestissimoFileFetcher.php | 26 +++++++++++++---------- tests/FetcherTest.php | 15 ++++++++++---- 5 files changed, 53 insertions(+), 67 deletions(-) delete mode 100644 src/InitialFileFetcher.php 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 60e8716..681a54e 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -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); 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 57671da..8fdc77e 100644 --- a/tests/FetcherTest.php +++ b/tests/FetcherTest.php @@ -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'); } } From c4f4ddb24fcfa4d2a34ec940611ae1af3f8b58cc Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Sun, 24 Dec 2017 16:37:58 +0100 Subject: [PATCH 8/8] Rename $erase variable into $override. --- src/FileFetcher.php | 4 ++-- src/PrestissimoFileFetcher.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/FileFetcher.php b/src/FileFetcher.php index e208eff..34e8472 100644 --- a/src/FileFetcher.php +++ b/src/FileFetcher.php @@ -42,10 +42,10 @@ public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInter $this->progress = $progress; } - public function fetch($version, $destination, $erase) { + public function fetch($version, $destination, $override) { foreach ($this->filenames as $sourceFilename => $filename) { $target = "$destination/$filename"; - if ($erase || !file_exists($target)) { + if ($override || !file_exists($target)) { $url = $this->getUri($sourceFilename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); if ($this->progress) { diff --git a/src/PrestissimoFileFetcher.php b/src/PrestissimoFileFetcher.php index abc501b..5f6d10e 100644 --- a/src/PrestissimoFileFetcher.php +++ b/src/PrestissimoFileFetcher.php @@ -24,20 +24,20 @@ public function __construct(\Composer\Util\RemoteFilesystem $remoteFilesystem, $ $this->config = $config; } - public function fetch($version, $destination, $erase) { + public function fetch($version, $destination, $override) { if (class_exists(CurlMulti::class)) { - $this->fetchWithPrestissimo($version, $destination, $erase); + $this->fetchWithPrestissimo($version, $destination, $override); return; } - parent::fetch($version, $destination, $erase); + parent::fetch($version, $destination, $override); } - protected function fetchWithPrestissimo($version, $destination, $erase) { + protected function fetchWithPrestissimo($version, $destination, $override) { $requests = []; foreach ($this->filenames as $sourceFilename => $filename) { $target = "$destination/$filename"; - if ($erase || !file_exists($target)) { + if ($override || !file_exists($target)) { $url = $this->getUri($sourceFilename, $version); $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename)); $requests[] = new CopyRequest($url, $target, false, $this->io, $this->config);