Skip to content

Commit

Permalink
Fix incorrect name format (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky authored Feb 9, 2024
1 parent a0d7e97 commit c2355df
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 4 additions & 8 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand All @@ -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;
}
Expand Down
22 changes: 9 additions & 13 deletions src/Twig/PwaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<link %s>', PHP_EOL, $attributes);
Expand All @@ -134,33 +134,29 @@ 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;
}

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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
[
Expand Down Expand Up @@ -211,7 +211,7 @@
[
'src' => 'pwa/1920x1920.svg',
'sizes' => [16, 48],
'format' => 'webp',
'type' => 'webp',
],
],
'auth' => false,
Expand Down

0 comments on commit c2355df

Please sign in to comment.