Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composer V2 Support #100

Closed
wants to merge 14 commits into from
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# 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_v2: '[A-Za-z0-9_.-]+/[A-Za-z0-9_./~-]+?'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why ~?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to be able to pass through dev packages, and they contain a ~

https://packagist.org/p2/symfony/polyfill-php72~dev.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could merge it in the previous pattern, but i wanted to make it obvious

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we can combine it

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
14 changes: 14 additions & 0 deletions src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function packages(): JsonResponse
],
],
'providers-lazy-url' => '/p/%package%',
// 'providers-api' => $this->generateUrl('index', [], RouterInterface::ABSOLUTE_URL) . '/providers/%package%.json'
]);
}

Expand All @@ -59,6 +60,19 @@ public function provider(string $package): JsonResponse
);
}

/**
* @Route("/p2/{package}.json", name="package_provider_v2", requirements={"package"="%package_name_pattern_v2%"}, 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
36 changes: 36 additions & 0 deletions src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will probably need to do it differently, in this case we will overwrite the v1 data with the v2 version.
If I find a moment, I can finish it, but currently I am still involved in something else, if you would like to finish it yourself, I suggest you do it through an additional parameter and save the data in a separate directory p2

}

/**
* @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