Files
xamxam/app/src/icon.php
T
Pontoporeia 34b5d3e585 justfile: standardise test recipes to lint-php/lint-css/lint-js/test + add fix recipe
- Removed ambiguous aliases: phpstan, cs-check (both pointed to lint-php which
  also ran php-cs-fixer, making the names misleading)
- Removed syntax (php -l) — redundant, phpstan already catches parse errors
- Split lint-biome into lint-css and lint-js with correct paths
- Added lint meta-recipe that runs all three linters
- Added fix recipe: biome check --write (CSS/JS format+lint) + php-cs-fixer fix (PHP)
- Updated build-lint to delegate to lint-css + lint-js
2026-07-05 10:50:42 +02:00

40 lines
1.5 KiB
PHP

<?php
/**
* Inline SVG icon helper.
*
* Returns the SVG markup for an icon from /assets/icons/{name}.svg.
* The SVG is inlined into the DOM so CSS `color` / `fill` cascade naturally.
* Width/height are set to 1em by default; override via CSS on the parent.
*
* Usage: <?= icon('trash') ?>
* <?= icon('search', 0, 'header-search-icon') ?>
*/
function icon(string $name, int $size = 0, string $class = ''): string
{
$path = APP_ROOT . "/public/assets/icons/{$name}.svg";
if (!file_exists($path)) {
return "<!-- icon not found: {$name} -->";
}
$svg = file_get_contents($path);
// Normalise width/height to 1em so icons scale with font-size
$svg = preg_replace('/\bwidth="[^"]*"/', 'width="1em"', $svg);
$svg = preg_replace('/\bheight="[^"]*"/', 'height="1em"', $svg);
// Ensure aria-hidden by default (icons are decorative when used via this helper)
if (!str_contains($svg, 'aria-hidden')) {
$svg = str_replace('<svg', '<svg aria-hidden="true"', $svg);
}
// Inject CSS class if provided
if ($class !== '') {
if (str_contains($svg, 'class="')) {
$svg = str_replace('class="', 'class="' . $class . ' ', $svg);
} else {
$svg = str_replace('<svg', '<svg class="' . $class . '"', $svg);
}
}
// Collapse newlines: otherwise raw SVG markup breaks JS string literals
// when icon() is used inside <script> tags (e.g., contenus.php inline rename)
$svg = str_replace(["\n", "\r"], '', $svg);
return $svg;
}