Skip to content

Commit 0107044

Browse files
Laravel Blade Directives Wrapper
1 parent 820ba0f commit 0107044

File tree

3 files changed

+126
-52
lines changed

3 files changed

+126
-52
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Support Package
2-
Support Package For PHP and Laravel
2+
Support Package For PHP, Laravel and PHP Frameworks
33

44
[![Total Downloads](https://poser.pugx.org/tamedevelopers/support/downloads)](https://packagist.org/packages/tamedevelopers/support)
55
[![Latest Stable Version](https://poser.pugx.org/tamedevelopers/support/version)](https://packagist.org/packages/tamedevelopers/support)
@@ -308,6 +308,8 @@ Support Package For PHP and Laravel
308308
* [Asset](#Asset)
309309
* [Asset config](#asset-config)
310310
* [Asset Cache](#asset-cache)
311+
* [Laravel](#laravel)
312+
* [Blade Directives](#blade-directives)
311313
* [View](#view)
312314
* [Usage](#view-usage)
313315
* [Support](#view-support)
@@ -1327,6 +1329,39 @@ config_asset('storage/style.css', true, true);
13271329
// Output: /storage/style.css?v=111111111
13281330
```
13291331

1332+
1333+
## Laravel
1334+
- Customer Laravel Blade Directives out of th box
1335+
1336+
1337+
### Blade Directives
1338+
- Uses the `config_asset()` Default path as an entry level for root-level folder
1339+
1340+
| name | Description/Usage |
1341+
|--------|-----------------------------|
1342+
| @css | `@css('css/app.css')` |
1343+
| @js | `@js('js/app.js')` |
1344+
| @asset | `@asset('images/logo.png', true, true)` - Similar to `tasset()` |
1345+
| @svg | `@svg('images/icon.svg', 'w-6 h-6 text-gray-500')` - Takes path and custom class to be attached to the svg |
1346+
1347+
1348+
```php
1349+
// if you've define root access inside the public folder
1350+
1351+
config_asset(
1352+
base_path: '/', // staring from project root[no default folder]
1353+
cache: true, // ability to invalidate and cache files
1354+
);
1355+
```
1356+
- `then`
1357+
1358+
```html
1359+
<div>
1360+
@js('public/assets/ckeditor5/build/ckeditor.js');
1361+
@svg('resources/svg/icon.svg');
1362+
</div>
1363+
```
1364+
13301365
## View
13311366

13321367
### View Usage

src/Asset.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public static function asset(?string $asset = null, $cache = null, $path_type =
3434
// asset path
3535
$assetPath = ASSET_BASE_DIRECTORY;
3636

37-
// if asset method cache is not null
37+
// if asset method cache is not bool
3838
// then we override the global configuration
3939
if(!is_bool($cache)){
4040
$cache = $assetPath['cache'];
4141
}
4242

43-
// if asset method path_type is not null
43+
// if asset method path_type is not bool
4444
// then we override the global configuration
4545
if(!is_bool($path_type)){
4646
$path_type = $assetPath['path_type'];

src/Laravel.php

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,122 @@
99

1010

1111
/**
12-
* Laravel Wrapper
13-
*/
12+
* Laravel Blade Directives Wrapper
13+
* Provides custom reusable Blade directives
14+
*/
1415
class Laravel{
15-
16+
17+
/**
18+
* Initialize blade facades
19+
*/
20+
private static function initBladeFacades(): string
21+
{
22+
return "\Illuminate\Support\Facades\Blade";
23+
}
1624

1725
/**
18-
* Register a Blade directive
26+
* Register all directives at once
27+
* Useful for quick bootstrapping inside a ServiceProvider
1928
*
2029
* @return void
2130
*/
22-
public function cssDirective()
31+
public static function registerDirectives()
2332
{
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+
}
3238

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+
?>";
3455
});
3556
}
3657

3758
/**
38-
* Register a Blade directive
59+
* Register the @js directive
60+
* Usage: @js('js/app.js')
3961
*
4062
* @return void
4163
*/
42-
public function jsDirective()
64+
public static function jsDirective()
4365
{
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+
?>";
5474
});
5575
}
5676

5777
/**
58-
* Register a Blade directive
78+
* Register the @svg directive
79+
* Usage: @svg('images/icon.svg', 'w-6 h-6 text-gray-500')
5980
*
60-
* @return mixed
81+
* @return void
6182
*/
62-
public function svgDirective()
83+
public static function svgDirective()
6384
{
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);
6692
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);
7296
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+
}
78100
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);
82102
}
103+
?>";
104+
});
105+
}
83106

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+
?>";
89128
});
90129
}
91130

0 commit comments

Comments
 (0)