|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Dependencies\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Sfneal\Dependencies\Utils\DependencySvg; |
|
6
|
|
|
use Sfneal\Dependencies\Utils\DependencyUrl; |
|
7
|
|
|
|
|
8
|
|
|
class DependenciesService |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string[] Array of supported dependency types |
|
12
|
|
|
*/ |
|
13
|
|
|
private const DEPENDENCY_TYPES = [ |
|
14
|
|
|
'composer', |
|
15
|
|
|
'docker', |
|
16
|
|
|
'python', |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string Name of the dependency |
|
21
|
|
|
*/ |
|
22
|
|
|
public $package; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string Type of Dependency ('composer' or 'docker') |
|
26
|
|
|
*/ |
|
27
|
|
|
public $type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Name of the GitHub package. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $githubRepo; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Name of the project without the GitHub user prefix (used for Python projects). |
|
36
|
|
|
*/ |
|
37
|
|
|
public $project; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* DependenciesService constructor. |
|
41
|
|
|
* @param string $package |
|
42
|
|
|
* @param string $type |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(string $package, string $type = 'composer') |
|
45
|
|
|
{ |
|
46
|
|
|
$this->package = $package; |
|
47
|
|
|
$this->setGitHubRepo($package); |
|
48
|
|
|
$this->setType($type); |
|
49
|
|
|
$this->setProject($package); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Retrieve the GitHub package name with alias replacement. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $fullPackageName |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
private function setGitHubRepo(string $fullPackageName): void |
|
59
|
|
|
{ |
|
60
|
|
|
[$user, $package] = explode('/', $fullPackageName); |
|
61
|
|
|
|
|
62
|
|
|
// Replace GitHub username with alias if one is provided |
|
63
|
|
|
if (array_key_exists($user, config('dependencies.github_alias'))) { |
|
64
|
|
|
$this->githubRepo = config('dependencies.github_alias')[$user]."/{$package}"; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Use default package name |
|
68
|
|
|
else { |
|
69
|
|
|
$this->githubRepo = $fullPackageName; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set the dependencies type. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $type |
|
77
|
|
|
*/ |
|
78
|
|
|
private function setType(string $type): void |
|
79
|
|
|
{ |
|
80
|
|
|
assert( |
|
81
|
|
|
in_array($type, self::DEPENDENCY_TYPES), |
|
82
|
|
|
"'{$type} is not a supported dependency type (supported: ".join(', ', self::DEPENDENCY_TYPES) |
|
83
|
|
|
); |
|
84
|
|
|
$this->type = $type; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set the dependency project name. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $fullPackageName |
|
91
|
|
|
*/ |
|
92
|
|
|
private function setProject(string $fullPackageName): void |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->type == 'python') { |
|
95
|
|
|
[$user, $package] = explode('/', $fullPackageName); |
|
96
|
|
|
$this->project = $package; |
|
97
|
|
|
} else { |
|
98
|
|
|
$this->project = $fullPackageName; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieve a GitHub URL for a dependency. |
|
104
|
|
|
* |
|
105
|
|
|
* @return DependencyUrl |
|
106
|
|
|
*/ |
|
107
|
|
|
public function gitHub(): DependencyUrl |
|
108
|
|
|
{ |
|
109
|
|
|
return new DependencyUrl("github.com/{$this->githubRepo}"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Retrieve a Travis CI build status SVG URL for a dependency. |
|
114
|
|
|
* |
|
115
|
|
|
* @return DependencySvg |
|
116
|
|
|
*/ |
|
117
|
|
|
public function travis(): DependencySvg |
|
118
|
|
|
{ |
|
119
|
|
|
return new DependencySvg( |
|
120
|
|
|
"app.travis-ci.com/{$this->githubRepo}", |
|
121
|
|
|
"app.travis-ci.com/{$this->githubRepo}.svg?branch=master", |
|
122
|
|
|
'' |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Retrieve the Dependencies latest version. |
|
128
|
|
|
* |
|
129
|
|
|
* @return DependencySvg |
|
130
|
|
|
*/ |
|
131
|
|
|
public function version(): DependencySvg |
|
132
|
|
|
{ |
|
133
|
|
|
switch ($this->type) { |
|
134
|
|
|
// Docker |
|
135
|
|
|
case 'docker': |
|
136
|
|
|
return $this->docker(); |
|
137
|
|
|
|
|
138
|
|
|
// Python |
|
139
|
|
|
case 'python': |
|
140
|
|
|
return $this->pypi(); |
|
141
|
|
|
|
|
142
|
|
|
// PHP |
|
143
|
|
|
default: |
|
144
|
|
|
return $this->packagist(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Retrieve date of the last GitHub commit. |
|
150
|
|
|
* |
|
151
|
|
|
* @return DependencySvg |
|
152
|
|
|
*/ |
|
153
|
|
|
public function lastCommit(): DependencySvg |
|
154
|
|
|
{ |
|
155
|
|
|
return new DependencySvg( |
|
156
|
|
|
"github.com/{$this->githubRepo}", |
|
157
|
|
|
"github/last-commit/{$this->githubRepo}" |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Retrieve number of open issues. |
|
163
|
|
|
* |
|
164
|
|
|
* @return DependencySvg |
|
165
|
|
|
*/ |
|
166
|
|
|
public function openIssues(): DependencySvg |
|
167
|
|
|
{ |
|
168
|
|
|
return new DependencySvg( |
|
169
|
|
|
"github.com/{$this->githubRepo}/issues", |
|
170
|
|
|
"github/issues-raw/{$this->githubRepo}" |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Retrieve number of closed issues. |
|
176
|
|
|
* |
|
177
|
|
|
* @return DependencySvg |
|
178
|
|
|
*/ |
|
179
|
|
|
public function closedIssues(): DependencySvg |
|
180
|
|
|
{ |
|
181
|
|
|
return new DependencySvg( |
|
182
|
|
|
"github.com/{$this->githubRepo}/issues?q=is%3Aissue+is%3Aclosed", |
|
183
|
|
|
"github/issues-closed-raw/{$this->githubRepo}" |
|
184
|
|
|
); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Retrieve a Packagist versions SVG URL for a dependency. |
|
189
|
|
|
* |
|
190
|
|
|
* @return DependencySvg |
|
191
|
|
|
*/ |
|
192
|
|
|
private function packagist(): DependencySvg |
|
193
|
|
|
{ |
|
194
|
|
|
return new DependencySvg( |
|
195
|
|
|
"packagist.org/packages/{$this->package}", |
|
196
|
|
|
"packagist/v/{$this->package}.svg" |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Retrieve the latest Docker image tag for a dependency. |
|
202
|
|
|
* |
|
203
|
|
|
* @return DependencySvg |
|
204
|
|
|
*/ |
|
205
|
|
|
private function docker(): DependencySvg |
|
206
|
|
|
{ |
|
207
|
|
|
return new DependencySvg( |
|
208
|
|
|
"hub.docker.com/r/{$this->package}", |
|
209
|
|
|
"docker/v/{$this->package}.svg?sort=semver" |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Retrieve the latest PyPi version a dependency. |
|
215
|
|
|
* |
|
216
|
|
|
* @return DependencySvg |
|
217
|
|
|
*/ |
|
218
|
|
|
private function pypi(): DependencySvg |
|
219
|
|
|
{ |
|
220
|
|
|
$project = explode('/', $this->package)[1]; |
|
221
|
|
|
|
|
222
|
|
|
return new DependencySvg( |
|
223
|
|
|
"pypi.org/project/{$project}", |
|
224
|
|
|
"pypi/v/{$project}.svg" |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|