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 as CConfig; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* cache manager for curl handler |
14
|
|
|
* |
15
|
|
|
* Singleton |
16
|
|
|
*/ |
17
|
|
|
final class Factory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* not need Authorization |
21
|
|
|
* @var array { |
22
|
|
|
* 'origin.example.com' => x |
23
|
|
|
* } |
24
|
|
|
*/ |
25
|
|
|
private static $connections = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* need Authorization header |
29
|
|
|
* @var array { |
30
|
|
|
* 'origin.example.com' => x |
31
|
|
|
* } |
32
|
|
|
*/ |
33
|
|
|
private static $authConnections = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* get cached curl handler |
37
|
|
|
* @param string $origin |
38
|
|
|
* @param bool $auth |
39
|
|
|
* @return resource<curl> |
|
|
|
|
40
|
|
|
*/ |
41
|
7 |
|
public static function getConnection($origin, $auth = false) |
42
|
|
|
{ |
43
|
7 |
|
if ($auth) { |
44
|
1 |
|
if (isset(self::$authConnections[$origin])) { |
45
|
1 |
|
return self::$authConnections[$origin]; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return self::$authConnections[$origin] = curl_init(); |
49
|
|
|
} else { |
50
|
7 |
|
if (isset(self::$connections[$origin])) { |
51
|
6 |
|
return self::$connections[$origin]; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return self::$connections[$origin] = curl_init(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $origin domain text |
60
|
|
|
* @param string $url |
61
|
|
|
* @param IO\IOInterface $io |
62
|
|
|
* @param CConfig $config |
63
|
|
|
* @param array $pluginConfig |
64
|
|
|
* @return Aspects\HttpGetRequest |
65
|
|
|
*/ |
66
|
9 |
|
public static function getHttpGetRequest($origin, $url, IO\IOInterface $io, CConfig $config, array $pluginConfig) |
67
|
|
|
{ |
68
|
9 |
|
if (substr($origin, -10) === 'github.com') { |
69
|
1 |
|
$origin = 'github.com'; |
70
|
1 |
|
} |
71
|
9 |
|
$requestClass = __NAMESPACE__ . '\Aspects\\' . self::getRequestClass($origin, $config) . 'Request'; |
72
|
9 |
|
$request = new $requestClass($origin, $url, $io); |
73
|
9 |
|
$request->verbose = $pluginConfig['verbose']; |
74
|
9 |
|
if ($pluginConfig['insecure']) { |
75
|
1 |
|
$request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false; |
76
|
1 |
|
} |
77
|
9 |
|
if (!empty($pluginConfig['cainfo'])) { |
78
|
1 |
|
$request->curlOpts[CURLOPT_CAINFO] = $pluginConfig['cainfo']; |
79
|
1 |
|
} |
80
|
9 |
|
if (!empty($pluginConfig['userAgent'])) { |
81
|
1 |
|
$request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent']; |
82
|
1 |
|
} |
83
|
9 |
|
return $request; |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
private static function getRequestClass($origin, $config) |
87
|
|
|
{ |
88
|
9 |
|
if (in_array($origin, $config->get('github-domains') ?: array())) { |
89
|
1 |
|
return 'GitHub'; |
90
|
|
|
} |
91
|
9 |
|
if (in_array($origin, $config->get('gitlab-domains') ?: array())) { |
92
|
1 |
|
return 'GitLab'; |
93
|
|
|
} |
94
|
8 |
|
return 'HttpGet'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Aspects\JoinPoint |
99
|
|
|
*/ |
100
|
9 |
|
public static function getPreEvent(Aspects\HttpGetRequest $req) |
101
|
|
|
{ |
102
|
9 |
|
$pre = new Aspects\JoinPoint('pre-download', $req); |
103
|
9 |
|
$pre->attach(static::getAspectAuth()); |
104
|
9 |
|
$pre->attach(new Aspects\AspectRedirect); |
105
|
9 |
|
$pre->attach(new Aspects\AspectProxy); |
106
|
9 |
|
return $pre; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Aspects\JoinPoint |
111
|
|
|
*/ |
112
|
7 |
|
public static function getPostEvent(Aspects\HttpGetRequest $req) |
113
|
|
|
{ |
114
|
7 |
|
$post = new Aspects\JoinPoint('post-download', $req); |
115
|
7 |
|
$post->attach(static::getAspectAuth()); |
116
|
7 |
|
return $post; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Aspects\AspectAuth (same instance) |
121
|
|
|
*/ |
122
|
10 |
|
public static function getAspectAuth() |
123
|
|
|
{ |
124
|
10 |
|
static $auth; |
125
|
10 |
|
return $auth ?: $auth = new Aspects\AspectAuth; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.