| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Echo <link rel="stylesheet"> tags for every *.css file in the templates folder. |
| 5 | * |
| 6 | * @param string $templatesDirFs Filesystem path to templates dir, e.g. __DIR__ . '/templates' |
| 7 | * @param string $templatesDirUrl URL path to templates dir, e.g. '/templates' |
| 8 | */ |
| 9 | function echoTemplateCssLinks(string $templatesDirFs, string $templatesDirUrl = '/templates'): void |
| 10 | { |
| 11 | $templatesDirFs = rtrim($templatesDirFs, DIRECTORY_SEPARATOR); |
| 12 | $templatesDirUrl = rtrim($templatesDirUrl, '/'); |
| 13 | |
| 14 | if (!is_dir($templatesDirFs)) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | $files = glob($templatesDirFs . DIRECTORY_SEPARATOR . '*.css'); |
| 19 | if ($files === false) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | sort($files, SORT_NATURAL | SORT_FLAG_CASE); |
| 24 | |
| 25 | foreach ($files as $filePath) { |
| 26 | $fileName = basename($filePath); |
| 27 | |
| 28 | if (!preg_match('/^[a-zA-Z0-9_.-]+\.css$/', $fileName)) { |
| 29 | continue; |
| 30 | } |
| 31 | |
| 32 | $href = $templatesDirUrl . '/' . rawurlencode($fileName); |
| 33 | echo '<link rel="stylesheet" href="' . htmlspecialchars($href, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '">' . "\n"; |
| 34 | } |
| 35 | } |