diff --git a/src/Command/CreateIconsCommand.php b/src/Command/CreateIconsCommand.php index e494393..a29a152 100644 --- a/src/Command/CreateIconsCommand.php +++ b/src/Command/CreateIconsCommand.php @@ -28,6 +28,8 @@ public function __construct( private readonly Filesystem $filesystem, #[Autowire('%kernel.project_dir%')] private readonly string $projectDir, + #[Autowire('%spomky_labs_pwa.asset_public_prefix%')] + private readonly string $assetPublicPrefix, private readonly null|ImageProcessor $imageProcessor, ) { parent::__construct(); @@ -52,7 +54,7 @@ protected function configure(): void 'o', InputOption::VALUE_OPTIONAL, 'The output directory', - sprintf('%s/assets/icons/', $this->projectDir) + sprintf('%s%s/icons/', $this->projectDir, $this->assetPublicPrefix) ); $this->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'The output directory', 'icon'); $this->addOption( diff --git a/src/Command/CreateScreenshotCommand.php b/src/Command/CreateScreenshotCommand.php index 664eacd..fff967b 100644 --- a/src/Command/CreateScreenshotCommand.php +++ b/src/Command/CreateScreenshotCommand.php @@ -35,6 +35,8 @@ public function __construct( #[Autowire('%kernel.project_dir%')] private readonly string $projectDir, private readonly null|ImageProcessor $imageProcessor, + #[Autowire('%spomky_labs_pwa.asset_public_prefix%')] + private readonly string $assetPublicPrefix, #[Autowire('@pwa.web_client')] null|Client $webClient = null, ) { @@ -64,7 +66,7 @@ protected function configure(): void 'o', InputOption::VALUE_OPTIONAL, 'The output directory', - sprintf('%s/assets/icons/', $this->projectDir) + sprintf('%s%s/icons/', $this->projectDir, $this->assetPublicPrefix) ); $this->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'The output filename', 'screenshot'); $this->addOption('width', null, InputOption::VALUE_OPTIONAL, 'The width of the screenshot'); diff --git a/src/Command/CreateServiceWorkerCommand.php b/src/Command/CreateServiceWorkerCommand.php index 50bc38c..a5ac1d9 100644 --- a/src/Command/CreateServiceWorkerCommand.php +++ b/src/Command/CreateServiceWorkerCommand.php @@ -25,6 +25,8 @@ public function __construct( private readonly AssetMapperInterface $assetMapper, private readonly Filesystem $filesystem, private readonly FileLocator $fileLocator, + #[Autowire('%spomky_labs_pwa.asset_public_prefix%')] + private readonly string $assetPublicPrefix, #[Autowire('%kernel.project_dir%')] private readonly string $projectDir, ) { @@ -37,7 +39,7 @@ protected function configure(): void 'output', InputArgument::OPTIONAL, 'The output file', - sprintf('%s/assets/sw.js', $this->projectDir) + sprintf('%s%s/sw.js', $this->projectDir, $this->assetPublicPrefix) ); $this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the generation of the service worker'); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index be47bf5..2201ad6 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -67,6 +67,11 @@ private function setupServices(ArrayNodeDefinition $node): void ->info('The image processor to use to generate the icons of different sizes.') ->example(GDImageProcessor::class) ->end() + ->scalarNode('asset_public_prefix') + ->cannotBeOverwritten() + ->defaultNull() + ->info('The public prefix of the assets. Shall be the same as the one used in the asset mapper.') + ->end() ->scalarNode('web_client') ->defaultNull() ->info('The Panther Client for generating screenshots. If not set, the default client will be used.') diff --git a/src/DependencyInjection/SpomkyLabsPwaExtension.php b/src/DependencyInjection/SpomkyLabsPwaExtension.php index d298366..6bddbec 100644 --- a/src/DependencyInjection/SpomkyLabsPwaExtension.php +++ b/src/DependencyInjection/SpomkyLabsPwaExtension.php @@ -10,11 +10,12 @@ use Symfony\Component\Config\Definition\Processor; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use function in_array; -final class SpomkyLabsPwaExtension extends Extension +final class SpomkyLabsPwaExtension extends Extension implements PrependExtensionInterface { private const ALIAS = 'pwa'; @@ -36,6 +37,10 @@ public function load(array $configs, ContainerBuilder $container): void if ($config['web_client'] !== null) { $container->setAlias('pwa.web_client', $config['web_client']); } + $container->setParameter( + 'spomky_labs_pwa.asset_public_prefix', + '/' . trim((string) $config['asset_public_prefix'], '/') + ); $container->setParameter('spomky_labs_pwa.routes.reference_type', $config['path_type_reference']); $serviceWorkerConfig = $config['serviceworker']; $manifestConfig = $config['manifest']; @@ -62,4 +67,25 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co { return new Configuration(self::ALIAS); } + + public function prepend(ContainerBuilder $container): void + { + $bundles = $container->getParameter('kernel.bundles'); + if (isset($bundles['FrameworkBundle'])) { + foreach ($container->getExtensions() as $name => $extension) { + if ($name !== 'framework') { + continue; + } + $config = $container->getExtensionConfig($name); + foreach ($config as $c) { + if (! isset($c['asset_mapper']['public_prefix'])) { + continue; + } + $container->prependExtensionConfig('pwa', [ + 'asset_public_prefix' => $c['asset_mapper']['public_prefix'], + ]); + } + } + } + } } diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 2ed843a..075bac0 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -23,6 +23,8 @@ { public function __construct( private SerializerInterface $serializer, + #[Autowire('%spomky_labs_pwa.asset_public_prefix%')] + private readonly string $assetPublicPrefix, #[Autowire('%spomky_labs_pwa.sw.enabled%')] private bool $serviceWorkerEnabled, private Manifest $manifest, @@ -160,7 +162,7 @@ private function processStandardRules(Workbox $workbox, string $body): string //Images cache workbox.routing.registerRoute( - ({request, url}) => (request.destination === 'image' && !url.pathname.startsWith('/assets')), + ({request, url}) => (request.destination === 'image' && !url.pathname.startsWith('{$this->assetPublicPrefix}')), new workbox.strategies.CacheFirst({ cacheName: '{$workbox->imageCacheName}', plugins: [ @@ -187,7 +189,7 @@ private function processStandardRules(Workbox $workbox, string $body): string }); // - Strategy: only the Asset Mapper public route workbox.routing.registerRoute( - ({url}) => url.pathname.startsWith('/assets'), + ({url}) => url.pathname.startsWith('{$this->assetPublicPrefix}'), assetCacheStrategy ); self.addEventListener('install', event => {