]> BookStack Code Mirror - bookstack/blob - app/Util/SvgIcon.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Util / SvgIcon.php
1 <?php
2
3 namespace BookStack\Util;
4
5 class SvgIcon
6 {
7     public function __construct(
8         protected string $name,
9         protected array $attrs = []
10     ) {
11     }
12
13     public function toHtml(): string
14     {
15         $attrs = array_merge([
16             'class'     => 'svg-icon',
17             'data-icon' => $this->name,
18             'role'      => 'presentation',
19         ], $this->attrs);
20
21         $attrString = ' ';
22         foreach ($attrs as $attrName => $attr) {
23             $attrString .= $attrName . '="' . $attr . '" ';
24         }
25
26         $iconPath = resource_path('icons/' . $this->name . '.svg');
27         $themeIconPath = theme_path('icons/' . $this->name . '.svg');
28
29         if ($themeIconPath && file_exists($themeIconPath)) {
30             $iconPath = $themeIconPath;
31         } elseif (!file_exists($iconPath)) {
32             return '';
33         }
34
35         $fileContents = file_get_contents($iconPath);
36
37         return str_replace('<svg', '<svg' . $attrString, $fileContents);
38     }
39 }