Skip to content

Commit 1f00f52

Browse files
Allow absolute URL metadata patterns
1 parent 3f40b2a commit 1f00f52

5 files changed

Lines changed: 22 additions & 13 deletions

File tree

src/Repository.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
class Repository
66
{
77
public string $url;
8-
public ?string $metadata_pattern;
8+
public string $metadata_pattern;
99

10-
public function __construct(string $url, ?string $metadata_pattern)
10+
public function __construct(string $url, string $metadata_pattern)
1111
{
1212
$this->url = $url;
1313
$this->metadata_pattern = $metadata_pattern;
@@ -16,9 +16,12 @@ public function __construct(string $url, ?string $metadata_pattern)
1616
public function getMetadataURL(string $package): string
1717
{
1818
$url_info = parse_url($this->url);
19-
$path = $this->metadata_pattern != null
20-
? str_replace("%package%", $package, $this->metadata_pattern)
21-
: "/packages/{$package}.json";
22-
return "{$url_info['scheme']}://{$url_info['host']}$path";
19+
$metadata_info = parse_url($this->metadata_pattern);
20+
21+
$scheme = $metadata_info['scheme'] ?? $url_info['scheme'];
22+
$host = $metadata_info['host'] ?? $url_info['host'];
23+
$path = str_replace("%package%", $package, $metadata_info["path"]);
24+
25+
return "$scheme://$host$path";
2326
}
2427
}

src/RepositoryAPI.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public function getInfo(string $url, bool $verbose): ?Repository
2727
try {
2828
$response = $this->http_client->request('GET', "$url/packages.json");
2929
$result = json_decode($response->getBody()->getContents(), true) ?? [];
30-
return new Repository($url, array_key_exists('metadata-url', $result) ? $result['metadata-url'] : null);
30+
31+
$metadata_pattern = array_key_exists('metadata-url', $result)
32+
? $result['metadata-url']
33+
: "/p2/%package%.json";
34+
35+
return new Repository($url, $metadata_pattern);
36+
3137
} catch (GuzzleException $e) {
3238
if ($verbose) {
3339
fwrite($this->stderr, "Could not create repository for $url\n");

tests/CalculatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testSkipsBadRepositories()
105105
]
106106
]);
107107
$repo1 = null;
108-
$repo2 = new Repository('', null);
108+
$repo2 = new Repository('', '');
109109
$api->shouldReceive('getInfo')->andReturn(
110110
$repo1,
111111
$repo2

tests/RepositoryAPITest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testCanGetRepositoryInfo()
4040
$this->assertEquals('/metadata/%package%.json', $repo->metadata_pattern);
4141
}
4242

43-
public function testCanGetRepositoryInfoWithoutMetadata()
43+
public function testCanGetRepositoryInfoWithDefaultMetadataPattern()
4444
{
4545
//arrange
4646
$http_client = Mockery::mock(ClientInterface::class, [
@@ -60,7 +60,7 @@ public function testCanGetRepositoryInfoWithoutMetadata()
6060

6161
//assert
6262
$this->assertEquals('https://composer.example.com', $repo->url);
63-
$this->assertNull($repo->metadata_pattern);
63+
$this->assertEquals('/p2/%package%.json', $repo->metadata_pattern);
6464
}
6565

6666
public function testRepositoryIsNullOnException()

tests/RepositoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public function testCanGetPackageMetadataURL()
1919
$this->assertEquals("https://composer.example.com/metadata/ecoapm/libyear.json", $url);
2020
}
2121

22-
public function testCanGetDefaultPackageMetadataURL()
22+
public function testCanGetAbsoluteMetadataURL()
2323
{
2424
//arrange
25-
$repo = new Repository("https://composer.example.com/packages", null);
25+
$repo = new Repository("https://composer.example.com/packages", "https://packages.example.com/metadata/%package%.json");
2626

2727
//act
2828
$url = $repo->getMetadataURL("ecoapm/libyear");
2929

3030
//assert
31-
$this->assertEquals("https://composer.example.com/packages/ecoapm/libyear.json", $url);
31+
$this->assertEquals("https://packages.example.com/metadata/ecoapm/libyear.json", $url);
3232
}
3333
}

0 commit comments

Comments
 (0)