|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the composer-changelogs project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Loïck Piera <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Pyrech\ComposerChangelogs\UrlGenerator; |
|
13
|
|
|
|
|
14
|
|
|
use Pyrech\ComposerChangelogs\Version; |
|
15
|
|
|
|
|
16
|
|
|
abstract class GitBasedUrlGenerator implements UrlGenerator |
|
17
|
|
|
{ |
|
18
|
|
|
const REGEX_USER = '(?P<user>[^/]+)'; |
|
19
|
|
|
const REGEX_REPOSITORY = '(?P<repository>[^/]+)'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns the domain of the service, like "example.org". |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract protected function getDomain(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
37 |
|
public function supports($sourceUrl) |
|
32
|
|
|
{ |
|
33
|
37 |
|
return strpos($sourceUrl, $this->getDomain()) !== false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Generates the canonical http url for a repository. |
|
38
|
|
|
* |
|
39
|
|
|
* It ensures there is no .git part in http url. It also supports ssh urls |
|
40
|
|
|
* by converting them in their http equivalent format. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $sourceUrl |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
29 |
|
protected function generateBaseUrl($sourceUrl) |
|
47
|
|
|
{ |
|
48
|
29 |
|
if ($this->isSshUrl($sourceUrl)) { |
|
49
|
6 |
|
return $this->transformSshUrlIntoHttp($sourceUrl); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
23 |
|
$sourceUrl = parse_url($sourceUrl); |
|
53
|
23 |
|
$pos = strrpos($sourceUrl['path'], '.git'); |
|
54
|
|
|
|
|
55
|
23 |
|
return sprintf( |
|
56
|
23 |
|
'%s://%s%s', |
|
57
|
23 |
|
$sourceUrl['scheme'], |
|
58
|
23 |
|
$sourceUrl['host'], |
|
59
|
23 |
|
$pos === false ? $sourceUrl['path'] : substr($sourceUrl['path'], 0, strrpos($sourceUrl['path'], '.git')) |
|
60
|
23 |
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the version to use for the compare url. |
|
65
|
|
|
* |
|
66
|
|
|
* For dev versions, it returns the commit short hash in full pretty version. |
|
67
|
|
|
* |
|
68
|
|
|
* @param Version $version |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
21 |
|
protected function getCompareVersion(Version $version) |
|
73
|
|
|
{ |
|
74
|
21 |
|
if ($version->isDev()) { |
|
75
|
4 |
|
return substr( |
|
76
|
4 |
|
$version->getFullPretty(), |
|
77
|
4 |
|
strlen($version->getPretty()) + 1 |
|
78
|
4 |
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
21 |
|
return $version->getPretty(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Extracts information like user and repository from the http url. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $sourceUrl |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
8 |
|
protected function extractRepositoryInformation($sourceUrl) |
|
92
|
|
|
{ |
|
93
|
8 |
|
$pattern = '#' . $this->getDomain() . '/' . self::REGEX_USER . '/' . self::REGEX_REPOSITORY . '#'; |
|
94
|
|
|
|
|
95
|
8 |
|
preg_match($pattern, $sourceUrl, $matches); |
|
96
|
|
|
|
|
97
|
8 |
|
if (!isset($matches['user']) || !isset($matches['repository'])) { |
|
98
|
4 |
|
throw new \LogicException( |
|
99
|
4 |
|
sprintf('Unrecognized url format for %s ("%s")', $this->getDomain(), $sourceUrl) |
|
100
|
4 |
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return [ |
|
104
|
8 |
|
'user' => $matches['user'], |
|
105
|
8 |
|
'repository' => $matches['repository'], |
|
106
|
8 |
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns whether an url uses a ssh git protocol. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $url |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
29 |
|
private function isSshUrl($url) |
|
117
|
|
|
{ |
|
118
|
29 |
|
return strpos($url, 'git@') !== false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Transform an ssh git url into an http one. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $url |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
6 |
|
private function transformSshUrlIntoHttp($url) |
|
129
|
|
|
{ |
|
130
|
6 |
|
$pattern = '#git@' . $this->getDomain() . ':' . self::REGEX_USER . '/' . self::REGEX_REPOSITORY . '.git$#'; |
|
131
|
|
|
|
|
132
|
6 |
|
if (preg_match($pattern, $url, $matches)) { |
|
133
|
6 |
|
return sprintf( |
|
134
|
6 |
|
'https://%s/%s/%s', |
|
135
|
6 |
|
$this->getDomain(), |
|
136
|
6 |
|
$matches['user'], |
|
137
|
6 |
|
$matches['repository'] |
|
138
|
6 |
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $url; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|