|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\View\Components; |
| 6 | + |
| 7 | +use DateInterval; |
| 8 | +use DateTimeImmutable; |
| 9 | +use Exception; |
| 10 | +use Tempest\Cache\IconCache; |
| 11 | +use Tempest\Core\AppConfig; |
| 12 | +use Tempest\Http\Status; |
| 13 | +use Tempest\HttpClient\HttpClient; |
| 14 | +use Tempest\Support\Str\ImmutableString; |
| 15 | +use Tempest\View\Elements\ViewComponentElement; |
| 16 | +use Tempest\View\IconConfig; |
| 17 | +use Tempest\View\ViewComponent; |
| 18 | + |
| 19 | +final readonly class Icon implements ViewComponent |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private AppConfig $appConfig, |
| 23 | + private IconCache $iconCache, |
| 24 | + private IconConfig $iconConfig, |
| 25 | + private HttpClient $http, |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + public static function getName(): string |
| 30 | + { |
| 31 | + return 'x-icon'; |
| 32 | + } |
| 33 | + |
| 34 | + public function compile(ViewComponentElement $element): string |
| 35 | + { |
| 36 | + $name = $element->getAttribute('name'); |
| 37 | + $class = $element->getAttribute('class'); |
| 38 | + |
| 39 | + $svg = $this->render($name); |
| 40 | + |
| 41 | + if (! $svg) { |
| 42 | + return $this->appConfig->environment->isLocal() |
| 43 | + ? ('<!-- unknown-icon: ' . $name . ' -->') |
| 44 | + : ''; |
| 45 | + } |
| 46 | + |
| 47 | + return match ($class) { |
| 48 | + null => $svg, |
| 49 | + default => $this->injectClass($svg, $class), |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Downloads the icon's SVG file from the Iconify API |
| 55 | + */ |
| 56 | + private function download(string $prefix, string $name): ?string |
| 57 | + { |
| 58 | + try { |
| 59 | + $url = new ImmutableString($this->iconConfig->iconifyApiUrl) |
| 60 | + ->finish('/') |
| 61 | + ->append("{$prefix}/{$name}.svg") |
| 62 | + ->toString(); |
| 63 | + |
| 64 | + $response = $this->http->get($url); |
| 65 | + |
| 66 | + if ($response->status !== Status::OK) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + return $response->body; |
| 71 | + } catch (Exception) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Renders an icon |
| 78 | + * |
| 79 | + * This method is responsible for rendering the icon. If the icon is not |
| 80 | + * in the cache, it will download it on the fly and cache it for future |
| 81 | + * use. If the icon is already in the cache, it will be served from there. |
| 82 | + */ |
| 83 | + private function render(string $name): ?string |
| 84 | + { |
| 85 | + try { |
| 86 | + $parts = explode(':', $name, 2); |
| 87 | + |
| 88 | + if (count($parts) !== 2) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + [$prefix, $name] = $parts; |
| 93 | + |
| 94 | + return $this->iconCache->resolve( |
| 95 | + key: "iconify-{$prefix}-{$name}", |
| 96 | + cache: fn () => $this->download($prefix, $name), |
| 97 | + expiresAt: $this->iconConfig->cacheDuration |
| 98 | + ? new DateTimeImmutable() |
| 99 | + ->add(DateInterval::createFromDateString("{$this->iconConfig->cacheDuration} seconds")) |
| 100 | + : null, |
| 101 | + ); |
| 102 | + } catch (Exception) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Forwards the user-provided class attribute to the SVG element |
| 109 | + */ |
| 110 | + private function injectClass(string $svg, string $class): string |
| 111 | + { |
| 112 | + return new ImmutableString($svg) |
| 113 | + ->replace( |
| 114 | + search: '<svg ', |
| 115 | + replace: "<svg class=\"{$class}\" ", |
| 116 | + ) |
| 117 | + ->toString(); |
| 118 | + } |
| 119 | +} |
0 commit comments