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
1 <?php
2
3 namespace DrupalComposer\DrupalScaffold;
4
5 use Composer\IO\IOInterface;
6 use Composer\Util\Filesystem;
7 use Composer\Util\RemoteFilesystem;
8
9 /**
10  * Downloads all required files and writes it to the file system.
11  */
12 class FileFetcher {
13
14   /**
15    * @var \Composer\Util\RemoteFilesystem
16    */
17   protected $remoteFilesystem;
18
19   /**
20    * @var \Composer\IO\IOInterface
21    */
22   protected $io;
23
24   /**
25    * @var bool
26    *
27    * A boolean indicating if progress should be displayed.
28    */
29   protected $progress;
30
31   protected $source;
32   protected $filenames;
33   protected $fs;
34
35   /**
36    * Constructs this FileFetcher object.
37    */
38   public function __construct(RemoteFilesystem $remoteFilesystem, $source, IOInterface $io, $progress = TRUE) {
39     $this->remoteFilesystem = $remoteFilesystem;
40     $this->io = $io;
41     $this->source = $source;
42     $this->fs = new Filesystem();
43     $this->progress = $progress;
44   }
45
46   /**
47    * Downloads all required files and writes it to the file system.
48    */
49   public function fetch($version, $destination, $override) {
50     foreach ($this->filenames as $sourceFilename => $filename) {
51       $target = "$destination/$filename";
52       if ($override || !file_exists($target)) {
53         $url = $this->getUri($sourceFilename, $version);
54         $this->fs->ensureDirectoryExists($destination . '/' . dirname($filename));
55         if ($this->progress) {
56           $this->io->writeError("  - <info>$filename</info> (<comment>$url</comment>): ", FALSE);
57           $this->remoteFilesystem->copy($url, $url, $target, $this->progress);
58           // Used to put a new line because the remote file system does not put
59           // one.
60           $this->io->writeError('');
61         }
62         else {
63           $this->remoteFilesystem->copy($url, $url, $target, $this->progress);
64         }
65       }
66     }
67   }
68
69   /**
70    * Set filenames.
71    */
72   public function setFilenames(array $filenames) {
73     $this->filenames = $filenames;
74   }
75
76   /**
77    * Replace filename and version in the source pattern with their values.
78    */
79   protected function getUri($filename, $version) {
80     $map = [
81       '{path}' => $filename,
82       '{version}' => $version,
83     ];
84     return str_replace(array_keys($map), array_values($map), $this->source);
85   }
86
87 }