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
|
56 |
|
public function supports($sourceUrl) |
32
|
|
|
{ |
33
|
56 |
|
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
|
46 |
|
protected function generateBaseUrl($sourceUrl) |
47
|
|
|
{ |
48
|
46 |
|
if ($this->isSshUrl($sourceUrl)) { |
49
|
10 |
|
return $this->transformSshUrlIntoHttp($sourceUrl); |
50
|
|
|
} |
51
|
|
|
|
52
|
36 |
|
$sourceUrl = parse_url($sourceUrl); |
53
|
36 |
|
$pos = strrpos($sourceUrl['path'], '.git'); |
54
|
|
|
|
55
|
36 |
|
return sprintf( |
56
|
36 |
|
'%s://%s%s', |
57
|
36 |
|
$sourceUrl['scheme'], |
58
|
36 |
|
$sourceUrl['host'], |
59
|
36 |
|
$pos === false ? $sourceUrl['path'] : substr($sourceUrl['path'], 0, strrpos($sourceUrl['path'], '.git')) |
60
|
36 |
|
); |
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
|
32 |
|
protected function getCompareVersion(Version $version) |
73
|
|
|
{ |
74
|
32 |
|
if ($version->isDev()) { |
75
|
6 |
|
return substr( |
76
|
6 |
|
$version->getFullPretty(), |
77
|
6 |
|
strlen($version->getPretty()) + 1 |
78
|
6 |
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
32 |
|
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
|
46 |
|
private function isSshUrl($url) |
117
|
|
|
{ |
118
|
46 |
|
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
|
10 |
|
private function transformSshUrlIntoHttp($url) |
129
|
|
|
{ |
130
|
10 |
|
$pattern = '#git@' . $this->getDomain() . ':' . self::REGEX_USER . '/' . self::REGEX_REPOSITORY . '.git$#'; |
131
|
|
|
|
132
|
10 |
|
if (preg_match($pattern, $url, $matches)) { |
133
|
10 |
|
return sprintf( |
134
|
10 |
|
'https://%s/%s/%s', |
135
|
10 |
|
$this->getDomain(), |
136
|
10 |
|
$matches['user'], |
137
|
10 |
|
$matches['repository'] |
138
|
10 |
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $url; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|