Skip to content

Commit

Permalink
Merge pull request #215 from Spomky-Labs/temp-a2ed25
Browse files Browse the repository at this point in the history
Merge up 1.2.1 to 1.3.x
  • Loading branch information
Spomky authored Jun 2, 2024
2 parents 664bfa2 + c1419d2 commit 071a923
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 101 deletions.
31 changes: 21 additions & 10 deletions src/DataCollector/PwaCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,24 @@ public function collect(Request $request, Response $response, Throwable $excepti
$this->data['serviceWorker'] = [
'enabled' => $this->serviceWorker->enabled,
'data' => $this->serviceWorker,
'files' => $swFiles,
'files' => $this->dataToFiles($swFiles),
];
$manifestFiles = $this->manifestCompiler->getFiles();
$manifestFiles = is_array($manifestFiles) ? $manifestFiles : iterator_to_array($manifestFiles);
$this->data['manifest'] = [
'enabled' => $this->serviceWorker->enabled,
'data' => $this->manifest,
'installable' => $this->isInstallable(),
'output' => $this->serializer->serialize($this->manifest, 'json', $jsonOptions),
'files' => $manifestFiles,
'files' => $this->dataToFiles($manifestFiles),
];

$faviconsFiles = $this->faviconsCompiler->getFiles();
$faviconsFiles = is_array($faviconsFiles) ? $faviconsFiles : iterator_to_array($faviconsFiles);
$this->data['favicons'] = [
'enabled' => $this->favicons->enabled,
'data' => $this->favicons,
'files' => array_map(
static fn (\SpomkyLabs\PwaBundle\Service\Data $data): array => [
'url' => $data->url,
'headers' => $data->headers,
'html' => $data->html,
],
$faviconsFiles
),
'files' => $this->dataToFiles($faviconsFiles),
];
}

Expand Down Expand Up @@ -157,6 +152,22 @@ public function getName(): string
return 'pwa';
}

/**
* @param \SpomkyLabs\PwaBundle\Service\Data[] $data
* @return array{url: string, html: string|null, headers: array<string, string|bool>}[]
*/
private function dataToFiles(array $data): array
{
return array_map(
static fn (\SpomkyLabs\PwaBundle\Service\Data $data): array => [
'url' => $data->url,
'headers' => $data->headers,
'html' => $data->html,
],
$data
);
}

/**
* @return array{status: bool, reasons: array<string, bool>}
*/
Expand Down
45 changes: 12 additions & 33 deletions src/Service/FaviconsCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@

