update config
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
DB_HOST=
|
||||||
|
DB_USER=root
|
||||||
|
DB_PASS=
|
||||||
|
DB_NAME=webgis_class
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_CHARSET=utf8mb4
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
.env
|
.env
|
||||||
.env.*
|
|
||||||
api/config/database.local.php
|
api/config/database.local.php
|
||||||
*.sql
|
*.sql
|
||||||
!database.sql
|
!database.sql
|
||||||
|
|||||||
+55
-2
@@ -3,16 +3,68 @@
|
|||||||
|
|
||||||
mysqli_report(MYSQLI_REPORT_OFF);
|
mysqli_report(MYSQLI_REPORT_OFF);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse and load a .env file into the environment.
|
||||||
|
* Supports KEY=VALUE format, ignores comments (#) and empty lines.
|
||||||
|
*/
|
||||||
|
if (!function_exists('loadEnvFile')) {
|
||||||
|
function loadEnvFile(string $filePath): void {
|
||||||
|
if (!is_file($filePath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
$line = trim($line);
|
||||||
|
|
||||||
|
// Skip comments and empty lines
|
||||||
|
if ($line === '' || str_starts_with($line, '#')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split on first '=' only
|
||||||
|
$eqPos = strpos($line, '=');
|
||||||
|
if ($eqPos === false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = trim(substr($line, 0, $eqPos));
|
||||||
|
$value = trim(substr($line, $eqPos + 1));
|
||||||
|
|
||||||
|
// Strip surrounding quotes if present
|
||||||
|
if (
|
||||||
|
strlen($value) >= 2 &&
|
||||||
|
(($value[0] === '"' && $value[-1] === '"') ||
|
||||||
|
($value[0] === "'" && $value[-1] === "'"))
|
||||||
|
) {
|
||||||
|
$value = substr($value, 1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only set if not already defined by the server/environment
|
||||||
|
if ($key !== '' && getenv($key) === false) {
|
||||||
|
putenv("$key=$value");
|
||||||
|
$_ENV[$key] = $value;
|
||||||
|
$_SERVER[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load .env from project root (two levels up from this file)
|
||||||
|
loadEnvFile(dirname(__DIR__, 2) . '/.env');
|
||||||
|
|
||||||
if (!function_exists('loadDatabaseConfig')) {
|
if (!function_exists('loadDatabaseConfig')) {
|
||||||
function loadDatabaseConfig(): array {
|
function loadDatabaseConfig(): array {
|
||||||
$config = [
|
$config = [
|
||||||
'host' => getenv('DB_HOST') ?: 'localhost',
|
'host' => getenv('DB_HOST') ?: 'localhost',
|
||||||
'user' => getenv('DB_USER') ?: 'root',
|
'user' => getenv('DB_USER') ?: 'root',
|
||||||
'pass' => getenv('DB_PASS') ?: '',
|
'pass' => getenv('DB_PASS') ?: '',
|
||||||
'name' => getenv('DB_NAME') ?: 'webgis',
|
'name' => getenv('DB_NAME') ?: 'webgis_class',
|
||||||
|
'port' => (int)(getenv('DB_PORT') ?: 3306),
|
||||||
'charset' => getenv('DB_CHARSET') ?: 'utf8mb4',
|
'charset' => getenv('DB_CHARSET') ?: 'utf8mb4',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Allow local override file for development (not committed to git)
|
||||||
$localConfigFile = __DIR__ . '/database.local.php';
|
$localConfigFile = __DIR__ . '/database.local.php';
|
||||||
if (is_file($localConfigFile)) {
|
if (is_file($localConfigFile)) {
|
||||||
$localConfig = require $localConfigFile;
|
$localConfig = require $localConfigFile;
|
||||||
@@ -31,12 +83,13 @@ define('DB_HOST', $dbConfig['host']);
|
|||||||
define('DB_USER', $dbConfig['user']);
|
define('DB_USER', $dbConfig['user']);
|
||||||
define('DB_PASS', $dbConfig['pass']);
|
define('DB_PASS', $dbConfig['pass']);
|
||||||
define('DB_NAME', $dbConfig['name']);
|
define('DB_NAME', $dbConfig['name']);
|
||||||
|
define('DB_PORT', $dbConfig['port']);
|
||||||
define('DB_CHARSET', $dbConfig['charset']);
|
define('DB_CHARSET', $dbConfig['charset']);
|
||||||
|
|
||||||
function getDB(): mysqli {
|
function getDB(): mysqli {
|
||||||
static $conn = null;
|
static $conn = null;
|
||||||
if ($conn === null) {
|
if ($conn === null) {
|
||||||
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
|
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
|
||||||
if ($conn->connect_error) {
|
if ($conn->connect_error) {
|
||||||
error_log('DB connection failed: ' . $conn->connect_error);
|
error_log('DB connection failed: ' . $conn->connect_error);
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
|
|||||||
Reference in New Issue
Block a user