From 4d83fa202c341a62e4f44b1ca7d78259ddd52d7f Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Wed, 6 Mar 2024 10:34:48 +0100 Subject: [PATCH] Simplified public_prefix --- .../config/definition/asset_public_prefix.php | 16 ------------ src/Service/ServiceWorkerCompiler.php | 7 +++-- src/SpomkyLabsPwaBundle.php | 26 ------------------- 3 files changed, 5 insertions(+), 44 deletions(-) delete mode 100644 src/Resources/config/definition/asset_public_prefix.php diff --git a/src/Resources/config/definition/asset_public_prefix.php b/src/Resources/config/definition/asset_public_prefix.php deleted file mode 100644 index 1ab6926..0000000 --- a/src/Resources/config/definition/asset_public_prefix.php +++ /dev/null @@ -1,16 +0,0 @@ -rootNode() - ->children() - ->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() - ->end(); -}; diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 978a81f..c8476e9 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -8,6 +8,7 @@ use SpomkyLabs\PwaBundle\Dto\ServiceWorker; use SpomkyLabs\PwaBundle\Dto\Workbox; use Symfony\Component\AssetMapper\AssetMapperInterface; +use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Serializer\Encoder\JsonEncode; use Symfony\Component\Serializer\SerializerInterface; @@ -25,6 +26,7 @@ private array $jsonOptions; private string $manifestPublicUrl; + private string $assetPublicPrefix; public function __construct( private SerializerInterface $serializer, @@ -32,14 +34,15 @@ public function __construct( string $manifestPublicUrl, #[Autowire('%spomky_labs_pwa.sw.enabled%')] private bool $serviceWorkerEnabled, - #[Autowire('%spomky_labs_pwa.asset_public_prefix%')] - private string $assetPublicPrefix, + #[Autowire(service: 'asset_mapper.public_assets_path_resolver')] + PublicAssetsPathResolverInterface $publicAssetsPathResolver, private Manifest $manifest, private ServiceWorker $serviceWorker, private AssetMapperInterface $assetMapper, #[Autowire('%kernel.debug%')] bool $debug, ) { + $this->assetPublicPrefix = rtrim($publicAssetsPathResolver->resolvePublicPath(''), '/'); $this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/'); $options = [ JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, diff --git a/src/SpomkyLabsPwaBundle.php b/src/SpomkyLabsPwaBundle.php index ee9040f..cf83028 100644 --- a/src/SpomkyLabsPwaBundle.php +++ b/src/SpomkyLabsPwaBundle.php @@ -36,10 +36,6 @@ public function loadExtension(array $config, ContainerConfigurator $container, C if ($serviceWorkerConfig['enabled'] === true && $manifestConfig['enabled'] === true) { $manifestConfig['serviceworker'] = $serviceWorkerConfig; } - $builder->setParameter( - 'spomky_labs_pwa.asset_public_prefix', - '/' . trim((string) $config['asset_public_prefix'], '/') - ); /*** Manifest ***/ $builder->setParameter('spomky_labs_pwa.manifest.enabled', $config['manifest']['enabled']); @@ -58,7 +54,6 @@ public function loadExtension(array $config, ContainerConfigurator $container, C public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void { - $this->setAssetPublicPrefix($builder); $this->setAssetMapperPath($builder); } @@ -72,25 +67,4 @@ private function setAssetMapperPath(ContainerBuilder $builder): void ], ]); } - - private function setAssetPublicPrefix(ContainerBuilder $builder): void - { - $bundles = $builder->getParameter('kernel.bundles'); - if (isset($bundles['FrameworkBundle'])) { - foreach ($builder->getExtensions() as $name => $extension) { - if ($name !== 'framework') { - continue; - } - $config = $builder->getExtensionConfig($name); - foreach ($config as $c) { - if (! isset($c['asset_mapper']['public_prefix'])) { - continue; - } - $builder->prependExtensionConfig('pwa', [ - 'asset_public_prefix' => $c['asset_mapper']['public_prefix'], - ]); - } - } - } - } }