-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asset.php
127 lines (104 loc) · 3.41 KB
/
Asset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Tamedevelopers\Support\Str;
use Tamedevelopers\Support\Traits\ServerTrait;
class Asset{
use ServerTrait;
/**
* Create assets Real path url
*
* @param string $asset
* - asset file e.g (style.css | js/main.js)
*
* @param bool|null $cache
*
* @return string
*/
static public function asset(?string $asset = null, $cache = null)
{
// if coniguration has not been used in the global space
// then we call to define paths for us
if(!defined('ASSET_BASE_DIRECTORY')){
self::config();
}
// asset path
$assetPath = ASSET_BASE_DIRECTORY;
// if asset method cache is not null
// then we override the global configuration
if(!is_bool($cache)){
$cache = $assetPath['cache'];
}
// trim
$asset = Str::trim($asset, '/');
$file_domain = "{$assetPath['domain']}/{$asset}";
// file server path
$file_server = "{$assetPath['server']}/{$asset}";
// append file update time
$cacheTimeAppend = null;
// cache allow from self method
if($cache){
if(!empty($asset)){
$cacheTimeAppend = self::getFiletime($file_server) ?? null;
}
}
return "{$file_domain}{$cacheTimeAppend}";
}
/**
* Configure Assets Default Directory
*
* @param string $base_path
* - [optional] Default is `base_directory/assets`
* - If set and directory is not found, then we revert back to the default
*
* @param bool $cache
* - [optional] Default is true
* - End point of link `?v=xxxxxxxx` is with cache of file time change
* - This will automatically tells the broswer to fetch new file if the time change
* - Time will only change if you make changes or modify the request file
*
* @return void
*/
static public function config(?string $base_path = null, ?bool $cache = false)
{
// if not defined
if(!defined('ASSET_BASE_DIRECTORY')){
// url helper class
$urlFromhelper = UrlHelper::url();
// if base path is set
if(!empty($base_path)){
// - Trim forward slash from left and right
$base_path = Str::trim($base_path, '/');
// base for url path
$baseForUrlPath = $base_path;
// check if accessed from default ip:address
if(UrlHelper::isIpAccessedVia127Port()){
$baseForUrlPath = '';
}
// compile
$urlFromhelper = "{$urlFromhelper}/{$baseForUrlPath}";
}
define('ASSET_BASE_DIRECTORY', [
'cache' => $cache,
'server' => self::formatWithBaseDirectory($base_path),
'domain' => rtrim(
self::cleanServerPath($urlFromhelper),
'/'
),
]);
}
}
/**
* Get Last Modification of File
*
* @param string $file_path
*
* @return int|false
*/
static private function getFiletime(?string $file_path = null)
{
return file_exists($file_path)
? "?v=" . filemtime($file_path)
: false;
}
}