@@ -26,22 +26,35 @@ static public function svgDirective()
2626
2727 // Register a Blade directive
2828 Blade::directive ('svg ' , function ($ expression ) {
29- // Parse the expression to get the path and classes
30- list ($ path , $ classes ) = explode (', ' , $ expression , 2 );
3129
32- // path
33-
34- // Load the SVG file contents
35- $ svgContent = file_get_contents (public_path (trim ($ path , '" ' )));
36-
37- // Add classes to SVG
38- $ svg = simplexml_load_string ($ svgContent );
39- if (!empty ($ classes )) {
40- $ svg ->addAttribute ('class ' , trim ($ classes , '" ' ));
41- }
42-
43- // Return the modified SVG
44- return $ svg ->asXML ();
30+ list ($ path , $ class ) = array_pad (explode (', ' , $ expression , 2 ), 2 , '' );
31+
32+ $ path = str_replace (['" ' , "' " ], '' , $ path );
33+ $ class = str_replace (['" ' , "' " ], '' , $ class );
34+
35+ // fullpath
36+ $ fullPath = public_path ($ path );
37+
38+ // if file exists
39+ if (Tame::exists ($ fullPath )){
40+ // Load the SVG file contents
41+ $ svgContent = file_get_contents ($ fullPath );
42+
43+ // Parse the SVG content into a SimpleXMLElement
44+ $ svg = simplexml_load_string ($ svgContent );
45+
46+ // If a class is provided, add it to the SVG element
47+ if (!empty ($ class )) {
48+ if (isset ($ svg ['class ' ])) {
49+ $ svg ['class ' ] .= ' ' . $ class ;
50+ } else {
51+ $ svg ->addAttribute ('class ' , $ class );
52+ }
53+ }
54+
55+ // Return the modified SVG
56+ return $ svg ->asXML ();
57+ };
4558 });
4659 });
4760 }
0 commit comments