1111use Tempest \Core \AppConfig ;
1212use Tempest \Http \Status ;
1313use Tempest \HttpClient \HttpClient ;
14+ use Tempest \Support \Html \HtmlString ;
1415use Tempest \Support \Str \ImmutableString ;
15- use Tempest \View \Elements \ViewComponentElement ;
1616use Tempest \View \IconConfig ;
17- use Tempest \View \ViewComponent ;
1817
19- final readonly class Icon implements ViewComponent
18+ final readonly class Icon
2019{
2120 public function __construct (
2221 private AppConfig $ appConfig ,
@@ -25,28 +24,21 @@ public function __construct(
2524 private HttpClient $ http ,
2625 ) {}
2726
28- public static function getName ( ): string
27+ public function render ( string $ name , ? string $ class = null ): HtmlString
2928 {
30- return 'x-icon ' ;
31- }
32-
33- public function compile (ViewComponentElement $ element ): string
34- {
35- $ name = $ element ->getAttribute ('name ' );
36- $ class = $ element ->getAttribute ('class ' );
29+ $ html = $ this ->svg ($ name );
3730
38- $ svg = $ this ->render ($ name );
39-
40- if (! $ svg ) {
41- return $ this ->appConfig ->environment ->isLocal ()
31+ if (! $ html ) {
32+ return new HtmlString ($ this ->appConfig ->environment ->isLocal ()
4233 ? ('<!-- unknown-icon: ' . $ name . ' --> ' )
43- : '' ;
34+ : '' );
35+ }
36+
37+ if ($ class ) {
38+ $ html = $ this ->injectClass ($ html , $ class );
4439 }
4540
46- return match ($ class ) {
47- null => $ svg ,
48- default => $ this ->injectClass ($ svg , $ class ),
49- };
41+ return new HtmlString ($ html );
5042 }
5143
5244 /**
@@ -79,7 +71,7 @@ private function download(string $prefix, string $name): ?string
7971 * in the cache, it will download it on the fly and cache it for future
8072 * use. If the icon is already in the cache, it will be served from there.
8173 */
82- private function render (string $ name ): ?string
74+ private function svg (string $ name ): ?string
8375 {
8476 try {
8577 $ parts = explode (': ' , $ name , 2 );
@@ -110,8 +102,8 @@ private function injectClass(string $svg, string $class): string
110102 {
111103 return new ImmutableString ($ svg )
112104 ->replace (
113- search: '<svg ' ,
114- replace: "<svg class= \"{$ class }\" " ,
105+ search: '<svg ' ,
106+ replace: "<svg class= \"{$ class }\"" ,
115107 )
116108 ->toString ();
117109 }
0 commit comments