diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0b9b57d..ee71307 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -120,6 +120,11 @@ parameters: count: 1 path: src/Dto/FileHandler.php + - + message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\GoogleFontCache has an uninitialized property \\$enabled\\. Give it default value or assign it in the constructor\\.$#" + count: 1 + path: src/Dto/GoogleFontCache.php + - message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Icon has an uninitialized property \\$sizeList\\. Give it default value or assign it in the constructor\\.$#" count: 1 @@ -300,6 +305,11 @@ parameters: count: 1 path: src/Dto/Workbox.php + - + message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$googleFontCache\\. Give it default value or assign it in the constructor\\.$#" + count: 1 + path: src/Dto/Workbox.php + - message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$offlineFallbackPlaceholder\\. Give it default value or assign it in the constructor\\.$#" count: 1 diff --git a/src/Dto/GoogleFontCache.php b/src/Dto/GoogleFontCache.php new file mode 100644 index 0000000..760ea60 --- /dev/null +++ b/src/Dto/GoogleFontCache.php @@ -0,0 +1,21 @@ +defaultFalse() ->info('Whether to use the local workbox or the CDN.') ->end() + ->arrayNode('google_fonts') + ->canBeDisabled() + ->children() + ->scalarNode('cache_prefix') + ->defaultNull() + ->info('The cache prefix for the Google fonts.') + ->end() + ->integerNode('max_age') + ->defaultNull() + ->info('The maximum age of the Google fonts cache (in seconds).') + ->end() + ->integerNode('max_entries') + ->defaultNull() + ->info('The maximum number of entries in the Google fonts cache.') + ->end() + ->end() + ->end() ->booleanNode('cache_manifest') ->defaultTrue() ->info('Whether to cache the manifest file.') ->end() + ->booleanNode('cache_google_fonts') + ->defaultTrue() + ->info('Whether to cache the Google fonts.') + ->end() ->scalarNode('version') ->defaultValue('7.0.0') ->info('The version of workbox. When using local files, the version shall be "7.0.0."') diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 3dd0933..0f86339 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -95,6 +95,7 @@ private function processWorkbox(Workbox $workbox, string $body): string $body = $this->processPageImageCacheRule($workbox, $body); $body = $this->processImageCacheRule($workbox, $body); $body = $this->processCacheRootFilesRule($workbox, $body); + $body = $this->processCacheGoogleFontsRule($workbox, $body); return $this->processOfflineFallback($workbox, $body); } @@ -285,6 +286,27 @@ private function processCacheRootFilesRule(Workbox $workbox, string $body): stri return $body . PHP_EOL . PHP_EOL . trim($declaration); } + private function processCacheGoogleFontsRule(Workbox $workbox, string $body): string + { + if ($workbox->googleFontCache->enabled === false) { + return $body; + } + $options = [ + 'cachePrefix' => $workbox->googleFontCache->cachePrefix, + 'maxAge' => $workbox->googleFontCache->maxAge, + 'maxEntries' => $workbox->googleFontCache->maxEntries, + ]; + $options = array_filter($options, static fn (mixed $v): bool => ($v !== null && $v !== '')); + $options = count($options) === 0 ? '' : $this->serializer->serialize($options, 'json', $this->jsonOptions); + + $declaration = <<pageFallback === null && $workbox->imageFallback === null && $workbox->fontFallback === null) {