|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Metric\System\Packages\Composer; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @package Hal\Metric\System\Packages\Composer |
|
6
|
|
|
*/ |
|
7
|
|
|
class Packagist |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @param $package |
|
12
|
|
|
* @return \StdClass |
|
13
|
|
|
*/ |
|
14
|
|
|
public function get($package) |
|
15
|
|
|
{ |
|
16
|
|
|
$response = new \StdClass; |
|
17
|
|
|
$response->latest = null; |
|
18
|
|
|
$response->license = []; |
|
19
|
|
|
$response->homepage = null; |
|
20
|
|
|
$response->description = null; |
|
21
|
|
|
$response->time = null; |
|
22
|
|
|
$response->zip = null; |
|
23
|
|
|
$response->compare = null; |
|
24
|
|
|
$response->type = 'unknown'; |
|
25
|
|
|
$response->github_stars = 0; |
|
26
|
|
|
$response->github_watchers = 0; |
|
27
|
|
|
$response->github_forks = 0; |
|
28
|
|
|
$response->github_open_issues = 0; |
|
29
|
|
|
$response->download_total = 0; |
|
30
|
|
|
$response->download_monthly = 0; |
|
31
|
|
|
$response->download_daily = 0; |
|
32
|
|
|
$response->favers = 0; |
|
33
|
|
|
|
|
34
|
|
|
if (!preg_match('/\w+\/\w+/', $package)) { |
|
35
|
|
|
return $response; |
|
36
|
|
|
} |
|
37
|
|
|
list($user, $name) = explode('/', $package); |
|
38
|
|
|
$uri = sprintf('https://packagist.org/packages/%s/%s.json', $user, $name); |
|
39
|
|
|
$json = $this->getURIContentAsJson($uri); |
|
40
|
|
|
|
|
41
|
|
|
if (!isset($json->package) || !is_object($json->package)) { |
|
42
|
|
|
return $response; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$response->type = $json->package->type; |
|
46
|
|
|
$response->description = $json->package->description; |
|
47
|
|
|
$response->type = $json->package->type; |
|
48
|
|
|
$response->github_stars = $json->package->github_stars; |
|
49
|
|
|
$response->github_watchers = $json->package->github_watchers; |
|
50
|
|
|
$response->github_forks = $json->package->github_forks; |
|
51
|
|
|
$response->github_open_issues = $json->package->github_open_issues; |
|
52
|
|
|
$response->download_total = $json->package->downloads->total; |
|
53
|
|
|
$response->download_monthly = $json->package->downloads->monthly; |
|
54
|
|
|
$response->download_daily = $json->package->downloads->daily; |
|
55
|
|
|
$response->favers = $json->package->favers; |
|
56
|
|
|
|
|
57
|
|
|
// get latest version |
|
58
|
|
|
$latest = '0.0.0'; |
|
59
|
|
|
foreach ((array)$json->package->versions as $version => $datas) { |
|
60
|
|
|
if ($version[0] === 'v') { |
|
61
|
|
|
$version = substr($version, 1); |
|
62
|
|
|
} |
|
63
|
|
|
if (!preg_match('#^[\.\d]+$#', $version)) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
if ($compare = version_compare($version, $latest) == 1) { |
|
67
|
|
|
$latest = $version; |
|
68
|
|
|
$response->name = $package; |
|
69
|
|
|
$response->latest = $version; |
|
70
|
|
|
$response->license = (array)$datas->license; |
|
71
|
|
|
$response->homepage = $datas->homepage; |
|
72
|
|
|
$response->time = $datas->time; |
|
73
|
|
|
$response->zip = $datas->dist->url; |
|
74
|
|
|
$response->compare = $compare; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $response; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Download the given URI and decode it as JSON. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $uri |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getURIContentAsJson($uri) |
|
89
|
|
|
{ |
|
90
|
|
|
// Get the environment variable. |
|
91
|
|
|
$httpsProxy = getenv('https_proxy'); |
|
92
|
|
|
$context = null; |
|
93
|
|
|
if ('' !== $httpsProxy) { |
|
94
|
|
|
// Create the context. |
|
95
|
|
|
$context = stream_context_create( |
|
96
|
|
|
[ |
|
97
|
|
|
'http' => [ |
|
98
|
|
|
'proxy' => $this->tcpProtocol($httpsProxy), |
|
99
|
|
|
'request_fulluri' => true, |
|
100
|
|
|
], |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return json_decode(@file_get_contents($uri, false, $context)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Replace http or https protocols with tcp for creating the stream context. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $httpsProxy |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
private function tcpProtocol($httpsProxy) |
|
116
|
|
|
{ |
|
117
|
|
|
return str_replace( |
|
118
|
|
|
'https://', |
|
119
|
|
|
'tcp://', |
|
120
|
|
|
str_replace( |
|
121
|
|
|
'http://', |
|
122
|
|
|
'tcp://', |
|
123
|
|
|
$httpsProxy |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|