|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\View\Components\Icons; |
| 6 | + |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tracks the SVG icons used while rendering the page so that subsequent renders |
| 11 | + * can emit a reference to the earlier rendered icon. |
| 12 | + * |
| 13 | + * @method static bool isFirstRender(string $filename) |
| 14 | + */ |
| 15 | +final class SvgIconRegistry |
| 16 | +{ |
| 17 | + private const ICON_DIRECTORY = 'public_html/images/icons'; |
| 18 | + private const BLADE_DIRECTORY = 'app/generated'; |
| 19 | + private const VIEW_PARENT_PATH = 'svg-icon'; |
| 20 | + |
| 21 | + public static function getBladePath(): string |
| 22 | + { |
| 23 | + return storage_path(self::BLADE_DIRECTORY); |
| 24 | + } |
| 25 | + |
| 26 | + protected array $used = []; |
| 27 | + |
| 28 | + protected array $compiled = []; |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns true if the icon with this filename has not been rendered yet. |
| 32 | + * |
| 33 | + * @param string $filename The filename of the icon to check. |
| 34 | + * @return bool |
| 35 | + */ |
| 36 | + public function isFirstRender(string $filename): bool |
| 37 | + { |
| 38 | + $firstRender = !isset($this->used[$filename]); |
| 39 | + $this->used[$filename] = true; |
| 40 | + return $firstRender; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the name of the view that renders the icon with this filename. |
| 45 | + * |
| 46 | + * If the view has not been compiled yet, or is out-of-date, it will be |
| 47 | + * compiled and saved to the icon-cache directory. |
| 48 | + * |
| 49 | + * @param string $filename The filename of the icon to get the view name for. |
| 50 | + * @return string The name of the view that renders the icon. |
| 51 | + */ |
| 52 | + public function getViewName(string $filename): string |
| 53 | + { |
| 54 | + if (isset($this->compiled[$filename])) { |
| 55 | + return $this->compiled[$filename]; |
| 56 | + } |
| 57 | + |
| 58 | + $sourcePath = base_path(implode(DIRECTORY_SEPARATOR, [self::ICON_DIRECTORY, "{$filename}.svg"])); |
| 59 | + |
| 60 | + $viewId = Str::slug($filename); |
| 61 | + $compiledPath = storage_path(implode(DIRECTORY_SEPARATOR, [self::BLADE_DIRECTORY, self::VIEW_PARENT_PATH, "{$viewId}.blade.php"])); |
| 62 | + |
| 63 | + if (!file_exists($sourcePath)) { |
| 64 | + throw new \RuntimeException("SVG icon not found at {$sourcePath} for {$filename}"); |
| 65 | + } |
| 66 | + |
| 67 | + if (!file_exists($compiledPath) || (filemtime($sourcePath) > filemtime($compiledPath))) { |
| 68 | + $svg = file_get_contents($sourcePath); |
| 69 | + $compiled = $this->transformSvgToBlade($svg, $viewId); |
| 70 | + |
| 71 | + @mkdir(dirname($compiledPath), 0777, true); |
| 72 | + file_put_contents($compiledPath, $compiled); |
| 73 | + } |
| 74 | + |
| 75 | + $this->compiled[$filename] = implode('.', [self::VIEW_PARENT_PATH, $viewId]); |
| 76 | + |
| 77 | + return $this->compiled[$filename]; |
| 78 | + } |
| 79 | + |
| 80 | + protected function buildSvgAttributeString(array $attributes): string |
| 81 | + { |
| 82 | + $attributes = array_filter($attributes); |
| 83 | + if (empty($attributes)) { |
| 84 | + return ''; |
| 85 | + } |
| 86 | + |
| 87 | + return ' ' . implode(' ', array_map( |
| 88 | + fn($key, $value) => $key . '="' . htmlspecialchars($value, ENT_QUOTES) . '"', |
| 89 | + array_keys($attributes), |
| 90 | + $attributes, |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Transforms the SVG markup into a Blade view that renders the icon. |
| 96 | + * |
| 97 | + * @param string $svg The SVG markup to transform. |
| 98 | + * @param string $viewId The ID of the view that will render the icon. |
| 99 | + * @return string The Blade view that renders the icon. |
| 100 | + */ |
| 101 | + protected function transformSvgToBlade(string $svg, string $viewId): string |
| 102 | + { |
| 103 | + $dom = new \DOMDocument(); |
| 104 | + $dom->loadXML($svg); |
| 105 | + |
| 106 | + $svgElement = $dom->documentElement; |
| 107 | + |
| 108 | + // Attributes to use on uses of the SVG element. |
| 109 | + $width = $svgElement->getAttribute('width'); |
| 110 | + $height = $svgElement->getAttribute('height'); |
| 111 | + |
| 112 | + // Attributes to copy to the SVG symbol |
| 113 | + $viewBox = $svgElement->getAttribute('viewBox'); |
| 114 | + $preserveAspectRatio = $svgElement->getAttribute('preserveAspectRatio'); |
| 115 | + |
| 116 | + // Extract title if present to use as a default title for the icon. |
| 117 | + $titleElement = $svgElement->getElementsByTagName('title')->item(0); |
| 118 | + $title = $titleElement ? $titleElement->textContent : ''; |
| 119 | + |
| 120 | + // Extract the aria-label if present to use as a default label for the icon. |
| 121 | + $label = $svgElement->getAttribute('aria-label'); |
| 122 | + |
| 123 | + $output = "@php \$title ??= '" . addslashes($title) . "'; @endphp\n"; |
| 124 | + $output .= "@php \$titleId = '$viewId-' . uniqid(); @endphp\n"; |
| 125 | + $output .= "@php \$label ??= '" . addslashes($label) . "'; @endphp\n"; |
| 126 | + |
| 127 | + $output .= '<svg xmlns="http://www.w3.org/2000/svg" version="2.0" role="img"'; |
| 128 | + $output .= $this->buildSvgAttributeString([ |
| 129 | + 'width' => $width, |
| 130 | + 'height' => $height, |
| 131 | + ]) . "\n"; |
| 132 | + |
| 133 | + $output .= " @if (!empty(\$label)) aria-label=\"{{ \$label }}\" @endif\n"; |
| 134 | + $output .= " @if (!empty(\$title)) aria-describedby=\"{{ \$titleId }}\" @endif\n"; |
| 135 | + $output .= ">\n"; |
| 136 | + |
| 137 | + $output .= " @if (!empty(\$title))\n"; |
| 138 | + $output .= " <title id=\"{{ \$titleId }}\">{{ \$title }}</title>\n"; |
| 139 | + $output .= " @endif\n"; |
| 140 | + |
| 141 | + $output .= " @if (\$firstRender)\n"; |
| 142 | + $output .= " <defs>\n"; |
| 143 | + $output .= " <symbol id=\"svg-icon-$viewId\""; |
| 144 | + $output .= $this->buildSvgAttributeString([ |
| 145 | + 'viewBox' => $viewBox, |
| 146 | + 'preserveAspectRatio' => $preserveAspectRatio, |
| 147 | + ]); |
| 148 | + $output .= ">\n"; |
| 149 | + |
| 150 | + // Add all child nodes except title |
| 151 | + foreach ($svgElement->childNodes as $child) { |
| 152 | + if ($child instanceof \DOMElement && $child->tagName === 'title') { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + $content = mb_trim($child->ownerDocument->saveXML($child)); |
| 157 | + if (!empty($content)) { |
| 158 | + $output .= " $content\n"; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + $output .= " </symbol>\n"; |
| 163 | + $output .= " </defs>\n"; |
| 164 | + $output .= " @endif\n"; |
| 165 | + $output .= " <use href=\"#svg-icon-$viewId\"/>\n"; |
| 166 | + $output .= "</svg>\n"; |
| 167 | + |
| 168 | + return $output; |
| 169 | + } |
| 170 | +} |
0 commit comments