Files
Woles-Framework/app/helpers.php
mwpn 0b42271bfe feat: Complete Woles Framework v1.0 with enterprise-grade UI
- Add comprehensive error handling system with custom error pages
- Implement professional enterprise-style design with Tailwind CSS
- Create modular HMVC architecture with clean separation of concerns
- Add security features: CSRF protection, XSS filtering, Argon2ID hashing
- Include CLI tools for development workflow
- Add error reporting dashboard with system monitoring
- Implement responsive design with consistent slate color scheme
- Replace all emoji icons with professional SVG icons
- Add comprehensive test suite with PHPUnit
- Include database migrations and seeders
- Add proper exception handling with fallback pages
- Implement template engine with custom syntax support
- Add helper functions and facades for clean code
- Include proper logging and debugging capabilities
2025-10-11 07:08:23 +07:00

286 lines
5.7 KiB
PHP

<?php
/**
* NovaCore Framework Helper Functions
* Global utility functions
*/
// Include core helpers
require_once __DIR__ . '/Core/helpers.php';
if (!function_exists('app')) {
/**
* Get service from container
*/
function app(?string $name = null)
{
// Use a global shared container instance
if (!isset($GLOBALS['__woles_container']) || !$GLOBALS['__woles_container']) {
$GLOBALS['__woles_container'] = new App\Core\Container();
}
$container = $GLOBALS['__woles_container'];
if ($name === null) {
return $container;
}
return $container->get($name);
}
}
if (!function_exists('app_set_container')) {
/**
* Set global container instance
*/
function app_set_container(object $container): void
{
$GLOBALS['__woles_container'] = $container;
}
}
if (!function_exists('request')) {
/**
* Get request instance
*/
function request(): App\Core\Request
{
return app('request');
}
}
if (!function_exists('response')) {
/**
* Get response instance
*/
function response(): App\Core\Response
{
return app('response');
}
}
if (!function_exists('view')) {
/**
* Render a view
*/
function view(string $view, array $data = []): string
{
return app('view')->render($view, $data);
}
}
if (!function_exists('redirect')) {
/**
* Redirect to URL
*/
function redirect(string $url, int $status = 302): void
{
response()->redirect($url, $status);
}
}
if (!function_exists('env')) {
/**
* Get environment variable
*/
function env(string $key, $default = null)
{
$value = getenv($key);
return $value !== false ? $value : $default;
}
}
if (!function_exists('config')) {
/**
* Get configuration value
*/
function config(string $key, $default = null)
{
static $config = [];
if (empty($config)) {
$configFile = __DIR__ . '/Config/app.php';
if (file_exists($configFile)) {
$config = include $configFile;
}
}
$keys = explode('.', $key);
$value = $config;
foreach ($keys as $k) {
if (is_array($value) && isset($value[$k])) {
$value = $value[$k];
} else {
return $default;
}
}
return $value;
}
}
if (!function_exists('csrf_token')) {
/**
* Generate CSRF token
*/
function csrf_token(): string
{
return app('security')->generateCsrfToken();
}
}
if (!function_exists('csrf_field')) {
/**
* Generate CSRF hidden field
*/
function csrf_field(): string
{
return '<input type="hidden" name="_token" value="' . csrf_token() . '">';
}
}
if (!function_exists('method_field')) {
/**
* Generate method field for forms
*/
function method_field(string $method): string
{
return '<input type="hidden" name="_method" value="' . strtoupper($method) . '">';
}
}
if (!function_exists('asset')) {
/**
* Generate asset URL
*/
function asset(string $path): string
{
$baseUrl = env('APP_URL', 'http://localhost:8000');
return rtrim($baseUrl, '/') . '/public/' . ltrim($path, '/');
}
}
if (!function_exists('url')) {
/**
* Generate URL
*/
function url(string $path = ''): string
{
$baseUrl = env('APP_URL', 'http://localhost:8000');
return rtrim($baseUrl, '/') . '/' . ltrim($path, '/');
}
}
if (!function_exists('route')) {
/**
* Generate route URL (placeholder for now)
*/
function route(string $name, array $params = []): string
{
// This would be implemented with a proper route name system
return url($name);
}
}
if (!function_exists('dd')) {
/**
* Dump and die
*/
function dd(...$vars): void
{
foreach ($vars as $var) {
echo '<pre>';
var_dump($var);
echo '</pre>';
}
die();
}
}
if (!function_exists('dump')) {
/**
* Dump variable
*/
function dump(...$vars): void
{
foreach ($vars as $var) {
echo '<pre>';
var_dump($var);
echo '</pre>';
}
}
}
if (!function_exists('old')) {
/**
* Get old input value
*/
function old(string $key, $default = null)
{
return $_SESSION['_old_input'][$key] ?? $default;
}
}
if (!function_exists('flash')) {
/**
* Set flash message
*/
function flash(string $key, $message): void
{
$_SESSION['_flash'][$key] = $message;
}
}
if (!function_exists('flash_get')) {
/**
* Get and remove flash message
*/
function flash_get(string $key)
{
$message = $_SESSION['_flash'][$key] ?? null;
unset($_SESSION['_flash'][$key]);
return $message;
}
}
if (!function_exists('e')) {
/**
* Escape HTML
*/
function e(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
}
if (!function_exists('str_random')) {
/**
* Generate random string
*/
function str_random(int $length = 32): string
{
return app('security')->generateRandomString($length);
}
}
if (!function_exists('bcrypt')) {
/**
* Hash password
*/
function bcrypt(string $password): string
{
return app('security')->hashPassword($password);
}
}
if (!function_exists('verify_password')) {
/**
* Verify password
*/
function verify_password(string $password, string $hash): bool
{
return app('security')->verifyPassword($password, $hash);
}
}