Skip to content

Commit

Permalink
Add support for Composer v2 (#205)
Browse files Browse the repository at this point in the history
* Add basic support for Composer v2

* Add support for private packages
  • Loading branch information
karniv00l authored Jun 26, 2020
1 parent ada6f40 commit b456d03
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
package_name_pattern: '[A-Za-z0-9_.-]+/[A-Za-z0-9_./-]+?'
package_name_pattern: '[A-Za-z0-9_.-]+/[A-Za-z0-9_./~-]+?'
organization_pattern: '[a-z0-9_-]+' # remember to change in security.yaml (access_control)
uuid_pattern: '[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}'
dists_dir: '%env(resolve:PROXY_DIST_DIR)%'
Expand Down
18 changes: 18 additions & 0 deletions src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public function provider(string $package): JsonResponse
);
}

/**
* @Route("/p2/{package}.json",
* name="package_provider_v2",
* host="repo.{domain}",
* defaults={"domain"="%domain%"},
* requirements={"package"="%package_name_pattern%","domain"="%domain%"},
* methods={"GET"})
*/
public function providerV2(string $package): JsonResponse
{
return new JsonResponse($this->register->all()
->map(fn (Proxy $proxy) => $proxy->providerDataV2($package))
->find(fn (Option $option) => !$option->isEmpty())
->map(fn (Option $option) => $option->get())
->getOrElse(['packages' => new \stdClass()])
);
}

/**
* @Route("/dists/{package}/{version}/{ref}.{type}",
* name="package_dist",
Expand Down
20 changes: 20 additions & 0 deletions src/Controller/RepoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buddy\Repman\Message\Organization\AddDownload;
use Buddy\Repman\Query\User\Model\Organization;
use Buddy\Repman\Query\User\Model\PackageName;
use Buddy\Repman\Query\User\PackageQuery;
use Buddy\Repman\Service\Organization\PackageManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
Expand Down Expand Up @@ -36,6 +37,7 @@ public function packages(Organization $organization): JsonResponse
{
return new JsonResponse([
'packages' => $this->packageManager->findProviders($organization->alias(), $this->packageQuery->getAllNames($organization->id())),
'metadata-url' => '/p2/%package%.json',
'notify-batch' => $this->generateUrl('repo_package_downloads', [
'organization' => $organization->alias(),
], UrlGeneratorInterface::ABSOLUTE_URL),
Expand Down Expand Up @@ -108,6 +110,24 @@ public function downloads(Request $request, Organization $organization): JsonRes
return new JsonResponse(['status' => 'success'], JsonResponse::HTTP_CREATED);
}

/**
* @Route("/p2/{package}.json",
* host="{organization}.repo.{domain}",
* name="repo_package_provider_v2",
* methods={"GET"},
* defaults={"domain":"%domain%"},
* requirements={"domain"="%domain%","package"="%package_name_pattern%"})
*/
public function providerV2(Organization $organization, string $package): JsonResponse
{
$providerData = $this->packageManager->findProviders(
$organization->alias(),
[new PackageName('', $package)]
);

return new JsonResponse($providerData === [] ? new \stdClass() : $providerData);
}

/**
* @return array<string, string>
*/
Expand Down
38 changes: 37 additions & 1 deletion src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function distFilename(string $package, string $version, string $ref, stri
*/
public function providerData(string $package, int $expireTime = self::PACKAGES_EXPIRE_TIME): Option
{
if (!($fromPath = $this->metadataProvider->fromPath($package, $this->url, $expireTime))->isEmpty()) {
if (!($fromPath = $this->metadataProvider->fromPath('p/'.$package, $this->url, $expireTime))->isEmpty()) {
return $fromPath;
}

Expand All @@ -61,6 +61,23 @@ public function providerData(string $package, int $expireTime = self::PACKAGES_E
return $this->metadataProvider->fromUrl($this->getUrl($providerPath->get()));
}

/**
* @return Option<array<mixed>>
*/
public function providerDataV2(string $package, int $expireTime = self::PACKAGES_EXPIRE_TIME): Option
{
if (!($fromPath = $this->metadataProvider->fromPath('p2/'.$package, $this->url, $expireTime))->isEmpty()) {
return $fromPath;
}

$providerPath = $this->getProviderPathV2($package);
if ($providerPath->isEmpty()) {
return Option::none();
}

return $this->metadataProvider->fromUrl($this->getUrl($providerPath->get()));
}

/**
* @return GenericList<string>
*/
Expand Down Expand Up @@ -115,6 +132,25 @@ private function getProviderPath(string $packageName): Option
return Option::none();
}

/**
* @return Option<string>
*/
private function getProviderPathV2(string $packageName): Option
{
$root = $this->getRootPackages();
if (isset($root['metadata-url'])) {
return Option::some(
(string) str_replace(
['%package%'],
[$packageName],
$root['metadata-url']
)
);
}

return Option::none();
}

/**
* @return array<mixed>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function fromPath(string $package, string $repoUrl, int $expireTime = 0):
return Option::none();
}

return $this->cache->find((string) parse_url($repoUrl, PHP_URL_HOST).'/p/'.$package, $expireTime);
return $this->cache->find((string) parse_url($repoUrl, PHP_URL_HOST).'/'.$package, $expireTime);
}

private function getPath(string $url): string
Expand Down
16 changes: 16 additions & 0 deletions tests/Functional/Controller/ProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public function testProviderActionEmptyPackagesWhenNotExist(): void
', $this->client->getResponse()->getContent());
}

public function testProviderV2Action(): void
{
$this->client->request('GET', '/p2/buddy-works/repman.json', [], [], [
'HTTP_HOST' => 'repo.repman.wip',
]);

self::assertMatchesPattern('
{
"packages":
{
"buddy-works/repman": "@array@"
}
}
', $this->client->getResponse()->getContent());
}

public function testDistributionAction(): void
{
$this->client->request('GET', '/dists/buddy-works/repman/0.1.2.0/f0c896a759d4e2e1eff57978318e841911796305.zip', [], [], [
Expand Down
33 changes: 32 additions & 1 deletion tests/Functional/Controller/RepoControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public function testOrganizationPackagesAction(): void

self::assertMatchesPattern('
{
"packages": {},
"packages": [],
"metadata-url": "/p2/%package%.json",
"notify-batch": "http://buddy.repo.repman.wip/downloads",
"search": "https://packagist.org/search.json?q=%query%&type=%type%",
"mirrors": [
Expand Down Expand Up @@ -160,4 +161,34 @@ public function testAnonymousUserAccess(): void

self::assertTrue($this->client->getResponse()->isOk());
}

public function testProviderV2Action(): void
{
$adminId = $this->createAndLoginAdmin('[email protected]', 'secret');
$this->fixtures->createToken($this->fixtures->createOrganization('buddy', $adminId), 'secret-org-token');

$this->client->request('GET', '/p2/buddy-works/repman.json', [], [], [
'HTTP_HOST' => 'buddy.repo.repman.wip',
'PHP_AUTH_USER' => 'token',
'PHP_AUTH_PW' => 'secret-org-token',
]);

self::assertTrue($this->client->getResponse()->isOk());

self::assertMatchesPattern('
{
"buddy-works/repman": {
"1.2.3": {
"version": "1.2.3",
"version_normalized": "1.2.3.0",
"dist": {
"type": "zip",
"url": "/path/to/reference.zip",
"reference": "ac7dcaf888af2324cd14200769362129c8dd8550"
}
}
}
}
', $this->client->getResponse()->getContent());
}
}
Loading

0 comments on commit b456d03

Please sign in to comment.