From 36ec3cb749ff724c64d5a302fce48b2ffe3ab341 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Mon, 15 Jan 2024 21:40:51 +0100 Subject: [PATCH] Better icon attributes --- src/DependencyInjection/Configuration.php | 8 ++-- src/Normalizer/IconNormalizer.php | 4 +- src/Twig/PwaRuntime.php | 49 ++++++++++++++++++----- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index bfcdc28..2dfb2d4 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -437,8 +437,8 @@ private function getIconsNode(string $info): ArrayNodeDefinition ->beforeNormalization() ->ifString() ->then(static fn (string $v): array => [ - 'src' => $v, - ]) + 'src' => $v, + ]) ->end() ->children() ->scalarNode('src') @@ -511,8 +511,8 @@ private function getScreenshotsNode(string $info): ArrayNodeDefinition ->beforeNormalization() ->ifString() ->then(static fn (string $v): array => [ - 'src' => $v, - ]) + 'src' => $v, + ]) ->end() ->children() ->scalarNode('src') diff --git a/src/Normalizer/IconNormalizer.php b/src/Normalizer/IconNormalizer.php index 599e2d2..5397bfc 100644 --- a/src/Normalizer/IconNormalizer.php +++ b/src/Normalizer/IconNormalizer.php @@ -5,7 +5,6 @@ namespace SpomkyLabs\PwaBundle\Normalizer; use SpomkyLabs\PwaBundle\Dto\Icon; -use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor; use Symfony\Component\AssetMapper\AssetMapperInterface; use Symfony\Component\AssetMapper\MappedAsset; use Symfony\Component\Mime\MimeTypes; @@ -16,7 +15,6 @@ { public function __construct( private AssetMapperInterface $assetMapper, - private null|ImageProcessor $imageProcessor, ) { } @@ -70,7 +68,7 @@ private function getFormat(Icon $object, ?MappedAsset $asset): ?string return $object->format; } - if ($this->imageProcessor === null || $asset === null || ! class_exists(MimeTypes::class)) { + if ($asset === null || ! class_exists(MimeTypes::class)) { return null; } diff --git a/src/Twig/PwaRuntime.php b/src/Twig/PwaRuntime.php index 11b7d99..5752243 100644 --- a/src/Twig/PwaRuntime.php +++ b/src/Twig/PwaRuntime.php @@ -4,9 +4,12 @@ namespace SpomkyLabs\PwaBundle\Twig; +use SpomkyLabs\PwaBundle\Dto\Icon; use SpomkyLabs\PwaBundle\Dto\Manifest; use Symfony\Component\AssetMapper\AssetMapperInterface; +use Symfony\Component\AssetMapper\MappedAsset; use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Symfony\Component\Mime\MimeTypes; use const PHP_EOL; final readonly class PwaRuntime @@ -28,14 +31,18 @@ public function load(bool $themeColor = true, bool $icons = true): string $output = sprintf('', $url); if ($this->manifest->icons !== [] && $icons === true) { foreach ($this->manifest->icons as $icon) { - $iconUrl = $this->getIconPublicUrl($icon->src); - $output .= sprintf( - '%s', - PHP_EOL, + ['url' => $url, 'format' => $format] = $this->getIconInfo($icon); + $attributes = sprintf( + 'rel="%s" sizes="%s" href="%s"', str_contains($icon->purpose ?? '', 'maskable') ? 'mask-icon' : 'icon', $icon->getSizeList(), - $iconUrl + $url ); + if ($format !== null) { + $attributes .= sprintf(' type="%s"', $format); + } + + $output .= sprintf('%s', PHP_EOL, $attributes); } } if ($this->manifest->themeColor !== null && $themeColor === true) { @@ -45,17 +52,39 @@ public function load(bool $themeColor = true, bool $icons = true): string return $output; } - private function getIconPublicUrl(string $source): ?string + /** + * @return array{url: string, format: string|null} + */ + private function getIconInfo(Icon $icon): array { $url = null; - if (! str_starts_with($source, '/')) { - $asset = $this->assetMapper->getAsset($source); + $format = $icon->format; + if (! str_starts_with($icon->src, '/')) { + $asset = $this->assetMapper->getAsset($icon->src); $url = $asset?->publicPath; + $format = $this->getFormat($icon, $asset); } if ($url === null) { - $url = $source; + $url = $icon; + } + + return [ + 'url' => $url, + 'format' => $format, + ]; + } + + private function getFormat(Icon $object, ?MappedAsset $asset): ?string + { + if ($object->format !== null) { + return $object->format; + } + + if ($asset === null || ! class_exists(MimeTypes::class)) { + return null; } - return $url; + $mime = MimeTypes::getDefault(); + return $mime->guessMimeType($asset->sourcePath); } }