Skip to content

Commit

Permalink
Manifest file is now compiled with other assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jan 15, 2024
1 parent 36ec3cb commit 382172e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SpomkyLabs\PwaBundle\ImageProcessor\GDImageProcessor;
use SpomkyLabs\PwaBundle\ImageProcessor\ImagickImageProcessor;
use SpomkyLabs\PwaBundle\Service\Builder;
use SpomkyLabs\PwaBundle\Subscriber\AssetsCompileEventListener;
use SpomkyLabs\PwaBundle\Subscriber\PwaDevServerSubscriber;
use SpomkyLabs\PwaBundle\Twig\PwaExtension;
use SpomkyLabs\PwaBundle\Twig\PwaRuntime;
Expand Down Expand Up @@ -50,6 +51,8 @@
;
}

$container->set(AssetsCompileEventListener::class);

$container->set(PwaDevServerSubscriber::class)
->args([
'$profiler' => service('profiler')
Expand Down
44 changes: 44 additions & 0 deletions src/Subscriber/AssetsCompileEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Subscriber;

use SpomkyLabs\PwaBundle\Dto\Manifest;
use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent;
use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

#[AsEventListener(PreAssetsCompileEvent::class)]
final readonly class AssetsCompileEventListener
{
private string $manifestPublicUrl;

public function __construct(
private SerializerInterface $serializer,
private Manifest $manifest,
#[Autowire('%spomky_labs_pwa.manifest_public_url%')]
string $manifestPublicUrl,
#[Autowire('@asset_mapper.local_public_assets_filesystem')]
private PublicAssetsFilesystemInterface $assetsFilesystem,
) {
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
}

public function __invoke(PreAssetsCompileEvent $event): void
{
$data = $this->serializer->serialize($this->manifest, 'json', [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
'json_encode_options' => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
]);
$this->assetsFilesystem->write($this->manifestPublicUrl, $data);
}
}

0 comments on commit 382172e

Please sign in to comment.