Skip to content

Commit

Permalink
Refactor code to check if service is enabled directly from service in…
Browse files Browse the repository at this point in the history
…stance

Removed the autowired enabled properties from Manifest and ServiceWorker services. We now directly use the enabled property from the Manifest and ServiceWorker instances. This simplifies the code and allows for better encapsulation and code readability.
  • Loading branch information
Spomky committed Apr 22, 2024
1 parent 2091496 commit 2700833
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 34 deletions.
7 changes: 2 additions & 5 deletions src/DataCollector/PwaCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
) {
}

Expand All @@ -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),
Expand Down Expand Up @@ -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(
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 @@ -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')]
Expand All @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions src/Subscriber/PwaDevServerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
):
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
7 changes: 1 addition & 6 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,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;
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) {
$output = $this->injectServiceWorker($output, $injectSW, $swAttributes);
}
$output = $this->injectIcons($output, $injectIcons);
Expand Down

0 comments on commit 2700833

Please sign in to comment.