@@ -16,14 +16,10 @@ class Asset{
1616 * Create assets Real path url
1717 *
1818 * @param string $asset
19- * - asset file e.g (style.css | js/main.js)
20- *
2119 * @param bool|null $cache
22- * @param bool|null $path_type
23- *
24- * @return string
20+ * @param bool|null $type "absolute" | "relative" (default: false → absolute)
2521 */
26- public static function asset (? string $ asset = null , $ cache = null , $ path_type = null )
22+ public static function asset ($ asset = null , $ cache = null , $ type = null ): string
2723 {
2824 // if coniguration has not been used in the global space
2925 // then we call to define paths for us
@@ -34,16 +30,14 @@ public static function asset(?string $asset = null, $cache = null, $path_type =
3430 // asset path
3531 $ assetPath = ASSET_BASE_DIRECTORY ;
3632
37- // if asset method cache is not bool
38- // then we override the global configuration
33+ // Only override global config, when <cache> it's not boolean
3934 if (!is_bool ($ cache )){
4035 $ cache = $ assetPath ['cache ' ];
4136 }
4237
43- // if asset method path_type is not bool
44- // then we override the global configuration
45- if (!is_bool ($ path_type )){
46- $ path_type = $ assetPath ['path_type ' ];
38+ // Only override global config, when <type> it's not boolean
39+ if (!is_bool ($ type )){
40+ $ type = $ assetPath ['type ' ];
4741 }
4842
4943 // trim
@@ -58,59 +52,55 @@ public static function asset(?string $asset = null, $cache = null, $path_type =
5852 $ cacheTimeAppend = null ;
5953
6054 // cache allow from self method
61- if ($ cache ){
62- if (!empty ($ asset )){
63- $ cacheTimeAppend = self ::getFiletime ($ file_server ) ?? null ;
64- }
55+ if ($ cache && !empty ($ asset )){
56+ $ cacheTimeAppend = self ::getFiletime ($ file_server ) ?? null ;
6557 }
66-
67- // if `$path_type` is true, then we'll use relative path
68- if ($ path_type ){
69-
58+
59+ // Using <relative path> when true
60+ if ($ type === true ){
7061 // replace domain path
7162 $ domain = Str::replace ($ assetPath ['removeDomain ' ], '' , $ file_domain );
7263 $ domain = ltrim ($ domain , '/ ' );
7364
7465 return "/ {$ domain }{$ cacheTimeAppend }" ;
7566 }
7667
77- // Using absolute path
68+ // Using < absolute path>
7869 return "{$ file_domain }{$ cacheTimeAppend }" ;
7970 }
8071
8172 /**
8273 * Configure Assets Default Directory
8374 *
84- * @param string $base_path
85- * - [optional] Default is `base_directory/assets`
86- * - If set and directory is not found, then we revert back to the default
87- *
88- * @param bool $cache
89- * - [optional] Default is true
90- * - End point of link `?v=xxxxxxxx` is with cache of file time change
91- * - This will automatically tells the broswer to fetch new file if the time change
92- * - Time will only change if you make changes or modify the request file
93- *
94- * @param string $path_type
95- * -[optional] Default is false[Absolute Path] | true[Relative path]
96- *
97- * @return void
75+ * @param string|null $path
76+ * @param bool $cache Whether to use cache-busting (default: true)
77+ * - End point of link `?v=xxxxxxxx` is with cache of file time chang
78+ * @param bool $type "absolute" | "relative" (default: false → absolute)
9879 */
99- public static function config (? string $ base_path = null , ? bool $ cache = false , $ path_type = false )
80+ public static function config ($ path = null , $ cache = false , $ type = false ): void
10081 {
10182 // if not defined
10283 if (!defined ('ASSET_BASE_DIRECTORY ' )){
10384 // url helper class
104- $ urlFromhelper = HttpRequest::url ();
85+ $ urlFromhelper = HttpRequest::host ();
86+
87+ // we don't care the configured url address
88+ // prepare a fallback of using combination of full url
89+ if (empty ($ urlFromhelper )){
90+ $ urlFromhelper = HttpRequest::url ();
91+ }
92+
93+ // clean http from url
94+ $ urlFromhelper = Str::replace (HttpRequest::http (), '' , $ urlFromhelper );
10595
10696 // if base path is set
107- if (!empty ($ base_path )){
97+ if (!empty ($ path )){
10898
10999 // - Trim forward slash from left and right
110- $ base_path = Str::trim ($ base_path , '/ ' );
100+ $ path = Str::trim ($ path , '/ ' );
111101
112102 // base for url path
113- $ baseForUrlPath = $ base_path ;
103+ $ baseForUrlPath = $ path ;
114104
115105 // check if accessed from default ip:address
116106 if (HttpRequest::isIpAccessedVia127Port ()){
@@ -123,10 +113,11 @@ public static function config(?string $base_path = null, ?bool $cache = false, $
123113
124114 define ('ASSET_BASE_DIRECTORY ' , [
125115 'cache ' => $ cache ,
126- 'path_type ' => $ path_type ,
127- 'server ' => self ::formatWithBaseDirectory ($ base_path ),
116+ 'type ' => $ type ,
117+ 'path ' => $ path ,
118+ 'server ' => self ::formatWithBaseDirectory ($ path ),
128119 'domain ' => rtrim (
129- self ::cleanServerPath ($ urlFromhelper ),
120+ self ::cleanServerPath (HttpRequest:: http () . $ urlFromhelper ),
130121 '/ '
131122 ),
132123 'removeDomain ' => HttpRequest::http () . HttpRequest::host ()
@@ -138,7 +129,6 @@ public static function config(?string $base_path = null, ?bool $cache = false, $
138129 * Get Last Modification of File
139130 *
140131 * @param string $file_path
141- *
142132 * @return int|false
143133 */
144134 private static function getFiletime (?string $ file_path = null )
0 commit comments