Files
dockerfluxodecaixapro/app/Helpers/Environment.php
T
2026-09-01 10:46:29 -03:00

173 lines
4.8 KiB
PHP
Executable File

<?php
/**
* SysCMS - Content Management System
*
* Copyright (c) 2025 Marco Costa
* All rights reserved.
*
* Author: Marco Costa
* Email: contato@syspanel.com.br
* Project: SysCMS
*
* This software is licensed, not sold.
* Permission is granted to use this software
* subject to the terms of the SysCMS Commercial License.
*
* Redistribution, sublicensing, resale, or
* disclosure of this software, in whole or in part,
* is strictly prohibited without prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*/
use Dotenv\Dotenv;
if (!function_exists('load_env')) {
/**
* Loads environment variables from the .env file
*
* @param string|null $path Path to the project root directory
* @return void
*/
function load_env(?string $path = null): void
{
static $loaded = false;
if ($loaded) {
return;
}
$path = $path ?? dirname(__DIR__, 2);
if (file_exists($path . '/.env')) {
$dotenv = Dotenv::createImmutable($path);
$dotenv->load();
$loaded = true;
}
}
}
if (!function_exists('get_env')) {
/**
* Gets an environment variable with a default value
*
* @param string $key Variable name
* @param mixed $default Default value if not exists
* @return mixed
*/
function get_env(string $key, $default = null)
{
return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
}
}
if (!function_exists('env')) {
/**
* Alias for get_env to maintain compatibility
*
* @param string $key Variable name
* @param mixed $default Default value if not exists
* @return mixed
*/
function env(string $key, $default = null)
{
return get_env($key, $default);
}
}
if (!function_exists('env_bool')) {
/**
* Gets an environment variable as boolean
*
* @param string $key Variable name
* @param bool $default Default value if not exists
* @return bool
*/
function env_bool(string $key, bool $default = false): bool
{
$value = get_env($key, $default);
if (is_bool($value)) {
return $value;
}
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}
if (!function_exists('env_int')) {
/**
* Gets an environment variable as integer
*
* @param string $key Variable name
* @param int $default Default value if not exists
* @return int
*/
function env_int(string $key, int $default = 0): int
{
return (int) get_env($key, $default);
}
}
if (!function_exists('get_db_config')) {
/**
* Gets database configuration from environment variables
*
* @return array
*/
function get_db_config(): array
{
return [
'driver' => get_env('DB_DRIVER', get_env('DB_CONNECTION', 'mysql')),
'host' => get_env('DB_HOST', 'localhost'),
'port' => get_env('DB_PORT', '3306'),
'database' => get_env('DB_DATABASE', 'syscms'),
'username' => get_env('DB_USERNAME', 'root'),
'password' => get_env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
];
}
}
if (!function_exists('get_app_config')) {
/**
* Gets application configuration from environment variables
*
* @return array
*/
function get_app_config(): array
{
return [
'name' => get_env('APP_NAME', 'SysCMS'),
'env' => get_env('APP_ENV', 'development'),
'debug' => env_bool('APP_DEBUG', true),
'url' => get_env('APP_URL', 'http://localhost:16240'),
'timezone' => get_env('APP_TIMEZONE', 'America/Sao_Paulo'),
'key' => get_env('APP_KEY', ''),
];
}
}
if (!function_exists('get_mail_config')) {
/**
* Gets email configuration from environment variables
*
* @return array
*/
function get_mail_config(): array
{
return [
'transport' => get_env('MAIL_TRANSPORT', 'smtp'),
'host' => get_env('MAIL_HOST', 'localhost'),
'port' => get_env('MAIL_PORT', '587'),
'username' => get_env('MAIL_USERNAME', ''),
'password' => get_env('MAIL_PASSWORD', ''),
'encryption' => get_env('MAIL_ENCRYPTION', 'tls'),
'from_address' => get_env('MAIL_FROM_ADDRESS', ''),
'from_name' => get_env('MAIL_FROM_NAME', 'SysCMS'),
];
}
}