From d75eb6a85d726f83c4cc69a1bc7a0357f31ffba0 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Mon, 22 Apr 2024 20:35:15 +0200 Subject: [PATCH 1/3] Add event dispatching to manifest compilation This update introduces event dispatching in the manifest compilation process. PreManifestCompileEvent and PostManifestCompileEvent have been added, which are dispatched before and after the compilation respectively. The modification enhances the flexibility and extensibility of the manifest compilation process by allowing custom actions just before or after compilation. --- src/Event/NullEventDispatcher.php | 15 +++++++++++++++ src/Event/PostManifestCompileEvent.php | 16 ++++++++++++++++ src/Event/PreManifestCompileEvent.php | 15 +++++++++++++++ src/Service/ServiceWorkerCompiler.php | 2 ++ .../ManifestCompileEventListener.php | 16 ++++++++++++++-- src/Subscriber/PwaDevServerSubscriber.php | 19 ++++++++++++++++--- 6 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/Event/NullEventDispatcher.php create mode 100644 src/Event/PostManifestCompileEvent.php create mode 100644 src/Event/PreManifestCompileEvent.php diff --git a/src/Event/NullEventDispatcher.php b/src/Event/NullEventDispatcher.php new file mode 100644 index 0000000..c33d850 --- /dev/null +++ b/src/Event/NullEventDispatcher.php @@ -0,0 +1,15 @@ +dispatcher = $dispatcher ?? new NullEventDispatcher(); $this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/'); $options = [ AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true, @@ -55,7 +63,11 @@ public function __invoke(PreAssetsCompileEvent $event): void if (! $this->manifestEnabled) { return; } - $data = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions); - $this->assetsFilesystem->write($this->manifestPublicUrl, $data); + $manifest = clone $this->manifest; + $this->dispatcher->dispatch(new PreManifestCompileEvent($manifest)); + $data = $this->serializer->serialize($manifest, 'json', $this->jsonOptions); + $postEvent = new PostManifestCompileEvent($manifest, $data); + $this->dispatcher->dispatch($postEvent); + $this->assetsFilesystem->write($this->manifestPublicUrl, $postEvent->data); } } diff --git a/src/Subscriber/PwaDevServerSubscriber.php b/src/Subscriber/PwaDevServerSubscriber.php index 74e7294..0083454 100644 --- a/src/Subscriber/PwaDevServerSubscriber.php +++ b/src/Subscriber/PwaDevServerSubscriber.php @@ -4,8 +4,12 @@ namespace SpomkyLabs\PwaBundle\Subscriber; +use Psr\EventDispatcher\EventDispatcherInterface; use SpomkyLabs\PwaBundle\Dto\Manifest; use SpomkyLabs\PwaBundle\Dto\ServiceWorker; +use SpomkyLabs\PwaBundle\Event\NullEventDispatcher; +use SpomkyLabs\PwaBundle\Event\PostManifestCompileEvent; +use SpomkyLabs\PwaBundle\Event\PreManifestCompileEvent; use SpomkyLabs\PwaBundle\Service\ServiceWorkerCompiler; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -31,6 +35,8 @@ final readonly class PwaDevServerSubscriber implements EventSubscriberInterface { + private EventDispatcherInterface $dispatcher; + private string $manifestPublicUrl; private null|string $serviceWorkerPublicUrl; @@ -55,7 +61,9 @@ public function __construct( private null|Profiler $profiler, #[Autowire('%kernel.debug%')] bool $debug, + null|EventDispatcherInterface $dispatcher = null, ) { + $this->dispatcher = $dispatcher ?? new NullEventDispatcher(); $this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/'); $serviceWorkerPublicUrl = $serviceWorker->dest; $this->serviceWorkerPublicUrl = '/' . trim($serviceWorkerPublicUrl, '/'); @@ -128,12 +136,17 @@ public static function getSubscribedEvents(): array private function serveManifest(RequestEvent $event): void { $this->profiler?->disable(); - $body = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions); - $response = new Response($body, Response::HTTP_OK, [ + $manifest = clone $this->manifest; + $this->dispatcher->dispatch(new PreManifestCompileEvent($manifest)); + $data = $this->serializer->serialize($manifest, 'json', $this->jsonOptions); + $postEvent = new PostManifestCompileEvent($manifest, $data); + $this->dispatcher->dispatch($postEvent); + + $response = new Response($data, Response::HTTP_OK, [ 'Cache-Control' => 'public, max-age=604800, immutable', 'Content-Type' => 'application/manifest+json', 'X-Manifest-Dev' => true, - 'Etag' => hash('xxh128', $body), + 'Etag' => hash('xxh128', $data), ]); $event->setResponse($response); From 3c557aeae1f1b683e741d3601f6b8f1eb564eb55 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Mon, 22 Apr 2024 20:36:22 +0200 Subject: [PATCH 2/3] Add event dispatching to manifest compilation This update introduces event dispatching in the manifest compilation process. PreManifestCompileEvent and PostManifestCompileEvent have been added, which are dispatched before and after the compilation respectively. The modification enhances the flexibility and extensibility of the manifest compilation process by allowing custom actions just before or after compilation. --- src/Service/ServiceWorkerCompiler.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 38fa7db..aa62f89 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -4,7 +4,6 @@ namespace SpomkyLabs\PwaBundle\Service; -use Psr\EventDispatcher\EventDispatcherInterface; use SpomkyLabs\PwaBundle\Dto\ServiceWorker; use SpomkyLabs\PwaBundle\ServiceWorkerRule\ServiceWorkerRuleInterface; use Symfony\Component\AssetMapper\AssetMapperInterface; @@ -27,7 +26,6 @@ public function __construct( private iterable $serviceworkerRules, #[Autowire('%kernel.debug%')] public bool $debug, - null|EventDispatcherInterface $dispatcher = null ) { } From 70eefaac6b684cc6b0b025924a24b85558dbbf6b Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Mon, 22 Apr 2024 21:23:47 +0200 Subject: [PATCH 3/3] Add event dispatching to manifest compilation This update introduces event dispatching in the manifest compilation process. PreManifestCompileEvent and PostManifestCompileEvent have been added, which are dispatched before and after the compilation respectively. The modification enhances the flexibility and extensibility of the manifest compilation process by allowing custom actions just before or after compilation. --- src/Event/NullEventDispatcher.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Event/NullEventDispatcher.php b/src/Event/NullEventDispatcher.php index c33d850..23768a4 100644 --- a/src/Event/NullEventDispatcher.php +++ b/src/Event/NullEventDispatcher.php @@ -8,8 +8,8 @@ final readonly class NullEventDispatcher implements EventDispatcherInterface { - public function dispatch(object $event): void + public function dispatch(object $event): object { - // Do nothing + return $event; } }