Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drupal-composer / drupal-scaffold / src / FileFetcher.php
index 4435306bad4eaf420d4de37f30ab740d55ab8ef2..653e7f22f1a8bdb58f2d4ea30e8a5e5e8c162a3e 100644 (file)
@@ -1,15 +1,14 @@
 <?php
 
-/**
- * @file
- * Contains \DrupalComposer\DrupalScaffold\FileFetcher.
- */
-
 namespace DrupalComposer\DrupalScaffold;
 
+use Composer\IO\IOInterface;
 use Composer\Util\Filesystem;
 use Composer\Util\RemoteFilesystem;
 
+/**
+ * Downloads all required files and writes it to the file system.
+ */
 class FileFetcher {
 
   /**
@@ -17,29 +16,70 @@ class FileFetcher {
    */
   protected $remoteFilesystem;
 
+  /**
+   * @var \Composer\IO\IOInterface
+   */
+  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 = []) {
+  /**
+   * Constructs this FileFetcher object.
+   */
+  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));
-      $this->remoteFilesystem->copy($url, $url, $destination . '/' . $filename);
-    });
+  /**
+   * Downloads all required files and writes it to the file system.
+   */
+  public function fetch($version, $destination, $override) {
+    foreach ($this->filenames as $sourceFilename => $filename) {
+      $target = "$destination/$filename";
+      if ($override || !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);
+        }
+      }
+    }
   }
 
+  /**
+   * Set filenames.
+   */
+  public function setFilenames(array $filenames) {
+    $this->filenames = $filenames;
+  }
+
+  /**
+   * Replace filename and version in the source pattern with their values.
+   */
   protected function getUri($filename, $version) {
     $map = [
       '{path}' => $filename,
-      '{version}' => $version
+      '{version}' => $version,
     ];
     return str_replace(array_keys($map), array_values($map), $this->source);
   }