Skip to content

Commit a7ac7da

Browse files
Assets Upgrade, Added Relative Path
1 parent 5c415f5 commit a7ac7da

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

Asset.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class Asset{
1818
* - asset file e.g (style.css | js/main.js)
1919
*
2020
* @param bool|null $cache
21+
* @param bool|null $path_type
2122
*
2223
* @return string
2324
*/
24-
static public function asset(?string $asset = null, $cache = null)
25+
static public function asset(?string $asset = null, $cache = null, $path_type = null)
2526
{
2627
// if coniguration has not been used in the global space
2728
// then we call to define paths for us
@@ -38,6 +39,12 @@ static public function asset(?string $asset = null, $cache = null)
3839
$cache = $assetPath['cache'];
3940
}
4041

42+
// if asset method path_type is not null
43+
// then we override the global configuration
44+
if(!is_bool($path_type)){
45+
$path_type = $assetPath['path_type'];
46+
}
47+
4148
// trim
4249
$asset = Str::trim($asset, '/');
4350

@@ -55,7 +62,18 @@ static public function asset(?string $asset = null, $cache = null)
5562
$cacheTimeAppend = self::getFiletime($file_server) ?? null;
5663
}
5764
}
58-
65+
66+
// if `$path_type` is true, then we'll use relative path
67+
if($path_type){
68+
69+
// replace domain path
70+
$domain = Str::replace($assetPath['removeDomain'], '', $file_domain);
71+
$domain = ltrim($domain, '/');
72+
73+
return "/{$domain}{$cacheTimeAppend}";
74+
}
75+
76+
// Using absolute path
5977
return "{$file_domain}{$cacheTimeAppend}";
6078
}
6179

@@ -72,9 +90,12 @@ static public function asset(?string $asset = null, $cache = null)
7290
* - This will automatically tells the broswer to fetch new file if the time change
7391
* - Time will only change if you make changes or modify the request file
7492
*
93+
* @param string $path_type
94+
* -[optional] Default is false (Absolute Path)|Else -- False is (Relative path)
95+
*
7596
* @return void
7697
*/
77-
static public function config(?string $base_path = null, ?bool $cache = false)
98+
static public function config(?string $base_path = null, ?bool $cache = false, $path_type = false)
7899
{
79100
// if not defined
80101
if(!defined('ASSET_BASE_DIRECTORY')){
@@ -101,11 +122,13 @@ static public function config(?string $base_path = null, ?bool $cache = false)
101122

102123
define('ASSET_BASE_DIRECTORY', [
103124
'cache' => $cache,
125+
'path_type' => $path_type,
104126
'server' => self::formatWithBaseDirectory($base_path),
105127
'domain' => rtrim(
106128
self::cleanServerPath($urlFromhelper),
107129
'/'
108130
),
131+
'removeDomain' => UrlHelper::http() . UrlHelper::host()
109132
]);
110133
}
111134
}

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,13 @@ tasset('css/style.css');
505505
```
506506

507507
## Asset Config
508-
- Takes two param as `string`
509-
- `base_path` path base directory
510-
- `cache` Tells method to return `cache` of assets.
508+
- Takes three param as `string`
509+
510+
| params | Description |
511+
|---------------|-----------------------------|
512+
| base_path | PAth to file |
513+
| cache | By Default `cache` is set to `false`. Tell method to include cache for each file |
514+
| path_type | By Default `cache` is set to `false`, which uses absolute path for all files. While `true` will use relative path |
511515
512516

513517
```
@@ -525,7 +529,7 @@ config_asset('public');
525529
```
526530

527531
### Asset Cache
528-
- By Default, `cache` is set to `true`
532+
- By Default, `cache` is set to `false`
529533
- You'll see a link representation as `http://domain.com/[path_to_asset_file]?v=111111111`
530534

531535
```
@@ -537,9 +541,12 @@ http://domain.com/storage/[asset_file]
537541

538542
- or -- `Helpers Function`
539543
```
540-
asset_config('storage', true);
544+
asset_config('storage/main.js', true);
545+
// Output: http://domain.com/storage/main.js?v=111111111
546+
541547
542-
http://domain.com/storage/[asset_file]?v=111111111
548+
asset_config('storage/style.css', true, true);
549+
// Output: /storage/style.css?v=111111111
543550
```
544551

545552
## ENV

Tests/url.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
config_asset('/', true);
1212

1313

14-
dd(
14+
dump(
1515
domain(),
1616
domain('admin'),
1717
tasset('zip.php'),
18+
tasset('zip.php', true, true),
1819

1920
urlHelper()->server(),
20-
21+
2122
[
2223
urlHelper()->url(),
2324
urlHelper()->full(),
@@ -26,8 +27,19 @@
2627
urlHelper()->http(),
2728
urlHelper()->host(),
2829
urlHelper()->path(),
29-
urlHelper()->path(),
3030
]
3131

3232
);
3333

34+
$unitImg = tasset('thousand_units.png');
35+
$unitImg2 = tasset('thousand_units.png', true, true);
36+
37+
?>
38+
39+
<a href="<?= $unitImg?>" target="_blank">
40+
<img src="<?= $unitImg?>" alt="units" width="300">
41+
</a>
42+
43+
<a href="<?= $unitImg2?>" target="_blank">
44+
<img src="<?= $unitImg2?>" alt="units" width="300">
45+
</a>

helpers.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,14 @@ function tview($viewPath, $data = [])
294294
*
295295
* @param bool|null $cache
296296
*
297+
* @param bool|null $path_type
298+
* -[optional] Default is false (Absolute Path)|Else -- False is (Relative path)
299+
*
297300
* @return string
298301
*/
299-
function tasset($asset = null, $cache = null)
302+
function tasset($asset = null, $cache = null, $path_type = null)
300303
{
301-
return Asset::asset($asset, $cache);
304+
return Asset::asset($asset, $cache, $path_type);
302305
}
303306
}
304307

@@ -310,17 +313,20 @@ function tasset($asset = null, $cache = null)
310313
* - [optional] Default is `base_directory/assets`
311314
* - If set and directory is not found, then we revert back to the default
312315
*
313-
* @param string $cache
316+
* @param bool $cache
314317
* - [optional] Default is false
315318
* - End point of link `?v=xxxxxxxx` is with cache of file time change
316319
* - This will automatically tells the broswer to fetch new file if the time change
317320
* - Time will only change if you make changes or modify the request file
318321
*
322+
* @param bool $path_type
323+
* -[optional] Default is false (Absolute Path)|Else -- False is (Relative path)
324+
*
319325
* @return void
320326
*/
321-
function config_asset($base_path = null, ?bool $cache = false)
327+
function config_asset($base_path = null, ?bool $cache = false, ?bool $path_type = false)
322328
{
323-
Asset::config($base_path, $cache);
329+
Asset::config($base_path, $cache, $path_type);
324330
}
325331
}
326332

0 commit comments

Comments
 (0)