|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * For icons, we're using SVG sprite technique. This big svg would be PITA to |
| 6 | + * maintain, so source icons are in single files. This script takes all .svg |
| 7 | + * files from given folder (recursively) and combines them into that svg sprite. |
| 8 | + */ |
| 9 | + |
| 10 | +// Some setup. |
| 11 | +$inputPath = __DIR__ . '/../icons'; |
| 12 | +$outputPath = realpath(__DIR__ . '/../build') . '/muil.icons.svg'; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Read directory recursively and return it's content in array filename => file content. |
| 17 | + * |
| 18 | + * @param string Icon directory location. |
| 19 | + * @param string Icon prefix (directory name) - used when called recursively. |
| 20 | + * @return array<iconName, svgCode> |
| 21 | + */ |
| 22 | +function readIcons(string $path, string $prefix = ''): array { |
| 23 | + $icons = []; |
| 24 | + |
| 25 | + foreach (new FilesystemIterator($path) as $fileInfo) { |
| 26 | + if ($fileInfo->getExtension() === 'svg') { |
| 27 | + $id = $fileInfo->getBasename('.svg'); |
| 28 | + $id = $prefix ? $prefix . '-' . $id : $id; |
| 29 | + $icons[$id] = file_get_contents($fileInfo->getPathname()); |
| 30 | + } elseif ($fileInfo->isDir()) { |
| 31 | + $dirName = $fileInfo->getFilename(); |
| 32 | + $icons = array_merge($icons, readIcons($fileInfo->getRealPath(), $dirName)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return $icons; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Convert SVG file into SVG symbol tag. |
| 41 | + * |
| 42 | + * @param string Icon SVG source. |
| 43 | + * @param string Icon name |
| 44 | + * @return string |
| 45 | + */ |
| 46 | +function fileToSymbol(string $svg, string $id): string { |
| 47 | + $svg = str_replace(' ', null, $svg); |
| 48 | + $svg = str_replace("\n", null, $svg); |
| 49 | + $svg = str_replace(' xmlns="http://www.w3.org/2000/svg"', null, $svg); |
| 50 | + $svg = str_replace('<svg ', sprintf('<symbol id="%s" ', $id), $svg); |
| 51 | + $svg = str_replace('</svg>', '</symbol>', $svg); |
| 52 | + |
| 53 | + // Unfuck firefox. |
| 54 | + $svg = preg_replace('/ width="[0-9]+"/', null, $svg); |
| 55 | + $svg = preg_replace('/ height="[0-9]+"/', null, $svg); |
| 56 | + |
| 57 | + return trim($svg) . PHP_EOL; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +// Read symbols. |
| 62 | +$symbols = []; |
| 63 | +$icons = readIcons($inputPath); |
| 64 | +$count = count($icons); |
| 65 | +echo "Processing $count files \n"; |
| 66 | +foreach ($icons as $id => $svg) { |
| 67 | + $symbols[$id] = fileToSymbol($svg, $id); |
| 68 | + echo "."; |
| 69 | +} |
| 70 | + |
| 71 | +ksort($symbols); |
| 72 | + |
| 73 | +echo "\n"; |
| 74 | +echo "Exporting sprite: $outputPath"; |
| 75 | + |
| 76 | +// Write sprite. |
| 77 | +file_put_contents($outputPath, sprintf( |
| 78 | + '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0">%s</svg>%s', |
| 79 | + implode('', $symbols), PHP_EOL |
| 80 | +)); |
| 81 | +echo "\n"; |
| 82 | + |
0 commit comments