final class FaviconsCompiler implements FileCompilerInterface, CanLogInterface
{
/**
* @var null|array<string, Data>
*/
private null|array $files = null;

private LoggerInterface $logger;

public function __construct(
Expand All @@ -37,25 +32,21 @@ public function __construct(
}

/**
* @return array<string, Data>
* @return iterable<Data>
*/
public function getFiles(): array
public function getFiles(): iterable
{
if ($this->files !== null) {
return $this->files;
}
$this->logger->debug('Compiling favicons.', [
'favicons' => $this->favicons,
]);
if ($this->imageProcessor === null || $this->favicons->enabled === false) {
$this->logger->debug('Favicons are disabled or no image processor is available.');
$this->files = [];
yield from [];

return $this->files;
return;
}
[$asset, $hash] = $this->getFavicon();
assert($asset !== null, 'The asset does not exist.');
$this->files = [];
$sizes = [
//Always
[
Expand Down Expand Up @@ -243,27 +234,16 @@ public function getFiles(): array
);
$completeHash = hash('xxh128', $hash . $configuration);
$filename = sprintf($size['url'], $size['width'], $size['height'], $completeHash);
$this->files[$filename] = $this->processIcon(
$asset,
$filename,
$configuration,
$size['mimetype'],
$size['rel'],
);
yield $this->processIcon($asset, $filename, $configuration, $size['mimetype'], $size['rel']);
}
if ($this->favicons->tileColor !== null) {
$this->logger->debug('Creating browserconfig.xml.');
$this->files = [...$this->files, ...$this->processBrowserConfig($asset, $hash)];
yield from $this->processBrowserConfig($asset, $hash);
}
if ($this->favicons->safariPinnedTabColor !== null && $this->favicons->useSilhouette === true) {
$safariPinnedTab = $this->generateSafariPinnedTab($asset);
$this->files[$safariPinnedTab->url] = $safariPinnedTab;
yield $this->generateSafariPinnedTab($asset, $hash);
}
$this->logger->debug('Favicons created.', [
'files' => $this->files,
]);

return $this->files;
$this->logger->debug('Favicons created.');
}

public function setLogger(LoggerInterface $logger): void
Expand Down Expand Up @@ -330,7 +310,7 @@ private function processIcon(
*/
private function processBrowserConfig(string $asset, string $hash): array
{
if ($this->favicons->useSilhouette === true) {
if ($this->favicons->useSilhouette === true && $this->debug === false) {
$asset = $this->generateSilhouette($asset);
}
$this->logger->debug('Processing browserconfig.xml.');
Expand Down Expand Up @@ -456,15 +436,14 @@ private function getFavicon(): array
return [$data, $hash];
}

private function generateSafariPinnedTab(string $content): Data
private function generateSafariPinnedTab(string $content, string $hash): Data
{
$silhouette = $this->generateSilhouette($content);
$hash = hash('xxh128', $silhouette);
$callback = fn (): string => $this->generateSilhouette($content);
$url = sprintf('/safari-pinned-tab-%s.svg', $hash);

return Data::create(
$url,
$silhouette,
$callback,
[
'Cache-Control' => 'public, max-age=604800, immutable',
'Content-Type' => 'image/svg+xml',
Expand Down
2 changes: 1 addition & 1 deletion src/Service/FileCompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface FileCompilerInterface
{
/**
* @return iterable<string, Data>
* @return iterable<Data>
*/
public function getFiles(): iterable;
}
56 changes: 24 additions & 32 deletions src/Service/ManifestCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ final class ManifestCompiler implements FileCompilerInterface, CanLogInterface

private LoggerInterface $logger;

/**
* @var array<string, Data>
*/
private null|array $files = null;

/**
* @param array<string> $locales
*/
Expand Down Expand Up @@ -71,38 +66,30 @@ public function __construct(
}

/**
* @return array<string, Data>
* @return iterable<Data>
*/
public function getFiles(): array
public function getFiles(): iterable
{
if ($this->files !== null) {
return $this->files;
}
$this->logger->debug('Compiling manifest files.', [
'manifest' => $this->manifest,
]);
if ($this->manifest->enabled === false) {
$this->logger->debug('Manifest is disabled. No file to compile.');
$this->files = [];
yield from [];

return $this->files;
return;
}
$this->files = [];
if ($this->locales === []) {
$this->logger->debug('No locale defined. Compiling default manifest.');
$this->files[$this->manifestPublicUrl] = $this->compileManifest(null);
yield $this->compileManifest(null);
}
foreach ($this->locales as $locale) {
$this->logger->debug('Compiling manifest for locale.', [
'locale' => $locale,
]);
$this->files[str_replace('{locale}', $locale, $this->manifestPublicUrl)] = $this->compileManifest($locale);
yield $this->compileManifest($locale);
}
$this->logger->debug('Manifest files compiled.', [
'files' => $this->files,
]);

return $this->files;
$this->logger->debug('Manifest files compiled.');
}

public function setLogger(LoggerInterface $logger): void
Expand All @@ -116,30 +103,35 @@ private function compileManifest(null|string $locale): Data
'locale' => $locale,
]);
$manifest = clone $this->manifest;
$preEvent = new PreManifestCompileEvent($manifest);
$preEvent = $this->dispatcher->dispatch($preEvent);
assert($preEvent instanceof PreManifestCompileEvent);

$options = $this->jsonOptions;
$manifestPublicUrl = $this->manifestPublicUrl;
if ($locale !== null) {
$options[TranslatableNormalizer::NORMALIZATION_LOCALE_KEY] = $locale;
$manifestPublicUrl = str_replace('{locale}', $locale, $this->manifestPublicUrl);
}
$data = $this->serializer->serialize($preEvent->manifest, 'json', $options);

$postEvent = new PostManifestCompileEvent($manifest, $data);
$postEvent = $this->dispatcher->dispatch($postEvent);
assert($postEvent instanceof PostManifestCompileEvent);
$callback = function () use ($manifest, $locale): string {
$preEvent = new PreManifestCompileEvent($manifest);
$preEvent = $this->dispatcher->dispatch($preEvent);
assert($preEvent instanceof PreManifestCompileEvent);

$options = $this->jsonOptions;
if ($locale !== null) {
$options[TranslatableNormalizer::NORMALIZATION_LOCALE_KEY] = $locale;
}
$data = $this->serializer->serialize($preEvent->manifest, 'json', $options);
$postEvent = new PostManifestCompileEvent($manifest, $data);
$postEvent = $this->dispatcher->dispatch($postEvent);
assert($postEvent instanceof PostManifestCompileEvent);

return $postEvent->data;
};

return Data::create(
$manifestPublicUrl,
$postEvent->data,
$callback,
[
'Cache-Control' => 'public, max-age=604800, immutable',
'Content-Type' => 'application/manifest+json',
'X-Manifest-Dev' => true,
'Etag' => hash('xxh128', $data),
]
);
}
Expand Down
56 changes: 31 additions & 25 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function __construct(
}

/**
* @return iterable<string, Data>
* @return iterable<Data>
*/
public function getFiles(): iterable
{
yield $this->serviceWorkerPublicUrl => $this->compileSW();
yield $this->compileSW();
yield from $this->getWorkboxFiles();
}

Expand All @@ -69,30 +69,33 @@ public function setLogger(LoggerInterface $logger): void
private function compileSW(): Data
{
$this->logger->debug('Service Worker compilation started.');
$body = '';

foreach ($this->serviceworkerRules as $rule) {
$this->logger->debug('Processing service worker rule.', [
'rule' => $rule::class,
]);
$ruleBody = $rule->process($this->debug);
if ($this->debug === false) {
$ruleBody = trim($ruleBody);
$callback = function (): string {
$body = '';

foreach ($this->serviceworkerRules as $rule) {
$this->logger->debug('Processing service worker rule.', [
'rule' => $rule::class,
]);
$ruleBody = $rule->process($this->debug);
if ($this->debug === false) {
$ruleBody = trim($ruleBody);
}
$body .= $ruleBody;
}
$body .= $ruleBody;
}
$body .= $this->includeRootSW();
$this->logger->debug('Service Worker compilation completed.', [
'body' => $body,
]);
$body .= $this->includeRootSW();
$this->logger->debug('Service Worker compilation completed.', [
'body' => $body,
]);

return $body;
};

return Data::create(
$this->serviceWorkerPublicUrl,
$body,
$callback,
[
'Content-Type' => 'application/javascript',
'X-SW-Dev' => true,
'Etag' => hash('xxh128', $body),
]
);
}
Expand All @@ -115,7 +118,7 @@ private function includeRootSW(): string
}

/**
* @return iterable<string, Data>
* @return iterable<Data>
*/
private function getWorkboxFiles(): iterable
{
Expand Down Expand Up @@ -147,7 +150,7 @@ private function getWorkboxFiles(): iterable
if ($data === null) {
continue;
}
yield $publicUrl => $data;
yield $data;
}
}

Expand All @@ -171,16 +174,19 @@ private function getWorkboxFile(string $publicUrl): null|Data
return null;
}

$body = file_get_contents($resourcePath);
assert(is_string($body), 'Unable to load the file content.');
$callback = function () use ($resourcePath): string {
$body = file_get_contents($resourcePath);
assert(is_string($body), 'Unable to load the file content.');

return $body;
};

return Data::create(
$publicUrl,
$body,
$callback,
[
'Content-Type' => 'application/javascript',
'X-SW-Dev' => true,
'Etag' => hash('xxh128', $body),
]
);
}
Expand Down

0 comments on commit 071a923

Please sign in to comment.