|
9 | 9 |
|
10 | 10 |
|
11 | 11 | /** |
12 | | - * Laravel Wrapper |
13 | | - */ |
| 12 | + * Laravel Blade Directives Wrapper |
| 13 | + * Provides custom reusable Blade directives |
| 14 | + */ |
14 | 15 | class Laravel{ |
15 | | - |
| 16 | + |
| 17 | + /** |
| 18 | + * Initialize blade facades |
| 19 | + */ |
| 20 | + private static function initBladeFacades(): string |
| 21 | + { |
| 22 | + return "\Illuminate\Support\Facades\Blade"; |
| 23 | + } |
16 | 24 |
|
17 | 25 | /** |
18 | | - * Register a Blade directive |
| 26 | + * Register all directives at once |
| 27 | + * Useful for quick bootstrapping inside a ServiceProvider |
19 | 28 | * |
20 | 29 | * @return void |
21 | 30 | */ |
22 | | - public function cssDirective() |
| 31 | + public static function registerDirectives() |
23 | 32 | { |
24 | | - Blade::directive('css', function ($expression) { |
25 | | - list($path, $class) = array_pad(explode(',', $expression, 2), 2, ''); |
26 | | - |
27 | | - $path = str_replace(['"', "'"], '', $path); |
28 | | - $class = str_replace(['"', "'"], '', $class); |
29 | | - |
30 | | - // fullpath |
31 | | - $assets = tasset($path, true); |
| 33 | + self::cssDirective(); |
| 34 | + self::jsDirective(); |
| 35 | + self::svgDirective(); |
| 36 | + self::assetDirective(); |
| 37 | + } |
32 | 38 |
|
33 | | - return "<link rel='stylesheet' type='text/css' href='{$assets}'>"; |
| 39 | + /** |
| 40 | + * Register the @css directive |
| 41 | + * Usage: @css('css/app.css') |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public static function cssDirective() |
| 46 | + { |
| 47 | + self::initBladeFacades()::directive('css', function ($expression) { |
| 48 | + return "<?php |
| 49 | + list(\$path, \$class) = array_pad(explode(',', {$expression}, 2), 2, ''); |
| 50 | + \$path = str_replace(['\"', \"'\"], '', \$path); |
| 51 | + \$class = str_replace(['\"', \"'\"], '', \$class); |
| 52 | + \$assets = tasset(\$path, true, true); |
| 53 | + echo \"<link rel='stylesheet' type='text/css' href='\$assets'>\"; |
| 54 | + ?>"; |
34 | 55 | }); |
35 | 56 | } |
36 | 57 |
|
37 | 58 | /** |
38 | | - * Register a Blade directive |
| 59 | + * Register the @js directive |
| 60 | + * Usage: @js('js/app.js') |
39 | 61 | * |
40 | 62 | * @return void |
41 | 63 | */ |
42 | | - public function jsDirective() |
| 64 | + public static function jsDirective() |
43 | 65 | { |
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 = tasset($path, true); |
52 | | - |
53 | | - return "<script src='{$assets}'></script>"; |
| 66 | + self::initBladeFacades()::directive('js', function ($expression) { |
| 67 | + return "<?php |
| 68 | + list(\$path, \$class) = array_pad(explode(',', {$expression}, 2), 2, ''); |
| 69 | + \$path = str_replace(['\"', \"'\"], '', \$path); |
| 70 | + \$class = str_replace(['\"', \"'\"], '', \$class); |
| 71 | + \$assets = tasset(\$path, true, true); |
| 72 | + echo \"<script src='\$assets'></script>\"; |
| 73 | + ?>"; |
54 | 74 | }); |
55 | 75 | } |
56 | 76 |
|
57 | 77 | /** |
58 | | - * Register a Blade directive |
| 78 | + * Register the @svg directive |
| 79 | + * Usage: @svg('images/icon.svg', 'w-6 h-6 text-gray-500') |
59 | 80 | * |
60 | | - * @return mixed |
| 81 | + * @return void |
61 | 82 | */ |
62 | | - public function svgDirective() |
| 83 | + public static function svgDirective() |
63 | 84 | { |
64 | | - Blade::directive('svg', function ($expression) { |
65 | | - list($path, $class) = array_pad(explode(',', $expression, 2), 2, ''); |
| 85 | + self::initBladeFacades()::directive('svg', function ($expression) { |
| 86 | + return "<?php |
| 87 | + list(\$path, \$class) = array_pad(explode(',', {$expression}, 2), 2, ''); |
| 88 | + \$path = str_replace(['\"', \"'\"], '', \$path); |
| 89 | + \$class = str_replace(['\"', \"'\"], '', \$class); |
| 90 | + |
| 91 | + \$fullPath = tasset(\$path, false, false); |
66 | 92 |
|
67 | | - $path = str_replace(['"', "'"], '', $path); |
68 | | - $class = str_replace(['"', "'"], '', $class); |
69 | | - |
70 | | - // fullpath |
71 | | - $fullPath = public_path($path); |
| 93 | + if (\\Tamedevelopers\\Support\\Tame::exists(\$fullPath)) { |
| 94 | + \$svg = new \\DOMDocument(); |
| 95 | + \$svg->load(\$fullPath); |
72 | 96 |
|
73 | | - // if file exists |
74 | | - if(Tame::exists($fullPath)){ |
75 | | - |
76 | | - $svg = new \DOMDocument(); |
77 | | - $svg->load($fullPath); |
| 97 | + if (!empty(\$class)) { |
| 98 | + \$svg->documentElement->setAttribute('class', \$class); |
| 99 | + } |
78 | 100 |
|
79 | | - // If a class is provided, add it to the SVG element |
80 | | - if (!empty($class)) { |
81 | | - $svg->documentElement->setAttribute("class", $class); |
| 101 | + echo \$svg->saveXML(\$svg->documentElement); |
82 | 102 | } |
| 103 | + ?>"; |
| 104 | + }); |
| 105 | + } |
83 | 106 |
|
84 | | - $output = $svg->saveXML($svg->documentElement); |
85 | | - |
86 | | - // Return the modified SVG |
87 | | - return $output; |
88 | | - }; |
| 107 | + /** |
| 108 | + * Register the @asset directive |
| 109 | + * Allows passing extra params for cache-busting control |
| 110 | + * Usage: @asset('images/logo.png', true, true) |
| 111 | + * |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + public static function assetDirective(): void |
| 115 | + { |
| 116 | + self::initBladeFacades()::directive('asset', function ($expression) { |
| 117 | + return "<?php |
| 118 | + \$params = explode(',', {$expression}); |
| 119 | + \$params = array_map(fn(\$p) => trim(\$p, '\"\\' '), \$params); |
| 120 | +
|
| 121 | + // Extract parameters with defaults |
| 122 | + \$asset = \$params[0] ?? ''; |
| 123 | + \$cache = isset(\$params[1]) ? filter_var(\$params[1], FILTER_VALIDATE_BOOLEAN) : true; |
| 124 | + \$path_type = isset(\$params[2]) ? filter_var(\$params[2], FILTER_VALIDATE_BOOLEAN) : true; |
| 125 | +
|
| 126 | + echo tasset(\$asset, \$cache, \$path_type); |
| 127 | + ?>"; |
89 | 128 | }); |
90 | 129 | } |
91 | 130 |
|
|
0 commit comments