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 Name of the sfneal composer dependency |
12
|
|
|
*/ |
13
|
|
|
public $package; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string Type of Dependency ('composer' or 'docker') |
17
|
|
|
*/ |
18
|
|
|
public $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string Name of the GitHub package. |
22
|
|
|
*/ |
23
|
|
|
public $packageGitubName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DependenciesService constructor. |
27
|
|
|
* @param string $package |
28
|
|
|
* @param string $type |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $package, string $type = 'composer') |
31
|
|
|
{ |
32
|
|
|
$this->package = $package; |
33
|
|
|
$this->packageGitubName = $this->getGitHubPackageName(); |
34
|
|
|
$this->type = $type; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Retrieve the GitHub package name with alias replacement. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
private function getGitHubPackageName(): string |
43
|
|
|
{ |
44
|
|
|
[$user, $package] = explode('/', $this->package); |
45
|
|
|
|
46
|
|
|
// Replace GitHub username with alias if one is provided |
47
|
|
|
if (array_key_exists($user, config('dependencies.github_alias'))) { |
48
|
|
|
return config('dependencies.github_alias')[$user]."/{$package}"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->package; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve a GitHub URL for a dependency. |
56
|
|
|
* |
57
|
|
|
* @return DependencyUrl |
58
|
|
|
*/ |
59
|
|
|
public function gitHub(): DependencyUrl |
60
|
|
|
{ |
61
|
|
|
return new DependencyUrl("github.com/{$this->packageGitubName}"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Retrieve a Travis CI build status SVG URL for a dependency. |
66
|
|
|
* |
67
|
|
|
* @return DependencySvg |
68
|
|
|
*/ |
69
|
|
|
public function travis(): DependencySvg |
70
|
|
|
{ |
71
|
|
|
return new DependencySvg( |
72
|
|
|
"travis-ci.com/{$this->packageGitubName}", |
73
|
|
|
"travis-ci.com/{$this->packageGitubName}.svg?branch=master", |
74
|
|
|
'' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieve the Dependencies latest version. |
80
|
|
|
* |
81
|
|
|
* @return DependencySvg |
82
|
|
|
*/ |
83
|
|
|
public function version(): DependencySvg |
84
|
|
|
{ |
85
|
|
|
return $this->type == 'composer' ? $this->packagist() : $this->docker(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve date of the last GitHub commit. |
90
|
|
|
* |
91
|
|
|
* @return DependencySvg |
92
|
|
|
*/ |
93
|
|
|
public function lastCommit(): DependencySvg |
94
|
|
|
{ |
95
|
|
|
return new DependencySvg( |
96
|
|
|
"github.com/{$this->packageGitubName}", |
97
|
|
|
"github/last-commit/{$this->packageGitubName}" |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieve a Packagist versions SVG URL for a dependency. |
103
|
|
|
* |
104
|
|
|
* @return DependencySvg |
105
|
|
|
*/ |
106
|
|
|
private function packagist(): DependencySvg |
107
|
|
|
{ |
108
|
|
|
return new DependencySvg( |
109
|
|
|
"packagist.org/packages/{$this->package}", |
110
|
|
|
"packagist/v/{$this->package}.svg" |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retrieve the latest Docker image tag for a dependency. |
116
|
|
|
* |
117
|
|
|
* @return DependencySvg |
118
|
|
|
*/ |
119
|
|
|
private function docker(): DependencySvg |
120
|
|
|
{ |
121
|
|
|
return new DependencySvg( |
122
|
|
|
"hub.docker.com/r/{$this->package}", |
123
|
|
|
"docker/v/{$this->package}.svg?sort=semver" |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|