From 30cdb60f7e0a13719893115f35c8b803c535d7fe Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Thu, 18 Apr 2024 18:49:43 +0200 Subject: [PATCH] Refactor code to check if service worker is enabled This commit removes all properties that check if a service worker is enabled in favor of checking this directly on the service worker object itself. It affects multiple files and results in a cleaner, more object-oriented approach to determining if the service worker is enabled. --- src/DataCollector/PwaCollector.php | 4 +--- src/Dto/Manifest.php | 2 ++ src/Service/ServiceWorkerCompiler.php | 4 +--- src/Subscriber/ManifestCompileEventListener.php | 4 +--- src/Subscriber/PwaDevServerSubscriber.php | 12 ++++-------- src/Subscriber/ServiceWorkerCompileEventListener.php | 6 +++--- src/Subscriber/WorkboxCompileEventListener.php | 4 +--- src/Twig/PwaRuntime.php | 8 ++------ 8 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/DataCollector/PwaCollector.php b/src/DataCollector/PwaCollector.php index 6406d8a..cb740aa 100644 --- a/src/DataCollector/PwaCollector.php +++ b/src/DataCollector/PwaCollector.php @@ -37,8 +37,6 @@ public function __construct( private readonly iterable $cachingServices, private readonly Manifest $manifest, private readonly ServiceWorker $serviceWorker, - #[Autowire(param: 'spomky_labs_pwa.manifest.enabled')] - private readonly bool $manifestEnabled, ) { } @@ -57,7 +55,7 @@ public function collect(Request $request, Response $response, Throwable $excepti } $this->data['serviceWorker'] = $this->serviceWorker; $this->data['manifest'] = [ - 'enabled' => $this->manifestEnabled, + 'enabled' => $this->serviceWorker->enabled, 'data' => $this->manifest, 'installable' => $this->isInstallable(), 'output' => $this->serializer->serialize($this->manifest, 'json', $jsonOptions), diff --git a/src/Dto/Manifest.php b/src/Dto/Manifest.php index 34abd7c..775c594 100644 --- a/src/Dto/Manifest.php +++ b/src/Dto/Manifest.php @@ -11,6 +11,8 @@ final class Manifest { use TranslatableTrait; + public bool $enabled = false; + #[SerializedName('use_credentials')] public bool $useCredentials = true; diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index aa62f89..cd8762f 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -18,8 +18,6 @@ * @param iterable $serviceworkerRules */ public function __construct( - #[Autowire('%spomky_labs_pwa.sw.enabled%')] - private bool $serviceWorkerEnabled, private ServiceWorker $serviceWorker, private AssetMapperInterface $assetMapper, #[TaggedIterator('spomky_labs_pwa.service_worker_rule', defaultPriorityMethod: 'getPriority')] @@ -31,7 +29,7 @@ public function __construct( public function compile(): ?string { - if ($this->serviceWorkerEnabled === false) { + if ($this->serviceWorker->enabled === false) { return null; } $body = ''; diff --git a/src/Subscriber/ManifestCompileEventListener.php b/src/Subscriber/ManifestCompileEventListener.php index dae83e6..b092aad 100644 --- a/src/Subscriber/ManifestCompileEventListener.php +++ b/src/Subscriber/ManifestCompileEventListener.php @@ -28,8 +28,6 @@ public function __construct( private SerializerInterface $serializer, private Manifest $manifest, - #[Autowire('%spomky_labs_pwa.manifest.enabled%')] - private bool $manifestEnabled, #[Autowire('%spomky_labs_pwa.manifest.public_url%')] string $manifestPublicUrl, #[Autowire('@asset_mapper.local_public_assets_filesystem')] @@ -52,7 +50,7 @@ public function __construct( public function __invoke(PreAssetsCompileEvent $event): void { - if (! $this->manifestEnabled) { + if (! $this->manifest->enabled) { return; } $data = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions); diff --git a/src/Subscriber/PwaDevServerSubscriber.php b/src/Subscriber/PwaDevServerSubscriber.php index 74e7294..bfe9b32 100644 --- a/src/Subscriber/PwaDevServerSubscriber.php +++ b/src/Subscriber/PwaDevServerSubscriber.php @@ -45,11 +45,7 @@ public function __construct( private ServiceWorkerCompiler $serviceWorkerBuilder, private SerializerInterface $serializer, private Manifest $manifest, - ServiceWorker $serviceWorker, - #[Autowire('%spomky_labs_pwa.manifest.enabled%')] - private bool $manifestEnabled, - #[Autowire('%spomky_labs_pwa.sw.enabled%')] - private bool $serviceWorkerEnabled, + private ServiceWorker $serviceWorker, #[Autowire('%spomky_labs_pwa.manifest.public_url%')] string $manifestPublicUrl, private null|Profiler $profiler, @@ -89,13 +85,13 @@ public function onKernelRequest(RequestEvent $event): void ->getPathInfo(); switch (true) { - case $this->manifestEnabled === true && $pathInfo === $this->manifestPublicUrl: + case $this->manifest->enabled === true && $pathInfo === $this->manifestPublicUrl: $this->serveManifest($event); break; - case $this->serviceWorkerEnabled === true && $pathInfo === $this->serviceWorkerPublicUrl: + case $this->serviceWorker->enabled === true && $pathInfo === $this->serviceWorkerPublicUrl: $this->serveServiceWorker($event); break; - case $this->serviceWorkerEnabled === true && $this->workboxVersion !== null && $this->workboxPublicUrl !== null && str_starts_with( + case $this->serviceWorker->enabled === true && $this->workboxVersion !== null && $this->workboxPublicUrl !== null && str_starts_with( $pathInfo, $this->workboxPublicUrl ): diff --git a/src/Subscriber/ServiceWorkerCompileEventListener.php b/src/Subscriber/ServiceWorkerCompileEventListener.php index 68721aa..8f678b9 100644 --- a/src/Subscriber/ServiceWorkerCompileEventListener.php +++ b/src/Subscriber/ServiceWorkerCompileEventListener.php @@ -4,6 +4,7 @@ namespace SpomkyLabs\PwaBundle\Subscriber; +use SpomkyLabs\PwaBundle\Dto\ServiceWorker; use SpomkyLabs\PwaBundle\Service\ServiceWorkerCompiler; use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent; use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface; @@ -16,9 +17,8 @@ private ?string $serviceWorkerPublicUrl; public function __construct( + private ServiceWorker $serviceWorker, private ServiceWorkerCompiler $serviceWorkerBuilder, - #[Autowire('%spomky_labs_pwa.sw.enabled%')] - private bool $serviceWorkerEnabled, #[Autowire('%spomky_labs_pwa.sw.public_url%')] ?string $serviceWorkerPublicUrl, #[Autowire('@asset_mapper.local_public_assets_filesystem')] @@ -32,7 +32,7 @@ public function __construct( public function __invoke(PreAssetsCompileEvent $event): void { - if (! $this->serviceWorkerEnabled) { + if (! $this->serviceWorker->enabled) { return; } $data = $this->serviceWorkerBuilder->compile(); diff --git a/src/Subscriber/WorkboxCompileEventListener.php b/src/Subscriber/WorkboxCompileEventListener.php index 2c56d76..d7e6071 100644 --- a/src/Subscriber/WorkboxCompileEventListener.php +++ b/src/Subscriber/WorkboxCompileEventListener.php @@ -19,8 +19,6 @@ final readonly class WorkboxCompileEventListener { public function __construct( - #[Autowire('%spomky_labs_pwa.sw.enabled%')] - private bool $serviceWorkerEnabled, #[Autowire('@asset_mapper.local_public_assets_filesystem')] private PublicAssetsFilesystemInterface $assetsFilesystem, private Manifest $manifest, @@ -29,7 +27,7 @@ public function __construct( public function __invoke(PreAssetsCompileEvent $event): void { - if (! $this->serviceWorkerEnabled) { + if (! $this->manifest->serviceWorker->enabled) { return; } $serviceWorker = $this->manifest->serviceWorker; diff --git a/src/Twig/PwaRuntime.php b/src/Twig/PwaRuntime.php index e3ca916..4e88023 100644 --- a/src/Twig/PwaRuntime.php +++ b/src/Twig/PwaRuntime.php @@ -20,10 +20,6 @@ private string $manifestPublicUrl; public function __construct( - #[Autowire('%spomky_labs_pwa.manifest.enabled%')] - private bool $manifestEnabled, - #[Autowire('%spomky_labs_pwa.sw.enabled%')] - private bool $serviceWorkerEnabled, private AssetMapperInterface $assetMapper, private Manifest $manifest, #[Autowire('%spomky_labs_pwa.manifest.public_url%')] @@ -42,10 +38,10 @@ public function load( array $swAttributes = [] ): string { $output = ''; - if ($this->manifestEnabled === true) { + if ($this->manifest->enabled === true) { $output = $this->injectManifestFile($output); } - if ($this->serviceWorkerEnabled === true) { + if ($this->manifest->serviceWorker->enabled === true) { $output = $this->injectServiceWorker($output, $injectSW, $swAttributes); } $output = $this->injectIcons($output, $injectIcons);