Skip to content

Commit

Permalink
Better icon attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jan 15, 2024
1 parent 0184663 commit 36ec3cb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 1 addition & 3 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +15,6 @@
{
public function __construct(
private AssetMapperInterface $assetMapper,
private null|ImageProcessor $imageProcessor,
) {
}

Expand Down Expand Up @@ -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;
}

Expand Down
49 changes: 39 additions & 10 deletions src/Twig/PwaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,14 +31,18 @@ public function load(bool $themeColor = true, bool $icons = true): string
$output = sprintf('<link rel="manifest" href="%s">', $url);
if ($this->manifest->icons !== [] && $icons === true) {
foreach ($this->manifest->icons as $icon) {
$iconUrl = $this->getIconPublicUrl($icon->src);
$output .= sprintf(
'%s<link rel="%s" sizes="%s" href="%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<link %s>', PHP_EOL, $attributes);
}
}
if ($this->manifest->themeColor !== null && $themeColor === true) {
Expand All @@ -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);
}
}

0 comments on commit 36ec3cb

Please sign in to comment.