-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUrl.php
212 lines (187 loc) · 4.5 KB
/
Url.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* This file is part of SebastianFeldmann\Git.
*
* (c) Sebastian Feldmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianFeldmann\Git;
use RuntimeException;
/**
* Class Url
*
* Represents a valid repository URL either http or ssh.
*
* @package SebastianFeldmann\Git
* @author Sebastian Feldmann <[email protected]>
* @link https://github.com/sebastianfeldmann/git
* @since Class available since Release 3.8.0
*/
final class Url
{
/**
* @var string
*/
private string $url;
/**
* @var string
*/
private string $scheme;
/**
* @var string
*/
private string $user;
/**
* @var string
*/
private string $host;
/**
* @var string
*/
private string $path;
/**
* @var string
*/
private string $repoName;
public function __construct(string $url)
{
$parsed = $this->parseUrl($url);
$this->url = $url;
$this->scheme = (string) ($parsed['scheme'] ?? '');
$this->user = (string) ($parsed['user'] ?? '');
$this->host = (string) ($parsed['host'] ?? '');
$this->path = (string) ($parsed['path'] ?? '');
$this->repoName = $this->parseRepoName($this->path);
}
/**
* Is the given url an SSH clone URL
*
* @param string $url
* @return bool
*/
public static function isSSHUrl(string $url): bool
{
// should not contain http
// should at least contain one colon
return !str_contains($url, 'http') && str_contains($url, ':');
}
/**
* Returns the full url
*
* @return string
*/
public function getUrl(): string
{
return $this->url;
}
/**
* Returns only the scheme
*
* @return string
*/
public function getScheme(): string
{
return $this->scheme;
}
/**
* Returns only the user
*
* @return string
*/
public function getUser(): string
{
return $this->user;
}
/**
* Returns only the host
*
* @return string
*/
public function getHost(): string
{
return $this->host;
}
/**
* Returns only the path
*
* @return string
*/
public function getPath(): string
{
return $this->path;
}
/**
* Returns the repo name (last path segment of the url)
*
* @return string
*/
public function getRepoName(): string
{
return $this->repoName;
}
/**
* Detect the url components
*
* @param string $url
* @return array<string, int|string>
*/
private function parseUrl(string $url): array
{
// By default, GitHub and gitlab urls can't be parsed by parse_url,
// so we have to make sure we end up with parsable urls
if (self::isSSHUrl($url)) {
$url = $this->convertToValidUrl($url);
}
$parsed = parse_url($url);
if (!is_array($parsed)) {
throw new RuntimeException('can\'t parse repository url');
}
return $parsed;
}
/**
* This converts GitHub and gitlab ssh urls to parsable urls
*
* @param string $url
* @return string
*/
private function convertToValidUrl(string $url): string
{
$url = $this->addMissingScheme($url);
return $this->replaceColonWithSlash($url);
}
/**
* Find the repo name within the url path
*
* @param string $path
* @return string
*/
private function parseRepoName(string $path): string
{
$lastSlashPosition = strrpos($path, '/');
return str_replace('.git', '', substr($path, $lastSlashPosition + 1));
}
/**
* This will add the ssh scheme if it is missing
*
* @param string $url
* @return string
*/
private function addMissingScheme(string $url): string
{
return str_contains($url, 'ssh://') ? $url : 'ssh://' . $url;
}
/**
* Replace the [email protected]:user/repo with github.com/user/repo
*
* @param string $url
* @return string
*/
private function replaceColonWithSlash(string $url): string
{
$lastColonPosition = (int)strrpos($url, ':');
return substr($url, 0, $lastColonPosition) . '/' . substr($url, $lastColonPosition + 1);
}
}