|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* hirak/prestissimo |
|
4
|
|
|
* @author Hiraku NAKANO |
|
5
|
|
|
* @license MIT https://github.com/hirak/prestissimo |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Hirak\Prestissimo; |
|
8
|
|
|
|
|
9
|
|
|
use Composer\IO; |
|
10
|
|
|
use Composer\Config; |
|
11
|
|
|
|
|
12
|
|
|
class CopyRequest extends BaseRequest |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $destination; |
|
16
|
|
|
|
|
17
|
|
|
/** @var resource<stream<plainfile>> */ |
|
18
|
|
|
private $fp; |
|
19
|
|
|
|
|
20
|
|
|
private $success = false; |
|
21
|
|
|
|
|
22
|
|
|
protected static $defaultCurlOptions = array( |
|
23
|
|
|
CURLOPT_HTTPGET => true, |
|
24
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
|
25
|
|
|
CURLOPT_MAXREDIRS => 20, |
|
26
|
|
|
CURLOPT_ENCODING => '', |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $url |
|
31
|
|
|
* @param string $destination |
|
32
|
|
|
* @param bool $useRedirector |
|
33
|
|
|
* @param IO\IOInterface $io |
|
34
|
|
|
* @param Config $config |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->setURL($url); |
|
39
|
|
|
$this->setDestination($destination); |
|
40
|
|
|
$this->setCA($config->get('capath'), $config->get('cafile')); |
|
41
|
|
|
$this->setupAuthentication($io, $useRedirector, $config->get('github-domains'), $config->get('gitlab-domains')); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function __destruct() |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->fp) { |
|
47
|
|
|
fclose($this->fp); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (!$this->success) { |
|
51
|
|
|
if (file_exists($this->destination)) { |
|
52
|
|
|
unlink($this->destination); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function makeSuccess() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->success = true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getCurlOptions() |
|
66
|
|
|
{ |
|
67
|
|
|
$curlOpts = parent::getCurlOptions(); |
|
68
|
|
|
$curlOpts[CURLOPT_FILE] = $this->fp; |
|
69
|
|
|
return $curlOpts; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setDestination($destination) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->destination = $destination; |
|
78
|
|
|
if (is_dir($destination)) { |
|
79
|
|
|
throw new FetchException( |
|
80
|
|
|
'The file could not be written to ' . $destination . '. Directory exists.' |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->createDir($destination); |
|
85
|
|
|
|
|
86
|
|
|
$this->fp = fopen($destination, 'wb'); |
|
87
|
|
|
if (!$this->fp) { |
|
88
|
|
|
throw new FetchException( |
|
89
|
|
|
'The file could not be written to ' . $destination |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function createDir($fileName) |
|
95
|
7 |
|
{ |
|
96
|
|
|
$targetdir = dirname($fileName); |
|
97
|
7 |
|
if (!file_exists($targetdir)) { |
|
98
|
7 |
|
if (!mkdir($targetdir, 0766, true)) { |
|
99
|
6 |
|
throw new FetchException( |
|
100
|
6 |
|
'The file could not be written to ' . $fileName |
|
101
|
6 |
|
); |
|
102
|
6 |
|
} |
|
103
|
6 |
|
} |
|
104
|
6 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|