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

Set packages.json last modified based on a provider-latest timestamp #283

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(ProxyRegister $register)
*/
public function packages(Request $request): JsonResponse
{
$providerHash = $this->register->getByHost('packagist.org')->latestProviderHash();
$metadata = $this->register->getByHost('packagist.org')->latestProvider();
$response = (new JsonResponse([
'notify-batch' => $this->generateUrl('package_downloads', [], RouterInterface::ABSOLUTE_URL),
'providers-url' => '/p/%package%$%hash%.json',
Expand All @@ -54,12 +54,18 @@ public function packages(Request $request): JsonResponse
],
],
'providers-lazy-url' => '/p/%package%',
'provider-includes' => $providerHash !== null ? ['p/provider-latest$%hash%.json' => ['sha256' => $providerHash]] : [],
'provider-includes' => $metadata->isPresent() ? ['p/provider-latest$%hash%.json' => ['sha256' => $metadata->get()->hash()]] : [],
]))
->setPublic()
->setTtl(360)
;

$now = new \DateTime();
$response->setLastModified(
$metadata->isPresent() ?
$now->setTimestamp($metadata->get()->timestamp()) :
$now
);

$response->isNotModified($request);

return $response;
Expand Down
19 changes: 14 additions & 5 deletions src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,25 @@ public function providers(string $version, string $hash): Option
return $this->fetchMetadata(sprintf('%s/provider/provider-%s$%s.json', $this->url, $version, $hash));
}

public function latestProviderHash(): ?string
/**
* @return Option<Metadata>
*/
public function latestProvider(): Option
{
foreach ($this->filesystem->listContents($this->name.'/provider') as $file) {
if ($file['type'] === 'file' && $file['extension'] === 'json') {
preg_match('/\$(?<hash>.+)$/', $file['filename'], $matches);

return $matches['hash'];
if (($hash = $matches['hash']) !== null) {
return $this->fetchMetadata(
sprintf('%s/provider/provider-latest$%s.json', $this->url, $hash),
$hash
);
}
}
}

return null;
return Option::none();
}

/**
Expand Down Expand Up @@ -249,7 +257,7 @@ private function decodeMetadata(string $package): array
/**
* @return Option<Metadata>
*/
private function fetchMetadata(string $url): Option
private function fetchMetadata(string $url, ?string $hash = null): Option
{
$path = $this->metadataPath($url);
if (!$this->filesystem->has($path)) {
Expand All @@ -263,7 +271,8 @@ private function fetchMetadata(string $url): Option

return Option::some(new Metadata(
(int) $this->filesystem->getTimestamp($path),
$stream
$stream,
$hash
));
}

Expand Down
10 changes: 9 additions & 1 deletion src/Service/Proxy/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ final class Metadata
*/
private $stream;

private ?string $hash = null;

/**
* @param resource $stream
*/
public function __construct(int $timestamp, $stream)
public function __construct(int $timestamp, $stream, ?string $hash = null)
{
$this->timestamp = $timestamp;
$this->stream = $stream;
$this->hash = $hash;
}

public static function fromString(string $string): self
Expand All @@ -41,4 +44,9 @@ public function stream()
{
return $this->stream;
}

public function hash(): ?string
{
return $this->hash;
}
}