Files
Woles-Framework/app/Core/helpers.php

201 lines
4.8 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* NovaCore Framework Core Helper Functions
* Additional utility functions
*/
if (!function_exists('storage_path')) {
/**
* Get storage path
*/
function storage_path(string $path = ''): string
{
$storagePath = __DIR__ . '/../../storage';
return $path ? $storagePath . '/' . ltrim($path, '/') : $storagePath;
}
}
if (!function_exists('database_path')) {
/**
* Get database path
*/
function database_path(string $path = ''): string
{
$dbPath = __DIR__ . '/../../database';
return $path ? $dbPath . '/' . ltrim($path, '/') : $dbPath;
}
}
if (!function_exists('base_path')) {
/**
* Get base path
*/
function base_path(string $path = ''): string
{
$basePath = __DIR__ . '/../..';
return $path ? $basePath . '/' . ltrim($path, '/') : $basePath;
}
}
if (!function_exists('app_path')) {
/**
* Get app path
*/
function app_path(string $path = ''): string
{
$appPath = __DIR__ . '/..';
return $path ? $appPath . '/' . ltrim($path, '/') : $appPath;
}
}
if (!function_exists('public_path')) {
/**
* Get public path
*/
function public_path(string $path = ''): string
{
$publicPath = __DIR__ . '/../../public';
return $path ? $publicPath . '/' . ltrim($path, '/') : $publicPath;
}
}
if (!function_exists('config_path')) {
/**
* Get config path
*/
function config_path(string $path = ''): string
{
$configPath = __DIR__ . '/../Config';
return $path ? $configPath . '/' . ltrim($path, '/') : $configPath;
}
}
if (!function_exists('is_production')) {
/**
* Check if running in production
*/
function is_production(): bool
{
return env('APP_ENV', 'production') === 'production';
}
}
if (!function_exists('is_development')) {
/**
* Check if running in development
*/
function is_development(): bool
{
return env('APP_ENV', 'production') === 'development';
}
}
if (!function_exists('is_testing')) {
/**
* Check if running in testing
*/
function is_testing(): bool
{
return env('APP_ENV', 'production') === 'testing';
}
}
if (!function_exists('abort')) {
/**
* Abort with error code
*/
function abort(int $code, string $message = ''): void
{
http_response_code($code);
if ($message) {
echo "<h1>{$code} - Error</h1>";
echo "<p>{$message}</p>";
} else {
switch ($code) {
case 404:
echo "<h1>404 - Not Found</h1>";
echo "<p>The requested page could not be found.</p>";
break;
case 500:
echo "<h1>500 - Internal Server Error</h1>";
echo "<p>Something went wrong on our end.</p>";
break;
default:
echo "<h1>{$code} - Error</h1>";
break;
}
}
exit;
}
}
if (!function_exists('logger')) {
/**
* Log message
*/
function logger(string $message, string $level = 'info'): void
{
$logFile = storage_path('logs/error.log');
$timestamp = date('Y-m-d H:i:s');
$logMessage = "[{$timestamp}] [{$level}] {$message}" . PHP_EOL;
file_put_contents($logFile, $logMessage, FILE_APPEND | LOCK_EX);
}
}
if (!function_exists('cache')) {
/**
* Simple cache helper
*/
function cache(string $key, $value = null, int $ttl = 3600)
{
$cacheFile = storage_path("cache/{$key}.cache");
if ($value === null) {
// Get from cache
if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $ttl) {
return unserialize(file_get_contents($cacheFile));
}
return null;
} else {
// Set cache
$cacheDir = dirname($cacheFile);
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
file_put_contents($cacheFile, serialize($value));
return $value;
}
}
}
if (!function_exists('cache_forget')) {
/**
* Remove from cache
*/
function cache_forget(string $key): bool
{
$cacheFile = storage_path("cache/{$key}.cache");
return file_exists($cacheFile) ? unlink($cacheFile) : false;
}
}
if (!function_exists('cache_flush')) {
/**
* Clear all cache
*/
function cache_flush(): void
{
$cacheDir = storage_path('cache');
if (is_dir($cacheDir)) {
$files = glob($cacheDir . '/*.cache');
foreach ($files as $file) {
unlink($file);
}
}
}
}