diff --git a/src/DataCollector/PwaCollector.php b/src/DataCollector/PwaCollector.php index 6406d8a..7e3b233 100644 --- a/src/DataCollector/PwaCollector.php +++ b/src/DataCollector/PwaCollector.php @@ -9,7 +9,6 @@ use SpomkyLabs\PwaBundle\Dto\Manifest; use SpomkyLabs\PwaBundle\Dto\ServiceWorker; use SpomkyLabs\PwaBundle\Dto\Workbox; -use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -37,8 +36,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 +54,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), @@ -101,7 +98,7 @@ public function getName(): string private function isInstallable(): array { $reasons = [ - 'The manifest must be enabled' => ! $this->manifestEnabled, + 'The manifest must be enabled' => ! $this->manifest->enabled, 'The manifest must have a short name or a name' => $this->manifest->shortName === null && $this->manifest->name === null, 'The manifest must have a start URL' => $this->manifest->startUrl === null, 'The manifest must have a display value set to "standalone", "fullscreen" or "minimal-ui' => ! in_array( 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 65b89c9..4d746de 100644 --- a/src/Subscriber/ManifestCompileEventListener.php +++ b/src/Subscriber/ManifestCompileEventListener.php @@ -34,8 +34,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')] @@ -60,7 +58,7 @@ public function __construct( public function __invoke(PreAssetsCompileEvent $event): void { - if (! $this->manifestEnabled) { + if (! $this->manifest->enabled) { return; } $manifest = clone $this->manifest; diff --git a/src/Subscriber/PwaDevServerSubscriber.php b/src/Subscriber/PwaDevServerSubscriber.php index 0083454..93d635c 100644 --- a/src/Subscriber/PwaDevServerSubscriber.php +++ b/src/Subscriber/PwaDevServerSubscriber.php @@ -51,11 +51,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, @@ -97,13 +93,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..291dc03 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,11 +27,8 @@ public function __construct( public function __invoke(PreAssetsCompileEvent $event): void { - if (! $this->serviceWorkerEnabled) { - return; - } $serviceWorker = $this->manifest->serviceWorker; - if ($serviceWorker === null || $serviceWorker->workbox->enabled !== true || $serviceWorker->workbox->useCDN === true) { + if ($serviceWorker === null || $serviceWorker->enabled !== true || $serviceWorker->workbox->enabled !== true || $serviceWorker->workbox->useCDN === true) { return; } $workboxVersion = $serviceWorker->workbox->version; diff --git a/src/Twig/PwaRuntime.php b/src/Twig/PwaRuntime.php index e3ca916..77256ae 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);