From 6556b2a1ab7fb7e8eb6715fa287e82461f67845d Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Wed, 6 Mar 2024 12:49:59 +0100 Subject: [PATCH] JSON Pretty only when debug is enabled --- src/Command/CreateServiceWorkerCommand.php | 4 ++++ src/Resources/config/services.php | 4 ++-- src/Service/ServiceWorkerCompiler.php | 3 +++ ...r.php => ManifestCompileEventListener.php} | 20 +++++++++++++------ src/Subscriber/PwaDevServerSubscriber.php | 18 ++++++++++++----- 5 files changed, 36 insertions(+), 13 deletions(-) rename src/Subscriber/{AssetsCompileEventListener.php => ManifestCompileEventListener.php} (79%) diff --git a/src/Command/CreateServiceWorkerCommand.php b/src/Command/CreateServiceWorkerCommand.php index 0d0e233..45b5a64 100644 --- a/src/Command/CreateServiceWorkerCommand.php +++ b/src/Command/CreateServiceWorkerCommand.php @@ -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 { diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 033f018..37e6e27 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -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; @@ -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); diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index c8476e9..071b64b 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -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; @@ -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) { diff --git a/src/Subscriber/AssetsCompileEventListener.php b/src/Subscriber/ManifestCompileEventListener.php similarity index 79% rename from src/Subscriber/AssetsCompileEventListener.php rename to src/Subscriber/ManifestCompileEventListener.php index 555a911..97ed9fc 100644 --- a/src/Subscriber/AssetsCompileEventListener.php +++ b/src/Subscriber/ManifestCompileEventListener.php @@ -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; public function __construct( private SerializerInterface $serializer, @@ -31,8 +32,19 @@ 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 @@ -40,11 +52,7 @@ 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); } } diff --git a/src/Subscriber/PwaDevServerSubscriber.php b/src/Subscriber/PwaDevServerSubscriber.php index 814e55b..b748f26 100644 --- a/src/Subscriber/PwaDevServerSubscriber.php +++ b/src/Subscriber/PwaDevServerSubscriber.php @@ -37,6 +37,7 @@ private null|string $workboxPublicUrl; private null|string $workboxVersion; + private array $jsonOptions; public function __construct( private ServiceWorkerCompiler $serviceWorkerBuilder, @@ -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; @@ -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 @@ -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',