Skip to content

Commit

Permalink
Fix hardcoded asset public prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 5, 2024
1 parent f53c6cd commit b843738
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Command/CreateIconsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion src/Command/CreateServiceWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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');
}
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
28 changes: 27 additions & 1 deletion src/DependencyInjection/SpomkyLabsPwaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'];
Expand All @@ -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'])) {

Check failure on line 81 in src/DependencyInjection/SpomkyLabsPwaExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Cannot access offset 'public_prefix' on mixed.
continue;
}
$container->prependExtensionConfig('pwa', [
'asset_public_prefix' => $c['asset_mapper']['public_prefix'],
]);
}
}
}
}
}
6 changes: 4 additions & 2 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: [
Expand All @@ -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 => {
Expand Down

0 comments on commit b843738

Please sign in to comment.