Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event dispatching to manifest compilation #179

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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