-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
204 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\Service; | ||
|
||
use SpomkyLabs\PwaBundle\Dto\Manifest; | ||
use Symfony\Component\AssetMapper\AssetMapperInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
use function assert; | ||
use function is_string; | ||
use const JSON_PRETTY_PRINT; | ||
use const JSON_THROW_ON_ERROR; | ||
use const JSON_UNESCAPED_SLASHES; | ||
use const JSON_UNESCAPED_UNICODE; | ||
|
||
final readonly class ServiceWorkerBuilder | ||
{ | ||
private ?string $serviceworkerPublicUrl; | ||
|
||
public function __construct( | ||
private SerializerInterface $serializer, | ||
private Manifest $manifest, | ||
private AssetMapperInterface $assetMapper, | ||
#[Autowire('%spomky_labs_pwa.serviceworker.precaching_placeholder%')] | ||
private string $precachingPlaceholder, | ||
) { | ||
$serviceWorkerPublicUrl = $manifest->serviceWorker?->dest; | ||
$this->serviceworkerPublicUrl = $serviceWorkerPublicUrl === null ? null : '/' . trim( | ||
$serviceWorkerPublicUrl, | ||
'/' | ||
); | ||
} | ||
|
||
public function build(): ?string | ||
{ | ||
if ($this->serviceworkerPublicUrl === null) { | ||
return null; | ||
} | ||
$serviceWorkerSource = $this->manifest->serviceWorker?->src; | ||
if ($serviceWorkerSource === null) { | ||
return null; | ||
} | ||
|
||
if (! str_starts_with($serviceWorkerSource, '/')) { | ||
$asset = $this->assetMapper->getAsset($serviceWorkerSource); | ||
assert($asset !== null, 'Unable to find service worker source asset'); | ||
$body = $asset->content ?? file_get_contents($asset->sourcePath); | ||
} else { | ||
$body = file_get_contents($serviceWorkerSource); | ||
} | ||
assert(is_string($body), 'Unable to find service worker source content'); | ||
return $this->processPrecachedAssets($body); | ||
} | ||
|
||
private function processPrecachedAssets(string $body): string | ||
{ | ||
if (! str_contains($body, $this->precachingPlaceholder)) { | ||
return $body; | ||
} | ||
$result = []; | ||
foreach ($this->assetMapper->allAssets() as $asset) { | ||
$result[] = [ | ||
'url' => $asset->publicPath, | ||
'revision' => $asset->digest, | ||
]; | ||
} | ||
return str_replace($this->precachingPlaceholder, $this->serializer->serialize($result, 'json', [ | ||
'json_encode_options' => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, | ||
]), $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\Subscriber; | ||
|
||
use SpomkyLabs\PwaBundle\Service\ServiceWorkerBuilder; | ||
use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent; | ||
use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
|
||
#[AsEventListener(PreAssetsCompileEvent::class)] | ||
final readonly class ServiceWorkerCompileEventListener | ||
{ | ||
private ?string $serviceworkerPublicUrl; | ||
|
||
public function __construct( | ||
private ServiceWorkerBuilder $serviceWorkerBuilder, | ||
#[Autowire('%spomky_labs_pwa.sw_public_url%')] | ||
?string $serviceWorkerPublicUrl, | ||
#[Autowire('@asset_mapper.local_public_assets_filesystem')] | ||
private PublicAssetsFilesystemInterface $assetsFilesystem, | ||
) { | ||
$this->serviceworkerPublicUrl = $serviceWorkerPublicUrl === null ? null : '/' . trim( | ||
$serviceWorkerPublicUrl, | ||
'/' | ||
); | ||
} | ||
|
||
public function __invoke(PreAssetsCompileEvent $event): void | ||
{ | ||
$data = $this->serviceWorkerBuilder->build(); | ||
if ($data === null || $this->serviceworkerPublicUrl === null) { | ||
return; | ||
} | ||
$this->assetsFilesystem->write($this->serviceworkerPublicUrl, $data); | ||
} | ||
} |