diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ea290c3..5a26005 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -596,8 +596,8 @@ private function getIconsNode(string $info): ArrayNodeDefinition ->example([['16', '32']]) ->integerPrototype()->end() ->end() - ->scalarNode('format') - ->info('The icon format output.') + ->scalarNode('type') + ->info('The icon mime type.') ->example(['image/webp', 'image/png']) ->end() ->scalarNode('purpose') diff --git a/src/Dto/Icon.php b/src/Dto/Icon.php index 1d3a359..8b013b9 100644 --- a/src/Dto/Icon.php +++ b/src/Dto/Icon.php @@ -16,7 +16,7 @@ final class Icon #[SerializedName('sizes')] public array $sizeList; - public null|string $format = null; + public null|string $type = null; public null|string $purpose = null; diff --git a/src/Normalizer/IconNormalizer.php b/src/Normalizer/IconNormalizer.php index 2d42f28..0c18982 100644 --- a/src/Normalizer/IconNormalizer.php +++ b/src/Normalizer/IconNormalizer.php @@ -28,16 +28,16 @@ public function __construct( public function normalize(mixed $object, string $format = null, array $context = []): array { assert($object instanceof Icon); - $imageFormat = null; + $imageType = $object->type; if (! str_starts_with($object->src->src, '/')) { $asset = $this->assetMapper->getAsset($object->src->src); - $imageFormat = $this->getFormat($object, $asset); + $imageType = $this->getType($asset); } $result = [ 'src' => $this->normalizer->normalize($object->src, $format, $context), 'sizes' => $object->getSizeList(), - 'type' => $imageFormat, + 'type' => $imageType, 'purpose' => $object->purpose, ]; @@ -64,12 +64,8 @@ public function getSupportedTypes(?string $format): array ]; } - private function getFormat(Icon $object, ?MappedAsset $asset): ?string + private function getType(?MappedAsset $asset): ?string { - if ($object->format !== null) { - return $object->format; - } - if ($asset === null || ! class_exists(MimeTypes::class)) { return null; } diff --git a/src/Twig/PwaRuntime.php b/src/Twig/PwaRuntime.php index fe21cf8..2b659a2 100644 --- a/src/Twig/PwaRuntime.php +++ b/src/Twig/PwaRuntime.php @@ -116,15 +116,15 @@ private function injectIcons(string $output, bool $injectIcons): string return $output; } foreach ($this->manifest->icons as $icon) { - ['url' => $url, 'format' => $format] = $this->getIconInfo($icon); + ['url' => $url, 'type' => $type] = $this->getIconInfo($icon); $attributes = sprintf( 'rel="%s" sizes="%s" href="%s"', str_contains($icon->purpose ?? '', 'maskable') ? 'mask-icon' : 'icon', $icon->getSizeList(), $url ); - if ($format !== null) { - $attributes .= sprintf(' type="%s"', $format); + if ($type !== null) { + $attributes .= sprintf(' type="%s"', $type); } $output .= sprintf('%s', PHP_EOL, $attributes); @@ -134,16 +134,16 @@ private function injectIcons(string $output, bool $injectIcons): string } /** - * @return array{url: string, format: string|null} + * @return array{url: string, type: string|null} */ private function getIconInfo(Icon $icon): array { $url = null; - $format = $icon->format; - if (! str_starts_with($icon->src->src, '/')) { + $type = $icon->type; + if ($type === null && ! str_starts_with($icon->src->src, '/')) { $asset = $this->assetMapper->getAsset($icon->src->src); $url = $asset?->publicPath; - $format = $this->getFormat($icon, $asset); + $type = $this->getFormat($asset); } if ($url === null) { $url = $icon->src->src; @@ -151,16 +151,12 @@ private function getIconInfo(Icon $icon): array return [ 'url' => $url, - 'format' => $format, + 'type' => $type, ]; } - private function getFormat(Icon $object, ?MappedAsset $asset): ?string + private function getFormat(?MappedAsset $asset): ?string { - if ($object->format !== null) { - return $object->format; - } - if ($asset === null || ! class_exists(MimeTypes::class)) { return null; } diff --git a/tests/config.php b/tests/config.php index 3d55e1d..cbfdf5f 100644 --- a/tests/config.php +++ b/tests/config.php @@ -78,12 +78,12 @@ [ 'src' => 'pwa/1920x1920.svg', 'sizes' => [48, 72, 96, 128, 256], - 'format' => 'webp', + 'type' => 'webp', ], [ 'src' => 'pwa/1920x1920.svg', 'sizes' => [48, 72, 96, 128, 256], - 'format' => 'png', + 'type' => 'png', 'purpose' => 'maskable', ], [ @@ -211,7 +211,7 @@ [ 'src' => 'pwa/1920x1920.svg', 'sizes' => [16, 48], - 'format' => 'webp', + 'type' => 'webp', ], ], 'auth' => false,