Skip to content

Commit

Permalink
Fixes #115. Support lazy load metadata for composer V1 proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed May 3, 2023
1 parent c0577fe commit b6b8013
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/Mirror/Decorator/ProxyRepositoryFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Packeton\Mirror\Decorator;

use Composer\Downloader\TransportException;
use Composer\Util\Http\Response;
use Packeton\Composer\MetadataMinifier;
use Packeton\Mirror\Exception\MetadataNotFoundException;
use Packeton\Mirror\Model\HttpMetadataTrait;
Expand Down Expand Up @@ -38,11 +39,11 @@ public function findPackageMetadata(string $nameOrUri, int $modifiedSince = null
[$package, ] = \explode('$', $nameOrUri);

$metadata = $this->fetch(__FUNCTION__, func_get_args(), function () use ($package) {
$metadata = $this->lazyFetchPackageMetadata($package);
$metadata = $this->lazyFetchPackageMetadata($package, $hash);

$this->repository->touchRoot();
$this->repository->dumpPackage($package, $metadata);
return new JsonMetadata(\json_encode($metadata, \JSON_UNESCAPED_SLASHES));
$this->repository->dumpPackage($package, $metadata, $hash);
return new JsonMetadata(\is_array($metadata) ? \json_encode($metadata, \JSON_UNESCAPED_SLASHES) : $metadata);
});

$this->rmp->markEnable($package);
Expand Down Expand Up @@ -98,7 +99,7 @@ protected function fetch(string $key, array $args = [], callable $fn = null): Js
return $meta ? : $fn($args);
}

protected function lazyFetchPackageMetadata(string $package): string|array
protected function lazyFetchPackageMetadata(string $package, string &$hash = null): string|array
{
$http = $this->syncService->initHttpDownloader($this->config);

Expand All @@ -116,7 +117,12 @@ protected function lazyFetchPackageMetadata(string $package): string|array
$this->requestMetadataVia2($http, [$package], $apiUrl, fn ($name, array $meta) => $queue->enqueue($meta), $reject);

} elseif ($apiUrl = $this->config->getMetadataV1Url($package)) {
$this->requestMetadataVia1($http, [$package], $apiUrl, fn ($name, array $meta) => $queue->enqueue($meta), $reject, $this->repository->lookupAllProviders());
$fulfilled = static function ($name, Response $meta, string $currentHash = null) use ($queue, &$hash) {
$hash = $currentHash;
$queue->enqueue($meta->getBody());
};

$this->requestMetadataVia1($http, [$package], $apiUrl, $fulfilled, $reject, $this->repository->lookupAllProviders());
}

if (!$queue->isEmpty() && !empty($meta = $queue->dequeue())) {
Expand Down
12 changes: 7 additions & 5 deletions src/Mirror/Model/HttpMetadataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function requestMetadataVia2(HttpDownloader $downloader, iterable $packa
}
}

private function requestMetadataVia1(HttpDownloader $downloader, iterable $packages, string $url, callable $onFulfilled, callable $onReject = null, iterable $providersGenerator = []): void
private function requestMetadataVia1(HttpDownloader $downloader, iterable $packages, string $url, callable $onFulfilled, callable $onReject = null, iterable $providers = []): void
{
$resolvedPackages = $promise = [];
$loop = new SignalLoop($downloader, $this->signal);
Expand All @@ -96,11 +96,13 @@ private function requestMetadataVia1(HttpDownloader $downloader, iterable $packa
$refParameter = $reflect->getParameters()[1] ?? null;
$asArray = $refParameter && $refParameter->getType()?->getName() === 'array';

foreach ($providersGenerator as $providers) {
foreach ($providers as $provider) {
foreach ($packages as $package) {
if (isset($providers[$package])) {
$packUrl = \str_replace(['%package%', '%hash%'], [$package, $hash ?? ''], $url);
$resolvedPackages[$package] = [\str_replace('$', '', $packUrl), $providers[$package]['sha256'] ?? null];
if (isset($provider[$package])) {
$hash = $provider[$package]['sha256'] ?? null;
$packUrl = \str_replace(['%package%', '%hash%'], [$package, $hash ?: ''], $url);
$packUrl = empty($hash) ? \str_replace('$', '', $packUrl) : $packUrl;
$resolvedPackages[$package] = [$packUrl, $hash];
}
}
}
Expand Down

0 comments on commit b6b8013

Please sign in to comment.