Skip to content

Commit

Permalink
JSON Pretty only when debug is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 6, 2024
1 parent 4d83fa2 commit 6556b2a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/Command/CreateServiceWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Symfony\Component\Yaml\Yaml;
use function count;

/**
* @deprecated This command will be removed in the next major version. Create an empty file in assets/sw.js instead.
*/
#[AsCommand(name: 'pwa:create:sw', description: 'Generate a basic Service Worker')]
final class CreateServiceWorkerCommand extends Command
{
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use SpomkyLabs\PwaBundle\Service\ManifestBuilder;
use SpomkyLabs\PwaBundle\Service\ServiceWorkerBuilder;
use SpomkyLabs\PwaBundle\Service\ServiceWorkerCompiler;
use SpomkyLabs\PwaBundle\Subscriber\AssetsCompileEventListener;
use SpomkyLabs\PwaBundle\Subscriber\ManifestCompileEventListener;
use SpomkyLabs\PwaBundle\Subscriber\PwaDevServerSubscriber;
use SpomkyLabs\PwaBundle\Subscriber\ServiceWorkerCompileEventListener;
use SpomkyLabs\PwaBundle\Subscriber\WorkboxCompileEventListener;
Expand Down Expand Up @@ -87,7 +87,7 @@

/*** Event Listeners and Subscribers ***/
$container->set(WorkboxCompileEventListener::class);
$container->set(AssetsCompileEventListener::class);
$container->set(ManifestCompileEventListener::class);
$container->set(ServiceWorkerCompileEventListener::class);
$container->set(ServiceWorkerCompiler::class);

Expand Down
3 changes: 3 additions & 0 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use function assert;
use function count;
Expand Down Expand Up @@ -45,6 +46,8 @@ public function __construct(
$this->assetPublicPrefix = rtrim($publicAssetsPathResolver->resolvePublicPath(''), '/');
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
$options = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
];
if ($debug === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
use const JSON_UNESCAPED_UNICODE;

#[AsEventListener(PreAssetsCompileEvent::class)]
final readonly class AssetsCompileEventListener
final readonly class ManifestCompileEventListener
{
private string $manifestPublicUrl;
private array $jsonOptions;

Check failure on line 24 in src/Subscriber/ManifestCompileEventListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Property SpomkyLabs\PwaBundle\Subscriber\ManifestCompileEventListener::$jsonOptions type has no value type specified in iterable type array.

public function __construct(
private SerializerInterface $serializer,
Expand All @@ -31,20 +32,27 @@ public function __construct(
string $manifestPublicUrl,
#[Autowire('@asset_mapper.local_public_assets_filesystem')]
private PublicAssetsFilesystemInterface $assetsFilesystem,
#[Autowire('%kernel.debug%')]
bool $debug,
) {
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
$options = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
];
if ($debug === true) {
$options[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT;
}
$this->jsonOptions = $options;
}

public function __invoke(PreAssetsCompileEvent $event): void
{
if (! $this->manifestEnabled) {
return;
}
$data = $this->serializer->serialize($this->manifest, 'json', [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
]);
$data = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions);
$this->assetsFilesystem->write($this->manifestPublicUrl, $data);
}
}
18 changes: 13 additions & 5 deletions src/Subscriber/PwaDevServerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
private null|string $workboxPublicUrl;

private null|string $workboxVersion;
private array $jsonOptions;

Check failure on line 40 in src/Subscriber/PwaDevServerSubscriber.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Property SpomkyLabs\PwaBundle\Subscriber\PwaDevServerSubscriber::$jsonOptions type has no value type specified in iterable type array.

public function __construct(
private ServiceWorkerCompiler $serviceWorkerBuilder,
Expand All @@ -50,6 +51,8 @@ public function __construct(
#[Autowire('%spomky_labs_pwa.manifest.public_url%')]
string $manifestPublicUrl,
private null|Profiler $profiler,
#[Autowire('%kernel.debug%')]
bool $debug,
) {
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
$serviceWorkerPublicUrl = $serviceWorker->dest;
Expand All @@ -62,6 +65,15 @@ public function __construct(
$this->workboxVersion = null;
$this->workboxPublicUrl = null;
}
$options = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
];
if ($debug === true) {
$options[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT;
}
$this->jsonOptions = $options;
}

public function onKernelRequest(RequestEvent $event): void
Expand Down Expand Up @@ -113,11 +125,7 @@ public static function getSubscribedEvents(): array
private function serveManifest(RequestEvent $event): void
{
$this->profiler?->disable();
$body = $this->serializer->serialize($this->manifest, 'json', [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
]);
$body = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions);
$response = new Response($body, Response::HTTP_OK, [
'Cache-Control' => 'public, max-age=604800, immutable',
'Content-Type' => 'application/manifest+json',
Expand Down

0 comments on commit 6556b2a

Please sign in to comment.