|
13 | 13 | */ |
14 | 14 | class Laravel{ |
15 | 15 |
|
| 16 | + |
16 | 17 | /** |
17 | | - * Create SVG Directrives |
18 | | - * |
19 | | - * @usage @svg('path_to_svg') |
20 | | - * |
21 | | - * @return mixed |
| 18 | + * Register a Blade directive |
| 19 | + * |
| 20 | + * @return void |
22 | 21 | */ |
23 | | - static public function svgDirective() |
| 22 | + public function cssDirective() |
24 | 23 | { |
25 | | - Tame::class_exists('Illuminate\Support\Facades\Blade', function(){ |
| 24 | + Blade::directive('css', function ($expression) { |
| 25 | + list($path, $class) = array_pad(explode(',', $expression, 2), 2, ''); |
26 | 26 |
|
27 | | - // Register a Blade directive |
28 | | - Blade::directive('svg', function ($expression) { |
| 27 | + $path = str_replace(['"', "'"], '', $path); |
| 28 | + $class = str_replace(['"', "'"], '', $class); |
29 | 29 |
|
30 | | - list($path, $class) = array_pad(explode(',', $expression, 2), 2, ''); |
| 30 | + // fullpath |
| 31 | + $assets = asset($path, true); |
31 | 32 |
|
32 | | - $path = str_replace(['"', "'"], '', $path); |
33 | | - $class = str_replace(['"', "'"], '', $class); |
| 33 | + return "<link rel='stylesheet' type='text/css' href='{$assets}'>"; |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Register a Blade directive |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public function jsDirective() |
| 43 | + { |
| 44 | + Blade::directive('js', function ($expression) { |
| 45 | + list($path, $class) = array_pad(explode(',', $expression, 2), 2, ''); |
| 46 | + |
| 47 | + $path = str_replace(['"', "'"], '', $path); |
| 48 | + $class = str_replace(['"', "'"], '', $class); |
| 49 | + |
| 50 | + // fullpath |
| 51 | + $assets = asset($path, true); |
| 52 | + |
| 53 | + return "<script src='{$assets}'></script>"; |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Register a Blade directive |
| 59 | + * |
| 60 | + * @return mixed |
| 61 | + */ |
| 62 | + public function svgDirective() |
| 63 | + { |
| 64 | + Blade::directive('svg', function ($expression) { |
| 65 | + list($path, $class) = array_pad(explode(',', $expression, 2), 2, ''); |
| 66 | + |
| 67 | + $path = str_replace(['"', "'"], '', $path); |
| 68 | + $class = str_replace(['"', "'"], '', $class); |
| 69 | + |
| 70 | + // fullpath |
| 71 | + $fullPath = public_path($path); |
| 72 | + |
| 73 | + // if file exists |
| 74 | + if(Tame::exists($fullPath)){ |
34 | 75 |
|
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 | | - }; |
58 | | - }); |
| 76 | + $svg = new \DOMDocument(); |
| 77 | + $svg->load($fullPath); |
| 78 | + |
| 79 | + // If a class is provided, add it to the SVG element |
| 80 | + if (!empty($class)) { |
| 81 | + $svg->documentElement->setAttribute("class", $class); |
| 82 | + } |
| 83 | + |
| 84 | + $output = $svg->saveXML($svg->documentElement); |
| 85 | + |
| 86 | + // Return the modified SVG |
| 87 | + return $output; |
| 88 | + }; |
59 | 89 | }); |
60 | 90 | } |
61 | 91 |
|
|
0 commit comments