Skip to content

Commit d75eb6a

Browse files
committed
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.
1 parent e39641a commit d75eb6a

6 files changed

+78
-5
lines changed

src/Event/NullEventDispatcher.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SpomkyLabs\PwaBundle\Event;
6+
7+
use Psr\EventDispatcher\EventDispatcherInterface;
8+
9+
final readonly class NullEventDispatcher implements EventDispatcherInterface
10+
{
11+
public function dispatch(object $event): void
12+
{
13+
// Do nothing
14+
}
15+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SpomkyLabs\PwaBundle\Event;
6+
7+
use SpomkyLabs\PwaBundle\Dto\Manifest;
8+
9+
final class PostManifestCompileEvent
10+
{
11+
public function __construct(
12+
public Manifest $manifest,
13+
public string $data
14+
) {
15+
}
16+
}

src/Event/PreManifestCompileEvent.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SpomkyLabs\PwaBundle\Event;
6+
7+
use SpomkyLabs\PwaBundle\Dto\Manifest;
8+
9+
class PreManifestCompileEvent
10+
{
11+
public function __construct(
12+
public Manifest $manifest,
13+
) {
14+
}
15+
}

src/Service/ServiceWorkerCompiler.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SpomkyLabs\PwaBundle\Service;
66

7+
use Psr\EventDispatcher\EventDispatcherInterface;
78
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
89
use SpomkyLabs\PwaBundle\ServiceWorkerRule\ServiceWorkerRuleInterface;
910
use Symfony\Component\AssetMapper\AssetMapperInterface;
@@ -26,6 +27,7 @@ public function __construct(
2627
private iterable $serviceworkerRules,
2728
#[Autowire('%kernel.debug%')]
2829
public bool $debug,
30+
null|EventDispatcherInterface $dispatcher = null
2931
) {
3032
}
3133

src/Subscriber/ManifestCompileEventListener.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
namespace SpomkyLabs\PwaBundle\Subscriber;
66

7+
use Psr\EventDispatcher\EventDispatcherInterface;
78
use SpomkyLabs\PwaBundle\Dto\Manifest;
9+
use SpomkyLabs\PwaBundle\Event\NullEventDispatcher;
10+
use SpomkyLabs\PwaBundle\Event\PostManifestCompileEvent;
11+
use SpomkyLabs\PwaBundle\Event\PreManifestCompileEvent;
812
use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent;
913
use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface;
1014
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -21,6 +25,8 @@
2125
#[AsEventListener(PreAssetsCompileEvent::class)]
2226
final readonly class ManifestCompileEventListener
2327
{
28+
private EventDispatcherInterface $dispatcher;
29+
2430
private string $manifestPublicUrl;
2531

2632
private array $jsonOptions;
@@ -36,7 +42,9 @@ public function __construct(
3642
private PublicAssetsFilesystemInterface $assetsFilesystem,
3743
#[Autowire('%kernel.debug%')]
3844
bool $debug,
45+
null|EventDispatcherInterface $dispatcher = null,
3946
) {
47+
$this->dispatcher = $dispatcher ?? new NullEventDispatcher();
4048
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
4149
$options = [
4250
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
@@ -55,7 +63,11 @@ public function __invoke(PreAssetsCompileEvent $event): void
5563
if (! $this->manifestEnabled) {
5664
return;
5765
}
58-
$data = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions);
59-
$this->assetsFilesystem->write($this->manifestPublicUrl, $data);
66+
$manifest = clone $this->manifest;
67+
$this->dispatcher->dispatch(new PreManifestCompileEvent($manifest));
68+
$data = $this->serializer->serialize($manifest, 'json', $this->jsonOptions);
69+
$postEvent = new PostManifestCompileEvent($manifest, $data);
70+
$this->dispatcher->dispatch($postEvent);
71+
$this->assetsFilesystem->write($this->manifestPublicUrl, $postEvent->data);
6072
}
6173
}

src/Subscriber/PwaDevServerSubscriber.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace SpomkyLabs\PwaBundle\Subscriber;
66

7+
use Psr\EventDispatcher\EventDispatcherInterface;
78
use SpomkyLabs\PwaBundle\Dto\Manifest;
89
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
10+
use SpomkyLabs\PwaBundle\Event\NullEventDispatcher;
11+
use SpomkyLabs\PwaBundle\Event\PostManifestCompileEvent;
12+
use SpomkyLabs\PwaBundle\Event\PreManifestCompileEvent;
913
use SpomkyLabs\PwaBundle\Service\ServiceWorkerCompiler;
1014
use Symfony\Component\Config\FileLocator;
1115
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -31,6 +35,8 @@
3135

3236
final readonly class PwaDevServerSubscriber implements EventSubscriberInterface
3337
{
38+
private EventDispatcherInterface $dispatcher;
39+
3440
private string $manifestPublicUrl;
3541

3642
private null|string $serviceWorkerPublicUrl;
@@ -55,7 +61,9 @@ public function __construct(
5561
private null|Profiler $profiler,
5662
#[Autowire('%kernel.debug%')]
5763
bool $debug,
64+
null|EventDispatcherInterface $dispatcher = null,
5865
) {
66+
$this->dispatcher = $dispatcher ?? new NullEventDispatcher();
5967
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
6068
$serviceWorkerPublicUrl = $serviceWorker->dest;
6169
$this->serviceWorkerPublicUrl = '/' . trim($serviceWorkerPublicUrl, '/');
@@ -128,12 +136,17 @@ public static function getSubscribedEvents(): array
128136
private function serveManifest(RequestEvent $event): void
129137
{
130138
$this->profiler?->disable();
131-
$body = $this->serializer->serialize($this->manifest, 'json', $this->jsonOptions);
132-
$response = new Response($body, Response::HTTP_OK, [
139+
$manifest = clone $this->manifest;
140+
$this->dispatcher->dispatch(new PreManifestCompileEvent($manifest));
141+
$data = $this->serializer->serialize($manifest, 'json', $this->jsonOptions);
142+
$postEvent = new PostManifestCompileEvent($manifest, $data);
143+
$this->dispatcher->dispatch($postEvent);
144+
145+
$response = new Response($data, Response::HTTP_OK, [
133146
'Cache-Control' => 'public, max-age=604800, immutable',
134147
'Content-Type' => 'application/manifest+json',
135148
'X-Manifest-Dev' => true,
136-
'Etag' => hash('xxh128', $body),
149+
'Etag' => hash('xxh128', $data),
137150
]);
138151

139152
$event->setResponse($response);

0 commit comments

Comments
 (0)