Skip to content

Commit

Permalink
Refactor code to check if service worker is enabled
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Spomky committed Apr 18, 2024
1 parent d9e2bc6 commit 30cdb60
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/DataCollector/PwaCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/Dto/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ final class Manifest
{
use TranslatableTrait;

public bool $enabled = false;

#[SerializedName('use_credentials')]
public bool $useCredentials = true;

Expand Down
4 changes: 1 addition & 3 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* @param iterable<ServiceWorkerRuleInterface> $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')]
Expand All @@ -31,7 +29,7 @@ public function __construct(

public function compile(): ?string
{
if ($this->serviceWorkerEnabled === false) {
if ($this->serviceWorker->enabled === false) {
return null;
}
$body = '';
Expand Down
4 changes: 1 addition & 3 deletions src/Subscriber/ManifestCompileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand All @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions src/Subscriber/PwaDevServerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
):
Expand Down
6 changes: 3 additions & 3 deletions src/Subscriber/ServiceWorkerCompileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')]
Expand All @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions src/Subscriber/WorkboxCompileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,7 +27,7 @@ public function __construct(

public function __invoke(PreAssetsCompileEvent $event): void
{
if (! $this->serviceWorkerEnabled) {
if (! $this->manifest->serviceWorker->enabled) {

Check failure on line 30 in src/Subscriber/WorkboxCompileEventListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Cannot access property $enabled on SpomkyLabs\PwaBundle\Dto\ServiceWorker|null.
return;
}
$serviceWorker = $this->manifest->serviceWorker;
Expand Down
8 changes: 2 additions & 6 deletions src/Twig/PwaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%')]
Expand All @@ -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) {

Check failure on line 44 in src/Twig/PwaRuntime.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Cannot access property $enabled on SpomkyLabs\PwaBundle\Dto\ServiceWorker|null.
$output = $this->injectServiceWorker($output, $injectSW, $swAttributes);
}
$output = $this->injectIcons($output, $injectIcons);
Expand Down

0 comments on commit 30cdb60

Please sign in to comment.