parent
57a78a4319
commit
b9e581b921
2 changed files with 230 additions and 1 deletions
@ -0,0 +1,229 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use Illuminate\Support\Str; |
||||||
|
use Illuminate\Support\Facades\Auth; |
||||||
|
use Illuminate\Support\Facades\Route; |
||||||
|
use Illuminate\Support\Facades\Storage; |
||||||
|
use Illuminate\Support\Facades\File; |
||||||
|
use Illuminate\Support\Facades\Cache; |
||||||
|
use Illuminate\Support\Facades\Log; |
||||||
|
use Illuminate\Support\Facades\Http; |
||||||
|
use Illuminate\Support\Carbon; |
||||||
|
|
||||||
|
/** |
||||||
|
* Format a number into currency format. |
||||||
|
*/ |
||||||
|
if (!function_exists('format_currency')) { |
||||||
|
function format_currency(float|int $amount, string $currency = 'VND'): string |
||||||
|
{ |
||||||
|
return number_format($amount, 0, ',', '.') . ' ' . strtoupper($currency); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get a human-readable difference between the current time and a given time. |
||||||
|
*/ |
||||||
|
if (!function_exists('human_time_diff')) { |
||||||
|
function human_time_diff($datetime): string |
||||||
|
{ |
||||||
|
return Carbon::parse($datetime)->diffForHumans(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check if the authenticated user is an admin. |
||||||
|
*/ |
||||||
|
if (!function_exists('is_admin')) { |
||||||
|
function is_admin(): bool |
||||||
|
{ |
||||||
|
return Auth::check() && Auth::user()->role === 'admin'; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Generate a versioned asset URL to prevent browser caching. |
||||||
|
*/ |
||||||
|
if (!function_exists('asset_versioned')) { |
||||||
|
function asset_versioned($path): string |
||||||
|
{ |
||||||
|
$version = config('app.asset_version', time()); |
||||||
|
return asset($path) . '?v=' . $version; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get a limited string excerpt with optional length. |
||||||
|
*/ |
||||||
|
if (!function_exists('str_excerpt')) { |
||||||
|
function str_excerpt(string $string, int $length = 100): string |
||||||
|
{ |
||||||
|
return Str::limit(strip_tags($string), $length); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Format a date using a given format (default: d/m/Y). |
||||||
|
*/ |
||||||
|
if (!function_exists('format_date')) { |
||||||
|
function format_date($date, string $format = 'd/m/Y'): string |
||||||
|
{ |
||||||
|
return Carbon::parse($date)->format($format); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Send a generic notification to a user. |
||||||
|
*/ |
||||||
|
if (!function_exists('send_notification')) { |
||||||
|
function send_notification(\App\Models\User $user, $message, $type = 'info') |
||||||
|
{ |
||||||
|
$user->notify(new \App\Notifications\GenericNotification($message, $type)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get an environment variable as a boolean. |
||||||
|
*/ |
||||||
|
if (!function_exists('env_bool')) { |
||||||
|
function env_bool(string $key, bool $default = false): bool |
||||||
|
{ |
||||||
|
return filter_var(env($key, $default), FILTER_VALIDATE_BOOLEAN); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check if the current route matches one or more given route names. |
||||||
|
*/ |
||||||
|
if (!function_exists('route_is')) { |
||||||
|
function route_is(string|array $names): bool |
||||||
|
{ |
||||||
|
return request()->routeIs($names); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Abort the request if the app is running in a specific environment. |
||||||
|
*/ |
||||||
|
if (!function_exists('abort_if_env')) { |
||||||
|
function abort_if_env(string $env, int $code = 403) |
||||||
|
{ |
||||||
|
if (app()->environment($env)) { |
||||||
|
abort($code, "Action not allowed in $env environment."); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return a CSS class if the current route matches one or more route names. |
||||||
|
*/ |
||||||
|
if (!function_exists('active_class')) { |
||||||
|
function active_class(string|array $routes, string $class = 'active'): string |
||||||
|
{ |
||||||
|
return request()->routeIs($routes) ? $class : ''; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Return a standard JSON response. |
||||||
|
*/ |
||||||
|
if (!function_exists('json_response')) { |
||||||
|
function json_response($data = [], string $message = '', int $code = 200) |
||||||
|
{ |
||||||
|
return response()->json([ |
||||||
|
'message' => $message, |
||||||
|
'data' => $data, |
||||||
|
], $code); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Generate a UUID (Universally Unique Identifier). |
||||||
|
*/ |
||||||
|
if (!function_exists('uuid')) { |
||||||
|
function uuid(): string |
||||||
|
{ |
||||||
|
return (string) Str::uuid(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Convert bytes into a human-readable file size format. |
||||||
|
*/ |
||||||
|
if (!function_exists('file_size_human')) { |
||||||
|
function file_size_human(int $bytes, int $precision = 2): string |
||||||
|
{ |
||||||
|
$units = ['B', 'KB', 'MB', 'GB', 'TB']; |
||||||
|
$bytes = max($bytes, 0); |
||||||
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
||||||
|
$pow = min($pow, count($units) - 1); |
||||||
|
$bytes /= (1 << (10 * $pow)); |
||||||
|
return round($bytes, $precision) . ' ' . $units[$pow]; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get a full URL for a file stored in Laravel's storage. |
||||||
|
*/ |
||||||
|
if (!function_exists('storage_url')) { |
||||||
|
function storage_url(string $path): string |
||||||
|
{ |
||||||
|
return Storage::url($path); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Log a debug message to the Laravel log. |
||||||
|
*/ |
||||||
|
if (!function_exists('log_debug')) { |
||||||
|
function log_debug(string $message, array $context = []) |
||||||
|
{ |
||||||
|
Log::debug($message, $context); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check if a string is valid JSON. |
||||||
|
*/ |
||||||
|
if (!function_exists('is_json')) { |
||||||
|
function is_json(string $string): bool |
||||||
|
{ |
||||||
|
json_decode($string); |
||||||
|
return json_last_error() === JSON_ERROR_NONE; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Remember cache for a given key and duration. |
||||||
|
*/ |
||||||
|
if (!function_exists('cache_remember')) { |
||||||
|
function cache_remember(string $key, int $minutes, Closure $callback) |
||||||
|
{ |
||||||
|
return Cache::remember($key, now()->addMinutes($minutes), $callback); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Perform a GET HTTP request and return the JSON response as array. |
||||||
|
*/ |
||||||
|
if (!function_exists('http_get_json')) { |
||||||
|
function http_get_json(string $url, array $headers = []): array |
||||||
|
{ |
||||||
|
try { |
||||||
|
$response = Http::withHeaders($headers)->get($url); |
||||||
|
return $response->json(); |
||||||
|
} catch (Exception $e) { |
||||||
|
Log::error("HTTP GET failed: " . $e->getMessage()); |
||||||
|
return []; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the authenticated user from a specific guard. |
||||||
|
*/ |
||||||
|
if (!function_exists('user_guard')) { |
||||||
|
function user_guard(string $guard = 'web') |
||||||
|
{ |
||||||
|
return Auth::guard($guard); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue