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 FetchRequest extends BaseRequest |
13
|
|
|
{ |
14
|
|
|
protected static $defaultCurlOptions = array( |
15
|
|
|
CURLOPT_HTTPGET => true, |
16
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
17
|
|
|
CURLOPT_MAXREDIRS => 20, |
18
|
|
|
CURLOPT_ENCODING => '', |
19
|
|
|
CURLOPT_RETURNTRANSFER => true, |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
private $headers = array(); |
23
|
|
|
private $errno; |
24
|
|
|
private $error; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $url |
28
|
|
|
* @param IO\IOInterface $io |
29
|
|
|
* @param Config $config |
30
|
|
|
*/ |
31
|
1 |
View Code Duplication |
public function __construct($url, IO\IOInterface $io, Config $config) |
|
|
|
|
32
|
|
|
{ |
33
|
1 |
|
$this->setURL($url); |
34
|
1 |
|
$this->setCA($config->get('capath'), $config->get('cafile')); |
35
|
1 |
|
$this->setupAuthentication( |
36
|
1 |
|
$io, |
37
|
1 |
|
false, |
38
|
1 |
|
$config->get('github-domains') ?: array(), |
39
|
1 |
|
$config->get('gitlab-domains') ?: array() |
40
|
|
|
); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function getCurlOptions() |
44
|
|
|
{ |
45
|
1 |
|
$curlOpts = parent::getCurlOptions(); |
46
|
1 |
|
$curlOpts[CURLOPT_RETURNTRANSFER] = true; |
47
|
1 |
|
$curlOpts[CURLOPT_HEADERFUNCTION] = array($this, 'headerCallback'); |
48
|
1 |
|
return $curlOpts; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
private static function getCurl($key) |
52
|
|
|
{ |
53
|
1 |
|
static $curlCache = array(); |
54
|
|
|
|
55
|
1 |
|
if (isset($curlCache[$key])) { |
56
|
|
|
return $curlCache[$key]; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$ch = curl_init(); |
60
|
1 |
|
Share::setup($ch); |
61
|
|
|
|
62
|
1 |
|
return $curlCache[$key] = $ch; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string|false |
67
|
|
|
*/ |
68
|
1 |
|
public function fetch() |
69
|
|
|
{ |
70
|
1 |
|
$ch = self::getCurl($this->getOriginURL()); |
71
|
1 |
|
curl_setopt_array($ch, $this->getCurlOptions()); |
72
|
|
|
|
73
|
1 |
|
$result = curl_exec($ch); |
74
|
|
|
|
75
|
1 |
|
$this->errno = $errno = curl_errno($ch); |
76
|
1 |
|
$this->error = curl_error($ch); |
77
|
1 |
|
$info = curl_getinfo($ch); |
78
|
|
|
|
79
|
1 |
|
if (!$this->isHTTP()) { |
80
|
|
|
return $result; |
81
|
1 |
|
} elseif ($errno === CURLE_OK && in_array($info['http_code'], array(200, 304))) { |
82
|
1 |
|
return $result; |
83
|
|
|
} else { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getLastError() |
89
|
|
|
{ |
90
|
|
|
if ($this->errno || $this->error) { |
91
|
|
|
return array($this->errno, $this->error); |
92
|
|
|
} else { |
93
|
|
|
return array(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function getLastHeaders() |
98
|
|
|
{ |
99
|
1 |
|
return $this->headers; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function headerCallback($ch, $headerString) |
103
|
|
|
{ |
104
|
1 |
|
$len = strlen($headerString); |
105
|
1 |
|
$this->headers[] = $headerString; |
106
|
1 |
|
return $len; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.