Skip to content

Commit

Permalink
Update service worker and favicon sources handling (#196)
Browse files Browse the repository at this point in the history
Updated the way favicon and service worker sources are handled including more assert conditions for validation. Also, made adjustments regarding PHPStan task and restructured import statements in 'castor.php'. Now service worker 'src' is not required anymore reducing unnecessary configuration burdens.
  • Loading branch information
Spomky committed May 11, 2024
1 parent 27059f7 commit d2b3a71
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
37 changes: 31 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,6 @@ parameters:
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$src\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/ServiceWorker.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ServiceWorker has an uninitialized property \\$workbox\\. Give it default value or assign it in the constructor\\.$#"
count: 1
Expand Down Expand Up @@ -580,11 +575,41 @@ parameters:
count: 1
path: src/Resources/config/definition/web_client.php

-
message: "#^Call to function assert\\(\\) with true and 'The asset does not…' will always evaluate to true\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Cannot call method process\\(\\) on SpomkyLabs\\\\PwaBundle\\\\ImageProcessor\\\\ImageProcessorInterface\\|null\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:getFavicon\\(\\) should return string but returns string\\|false\\.$#"
count: 2
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\#1 \\$asset of method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:processBrowserConfig\\(\\) expects SpomkyLabs\\\\PwaBundle\\\\Service\\\\MappedAsset, string given\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\#1 \\$content of method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:processIcon\\(\\) expects string, SpomkyLabs\\\\PwaBundle\\\\Service\\\\MappedAsset given\\.$#"
count: 5
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\$asset of method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:processBrowserConfig\\(\\) has invalid type SpomkyLabs\\\\PwaBundle\\\\Service\\\\MappedAsset\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Strict comparison using \\!\\=\\= between string and null will always evaluate to true\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
Expand All @@ -598,4 +623,4 @@ parameters:
-
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\SpomkyLabsPwaBundle\\:\\:loadExtension\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
path: src/SpomkyLabsPwaBundle.php
path: src/SpomkyLabsPwaBundle.php
2 changes: 1 addition & 1 deletion src/Dto/ServiceWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class ServiceWorker
{
public bool $enabled;

public Asset $src;
public null|Asset $src = null;

public string $dest;

Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/definition/service_worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
->end()
->children()
->scalarNode('src')
->isRequired()
->info('The path to the service worker source file. Can be served by Asset Mapper.')
->example('script/sw.js')
->end()
Expand Down
19 changes: 14 additions & 5 deletions src/Service/FaviconsCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use SpomkyLabs\PwaBundle\ImageProcessor\Configuration;
use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessorInterface;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\MappedAsset;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use function assert;
use const PHP_EOL;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function getFiles(): array
if ($this->imageProcessor === null || $this->favicons->enabled === false) {
return [];
}
$asset = $this->assetMapper->getAsset($this->favicons->src->src);
$asset = $this->getFavicon();
assert($asset !== null, 'The asset does not exist.');
$this->files = [];
$sizes = [
Expand Down Expand Up @@ -244,14 +243,12 @@ public function getFiles(): array
}

private function processIcon(
MappedAsset $asset,
string $content,
string $publicUrl,
Configuration $configuration,
string $mimeType,
null|string $rel,
): Data {
$content = file_get_contents($asset->sourcePath);
assert($content !== false);
if ($this->debug === true) {
$data = $this->imageProcessor->process($content, null, null, null, $configuration);
$url = str_replace('{hash}', '', $publicUrl);
Expand Down Expand Up @@ -385,4 +382,16 @@ private function processBrowserConfig(MappedAsset $asset): array
$browserConfig,
];
}

private function getFavicon(): string
{
$source = $this->favicons->src;
if (! str_starts_with($source->src, '/')) {
$asset = $this->assetMapper->getAsset($source->src);
assert($asset !== null, 'Unable to find the favicon source asset');
return $asset->content ?? file_get_contents($asset->sourcePath);
}
assert(file_exists($source->src), 'Unable to find the favicon source file');
return file_get_contents($source->src);
}
}
10 changes: 6 additions & 4 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,17 @@ private function compileSW(): Data

private function includeRootSW(): string
{
if ($this->serviceWorker->src->src === '') {
$source = $this->serviceWorker->src;
if ($source === null) {
return '';
}
if (! str_starts_with($this->serviceWorker->src->src, '/')) {
$asset = $this->assetMapper->getAsset($this->serviceWorker->src->src);
if (! str_starts_with($source->src, '/')) {
$asset = $this->assetMapper->getAsset($source->src);
assert($asset !== null, 'Unable to find service worker source asset');
$body = $asset->content ?? file_get_contents($asset->sourcePath);
} else {
$body = file_get_contents($this->serviceWorker->src->src);
assert(file_exists($source->src), 'Unable to find service worker source file');
$body = file_get_contents($source->src);
}
return is_string($body) ? $body : '';
}
Expand Down
17 changes: 7 additions & 10 deletions tests/Functional/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public static function dataConfigurationIsValid(): iterable
],
],
]];
yield 'Service Worker without source' => [
[
'pwa' => [
'serviceworker' => null,
],
],
];
yield 'Complete configuration' => [[
'pwa' => [
'image_processor' => DummyImageProcessor::class,
Expand Down Expand Up @@ -306,16 +313,6 @@ public static function dataConfigurationIsInvalid(): iterable
],
'The child config "src" under "pwa.favicons" must be configured: The source of the favicon. Shall be a SVG or large PNG.',
];
yield 'No service worker source' => [
[
'pwa' => [
'serviceworker' => [
'enabled' => true,
],
],
],
'The child config "src" under "pwa.serviceworker" must be configured: The path to the service worker source file. Can be served by Asset Mapper.',
];
}

protected function getConfiguration(): ConfigurationInterface
Expand Down
2 changes: 1 addition & 1 deletion tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
],
'serviceworker' => [
'enabled' => true,
'src' => __DIR__ . '/sw.js',
//'src' => __DIR__ . '/sw.js',
'scope' => '/',
'use_cache' => true,
'workbox' => [
Expand Down
7 changes: 0 additions & 7 deletions tests/sw.js

This file was deleted.

0 comments on commit d2b3a71

Please sign in to comment.