Skip to content

Commit acf5f57

Browse files
Merge pull request #79 from JellyBellyDev/fix/get-composer-method
fix: getComposer method
2 parents b7d41cc + 5791523 commit acf5f57

File tree

8 files changed

+97
-22
lines changed

8 files changed

+97
-22
lines changed

examples/getComposerLite.php examples/getComposerBranches.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
require __DIR__.'/../vendor/autoload.php';
44

55
$client = new Packagist\Api\Client();
6-
$package = $client->getComposerLite('sylius/sylius');
6+
$package = $client->getComposerBranches('sylius/sylius');
77

88
var_export($package);

examples/getComposerReleases.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
5+
$client = new Packagist\Api\Client();
6+
$package = $client->getComposerReleases('sylius/sylius');
7+
8+
var_export($package);

spec/Packagist/Api/ClientSpec.php

+25-9
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,42 @@ public function it_gets_package_details(HttpClient $client, Factory $factory, Re
9898

9999
public function it_gets_composer_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
100100
{
101-
$data = file_get_contents('spec/Packagist/Api/Fixture/get_composer.json');
101+
$data1 = file_get_contents('spec/Packagist/Api/Fixture/v2_get.json');
102+
$data2 = file_get_contents('spec/Packagist/Api/Fixture/v2_get_dev.json');
102103
$response->getBody()->shouldBeCalled()->willReturn($body);
103-
$body->getContents()->shouldBeCalled()->willReturn($data);
104+
$body->getContents()->shouldBeCalledTimes(2)->willReturn($data1, $data2);
105+
106+
$client->request('GET', 'https://packagist.org/p2/sylius/sylius.json')
107+
->shouldBeCalled()
108+
->willReturn($response);
104109

105110
$client->request('GET', 'https://packagist.org/p2/sylius/sylius~dev.json')
106111
->shouldBeCalled()
107112
->willReturn($response);
108113

109-
$packages = [
110-
'1.0.0' => ['name' => 'sylius/sylius', 'version' => '1.0.0']
114+
$data1 = json_decode($data1, true);
115+
$data2 = json_decode($data2, true);
116+
$factoryInput = $data1;
117+
$factoryInput['packages']['sylius/sylius'] = [
118+
...$data1['packages']['sylius/sylius'],
119+
...$data2['packages']['sylius/sylius'],
111120
];
112121

113-
$factory->create(json_decode($data, true))->shouldBeCalled()->willReturn($packages);
122+
$factory->create($factoryInput)->shouldBeCalled()->willReturn([
123+
'packages' => [
124+
'sylius/sylius' => [
125+
['name' => 'sylius/sylius', 'version' => '1.0.0'],
126+
['name' => 'sylius/sylius', 'version' => 'dev-master'],
127+
],
128+
],
129+
]);
114130

115-
$this->getComposer('sylius/sylius')->shouldBe($packages);
131+
$this->getComposer('sylius/sylius');
116132
}
117133

118-
public function it_gets_composer_lite_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
134+
public function it_gets_composer_releases_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
119135
{
120-
$data = file_get_contents('spec/Packagist/Api/Fixture/get_composer_lite.json');
136+
$data = file_get_contents('spec/Packagist/Api/Fixture/v2_get.json');
121137
$response->getBody()->shouldBeCalled()->willReturn($body);
122138
$body->getContents()->shouldBeCalled()->willReturn($data);
123139

@@ -131,7 +147,7 @@ public function it_gets_composer_lite_package_details(HttpClient $client, Factor
131147

132148
$factory->create(json_decode($data, true))->shouldBeCalled()->willReturn($packages);
133149

134-
$this->getComposerLite('sylius/sylius')->shouldBe($packages);
150+
$this->getComposerReleases('sylius/sylius')->shouldBe($packages);
135151
}
136152

137153
public function it_lists_all_package_names(HttpClient $client, Factory $factory, Response $response, Stream $body): void

spec/Packagist/Api/Fixture/get_composer_lite.json

-1
This file was deleted.

spec/Packagist/Api/Fixture/v2_get.json

+1
Large diffs are not rendered by default.

spec/Packagist/Api/Fixture/v2_get_dev.json

+1
Large diffs are not rendered by default.

spec/Packagist/Api/Result/FactorySpec.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ public function it_creates_popular_results()
4040
}
4141
}
4242

43-
public function it_creates_packages()
43+
public function it_creates_packages(): void
4444
{
4545
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get.json'), true);
4646

4747
$this->create($data)->shouldHaveType(Package::class);
4848
}
4949

50-
public function it_creates_composer_packages()
50+
public function it_creates_composer_packages(): void
5151
{
5252
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get_composer.json'), true);
5353

5454
$results = $this->create($data);
5555
$results->shouldHaveCount(1);
5656
$results->shouldBeArray();
5757
foreach ($results as $result) {
58-
$result->shouldBeAnInstanceOf('Packagist\Api\Result\Package');
58+
$result->shouldBeAnInstanceOf(Package::class);
5959

6060
foreach ($result->getVersions() as $version) {
61-
$version->shouldBeAnInstanceOf('Packagist\Api\Result\Version');
61+
$version->shouldBeAnInstanceOf(Package\Version::class);
6262
}
6363
}
6464
}
6565

66-
public function it_creates_composer_lite_packages(): void
66+
public function it_creates_composer_releases_packages(): void
6767
{
68-
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get_composer_lite.json'), true);
68+
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/v2_get.json'), true);
6969

7070
$results = $this->create($data);
7171
$results->shouldHaveCount(1);

src/Packagist/Api/Client.php

+55-5
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,24 @@ public function get(string $package)
9999
}
100100

101101
/**
102-
* @see https://packagist.org/apidoc#get-package-data
103-
* Contains tagged releases and dev versions
102+
* Similar to {@link get()}, but uses Composer metadata which is Packagist's preferred
103+
* way of retrieving details, since responses are cached efficiently as static files
104+
* by the Packagist service. The response lacks some metadata that is provided
105+
* by {@link get()}, see https://packagist.org/apidoc for details.
106+
*
107+
* Caution: Returns an array of packages, you need to select the correct one
108+
* from the indexed array.
109+
*
110+
* @since 1.6
104111
* @param string $package Full qualified name ex : myname/mypackage
105-
* @return Package[] An array of packages, including the requested one.
112+
* @return array An array of packages, including the requested one, containing releases and dev branch versions
106113
*/
107114
public function getComposer(string $package): array
108115
{
109-
return $this->respond(sprintf($this->url('/p2/%s~dev.json'), $package));
116+
return $this->multiRespond(
117+
sprintf($this->url('/p2/%s.json'), $package),
118+
sprintf($this->url('/p2/%s~dev.json'), $package)
119+
);
110120
}
111121

112122
/**
@@ -115,11 +125,22 @@ public function getComposer(string $package): array
115125
* @param string $package Full qualified name ex : myname/mypackage
116126
* @return Package[] An array of packages, including the requested one.
117127
*/
118-
public function getComposerLite(string $package): array
128+
public function getComposerReleases(string $package): array
119129
{
120130
return $this->respond(sprintf($this->url('/p2/%s.json'), $package));
121131
}
122132

133+
/**
134+
* @see https://packagist.org/apidoc#get-package-data
135+
* Contains only dev branches
136+
* @param string $package Full qualified name ex : myname/mypackage
137+
* @return Package[] An array of packages, including the requested one.
138+
*/
139+
public function getComposerBranches(string $package): array
140+
{
141+
return $this->respond(sprintf($this->url('/p2/%s~dev.json'), $package));
142+
}
143+
123144
/**
124145
* Search packages
125146
*
@@ -192,6 +213,35 @@ protected function respond(string $url)
192213
return $this->create($response);
193214
}
194215

216+
/**
217+
* Execute two URLs request, parse and merge the responses by adding the versions from the second URL
218+
* into the versions from the first URL.
219+
*
220+
* @param string $url1
221+
* @param string $url2
222+
* @return array|Package
223+
*/
224+
protected function multiRespond(string $url1, string $url2)
225+
{
226+
$response1 = $this->request($url1);
227+
$response1 = $this->parse((string) $response1);
228+
229+
$response2 = $this->request($url2);
230+
$response2 = $this->parse((string) $response2);
231+
232+
foreach ($response1['packages'] as $name => $package1) {
233+
if (empty($response2['packages'][$name])) {
234+
continue;
235+
}
236+
$response1['packages'][$name] = [
237+
...$response1['packages'][$name],
238+
...$response2['packages'][$name],
239+
];
240+
}
241+
242+
return $this->create($response1);
243+
}
244+
195245
/**
196246
* Execute the request URL
197247
*

0 commit comments

Comments
 (0)