Skip to content

Commit

Permalink
Add event dispatching to manifest compilation (#179)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Spomky authored Apr 22, 2024
1 parent e39641a commit 2091496
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/Event/NullEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Event;

use Psr\EventDispatcher\EventDispatcherInterface;

final readonly class NullEventDispatcher implements EventDispatcherInterface
{
public function dispatch(object $event): object
{
return $event;
}
}
16 changes: 16 additions & 0 deletions src/Event/PostManifestCompileEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Event;

use SpomkyLabs\PwaBundle\Dto\Manifest;

final class PostManifestCompileEvent
{
public function __construct(
public Manifest $manifest,
public string $data
) {
}
}
15 changes: 15 additions & 0 deletions src/Event/PreManifestCompileEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Event;

use SpomkyLabs\PwaBundle\Dto\Manifest;

class PreManifestCompileEvent
{
public function __construct(
public Manifest $manifest,
) {
}
}
16 changes: 14 additions & 2 deletions src/Subscriber/ManifestCompileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace SpomkyLabs\PwaBundle\Subscriber;

use Psr\EventDispatcher\EventDispatcherInterface;
use SpomkyLabs\PwaBundle\Dto\Manifest;
use SpomkyLabs\PwaBundle\Event\NullEventDispatcher;
use SpomkyLabs\PwaBundle\Event\PostManifestCompileEvent;
use SpomkyLabs\PwaBundle\Event\PreManifestCompileEvent;
use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent;
use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand All @@ -21,6 +25,8 @@
#[AsEventListener(PreAssetsCompileEvent::class)]
final readonly class ManifestCompileEventListener
{
private EventDispatcherInterface $dispatcher;

private string $manifestPublicUrl;

private array $jsonOptions;
Expand All @@ -36,7 +42,9 @@ public function __construct(
private PublicAssetsFilesystemInterface $assetsFilesystem,
#[Autowire('%kernel.debug%')]
bool $debug,
null|EventDispatcherInterface $dispatcher = null,
) {
$this->dispatcher = $dispatcher ?? new NullEventDispatcher();
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
$options = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
Expand All @@ -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);
}
}
19 changes: 16 additions & 3 deletions src/Subscriber/PwaDevServerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +35,8 @@

final readonly class PwaDevServerSubscriber implements EventSubscriberInterface
{
private EventDispatcherInterface $dispatcher;

private string $manifestPublicUrl;

private null|string $serviceWorkerPublicUrl;
Expand All @@ -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, '/');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2091496

Please sign in to comment.