Skip to content

Commit

Permalink
fix: getComposer method
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyBellyDev committed Jan 27, 2022
1 parent cf3906e commit 9bc68b0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
require __DIR__.'/../vendor/autoload.php';

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

var_export($package);
8 changes: 8 additions & 0 deletions examples/getComposerReleases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

require __DIR__.'/../vendor/autoload.php';

$client = new Packagist\Api\Client();
$package = $client->getComposerReleases('sylius/sylius');

var_export($package);
6 changes: 3 additions & 3 deletions spec/Packagist/Api/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public function it_gets_composer_package_details(HttpClient $client, Factory $fa
$this->getComposer('sylius/sylius')->shouldBe($packages);
}

public function it_gets_composer_lite_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
public function it_gets_composer_releases_package_details(HttpClient $client, Factory $factory, Response $response, Stream $body): void
{
$data = file_get_contents('spec/Packagist/Api/Fixture/get_composer_lite.json');
$data = file_get_contents('spec/Packagist/Api/Fixture/get_composer_releases.json');
$response->getBody()->shouldBeCalled()->willReturn($body);
$body->getContents()->shouldBeCalled()->willReturn($data);

Expand All @@ -131,7 +131,7 @@ public function it_gets_composer_lite_package_details(HttpClient $client, Factor

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

$this->getComposerLite('sylius/sylius')->shouldBe($packages);
$this->getComposerReleases('sylius/sylius')->shouldBe($packages);
}

public function it_lists_all_package_names(HttpClient $client, Factory $factory, Response $response, Stream $body): void
Expand Down
4 changes: 2 additions & 2 deletions spec/Packagist/Api/Result/FactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function it_creates_composer_packages()
}
}

public function it_creates_composer_lite_packages(): void
public function it_creates_composer_releases_packages(): void
{
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get_composer_lite.json'), true);
$data = json_decode(file_get_contents('spec/Packagist/Api/Fixture/get_composer_releases.json'), true);

$results = $this->create($data);
$results->shouldHaveCount(1);
Expand Down
48 changes: 45 additions & 3 deletions src/Packagist/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ public function get(string $package)

/**
* @see https://packagist.org/apidoc#get-package-data
* Contains tagged releases and dev versions
* Contains tagged releases and dev branches
* @param string $package Full qualified name ex : myname/mypackage
* @return Package[] An array of packages, including the requested one.
*/
public function getComposer(string $package): array
{
return $this->respond(sprintf($this->url('/p2/%s~dev.json'), $package));
return $this->multiRespond(
sprintf($this->url('/p2/%s.json'), $package),
sprintf($this->url('/p2/%s~dev.json'), $package)
);
}

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

/**
* @see https://packagist.org/apidoc#get-package-data
* Contains only dev branches
* @param string $package Full qualified name ex : myname/mypackage
* @return Package[] An array of packages, including the requested one.
*/
public function getComposerBranches(string $package): array
{
return $this->respond(sprintf($this->url('/p2/%s~dev.json'), $package));
}

/**
* Search packages
*
Expand Down Expand Up @@ -192,6 +206,34 @@ protected function respond(string $url)
return $this->create($response);
}

/**
* Execute two urls request, parse and merge the responses
*
* @param string $url1
* @param string $url2
* @return array|Package
*/
protected function multiRespond(string $url1, string $url2)
{
$response1 = $this->request($url1);
$response1 = $this->parse((string)$response1);

$response2 = $this->request($url2);
$response2 = $this->parse((string)$response2);

foreach ($response1['packages'] as $k1 => $package1) {
foreach ($response2['packages'] as $k2 => $package2) {
if ($k1 === $k2) {
foreach ($package2 as $version) {
$response1['packages'][$k1][] = $version;
}
}
}
}

return $this->create($response1);
}

/**
* Execute the request URL
*
Expand Down

0 comments on commit 9bc68b0

Please sign in to comment.