fix: support loading both .env and .env.example with reliable env parser and APP_ENV support

This commit is contained in:
2026-09-08 13:26:16 +02:00
parent f22417e6e3
commit ec233f3f9c
4 changed files with 91 additions and 36 deletions
+3
View File
@@ -5,6 +5,9 @@ DB_NAME="focizdev"
DB_USER="focizdev_user" DB_USER="focizdev_user"
DB_PASS="F0c1z_Dev_Secure_2026!" DB_PASS="F0c1z_Dev_Secure_2026!"
# Omgeving: 'development' (knippert rood) of 'live' (knippert groen)
APP_ENV="development"
# Applicatie URL (voor uitnodigingslinks) # Applicatie URL (voor uitnodigingslinks)
APP_URL="https://dev.fociz.nl" APP_URL="https://dev.fociz.nl"
+15
View File
@@ -0,0 +1,15 @@
# MariaDB Verbinding
DB_HOST="127.0.0.1"
DB_PORT="3306"
DB_NAME="fociz"
DB_USER="root"
DB_PASS=""
# Omgeving: 'development' (knippert rood) of 'live' (knippert groen)
APP_ENV="development"
# Applicatie URL (voor uitnodigingslinks)
APP_URL="https://uw-domein.nl"
# Applicatie Geheime Sleutel (willekeurige tekenreeks)
APP_SECRET="fociz-maria-db-super-secret-key-998877"
+56 -14
View File
@@ -1,40 +1,82 @@
<?php <?php
/** /**
* Fociz Applicatie- en Databaseconfiguratie * Fociz Applicatie- en Databaseconfiguratie
* Laadt automatisch .env (of .env.example indien .env niet bestaat)
*/ */
// Automatisch eenvoudige .env ondersteuning indien aanwezig $envVars = [];
$envFile = __DIR__ . '/../.env';
// 1. Zoek naar .env of val terug op .env.example
$envCandidates = [
__DIR__ . '/../.env',
__DIR__ . '/../.env.example',
];
foreach ($envCandidates as $envFile) {
if (file_exists($envFile)) { if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines !== false) {
foreach ($lines as $line) { foreach ($lines as $line) {
if (str_starts_with(trim($line), '#')) continue; $line = trim($line);
// Negeer lege regels en commentaarregels (# of //)
if ($line === '' || str_starts_with($line, '#') || str_starts_with($line, '//')) {
continue;
}
if (strpos($line, '=') !== false) { if (strpos($line, '=') !== false) {
list($name, $value) = explode('=', $line, 2); list($name, $value) = explode('=', $line, 2);
$name = trim($name); $name = trim($name);
$value = trim($value);
// Verwijder omlijstende aanhalingstekens (" of ')
$value = trim($value, " \t\n\r\0\x0B\"'"); $value = trim($value, " \t\n\r\0\x0B\"'");
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value)); // Sla op in array en zet in globale PHP variabelen
$envVars[$name] = $value;
$_ENV[$name] = $value; $_ENV[$name] = $value;
$_SERVER[$name] = $value; $_SERVER[$name] = $value;
@putenv("{$name}={$value}");
} }
} }
} }
break; // Stop zodra we het eerste geldige bestand hebben ingeladen
}
} }
// MariaDB Verbindingsgegevens (pas deze aan naar uw server instellingen of gebruik .env) /**
define('DB_HOST', getenv('DB_HOST') ?: '127.0.0.1'); * Veilige helper om een omgevingsvariabele op te vragen
define('DB_PORT', getenv('DB_PORT') ?: '3306'); */
define('DB_NAME', getenv('DB_NAME') ?: 'focizdev'); if (!function_exists('fociz_env')) {
define('DB_USER', getenv('DB_USER') ?: 'focizdev_user'); function fociz_env(string $key, $default = null) {
define('DB_PASS', getenv('DB_PASS') !== false ? getenv('DB_PASS') : 'F0c1z_Dev_Secure_2026!'); global $envVars;
if (isset($envVars[$key]) && $envVars[$key] !== '') {
return $envVars[$key];
}
if (isset($_ENV[$key]) && $_ENV[$key] !== '') {
return $_ENV[$key];
}
if (isset($_SERVER[$key]) && $_SERVER[$key] !== '') {
return $_SERVER[$key];
}
$val = @getenv($key);
if ($val !== false && $val !== '') {
return $val;
}
return $default;
}
}
// MariaDB Verbindingsgegevens (uit .env of .env.example)
define('DB_HOST', fociz_env('DB_HOST', '127.0.0.1'));
define('DB_PORT', fociz_env('DB_PORT', '3306'));
define('DB_NAME', fociz_env('DB_NAME', 'fociz'));
define('DB_USER', fociz_env('DB_USER', 'root'));
define('DB_PASS', fociz_env('DB_PASS', ''));
define('DB_CHARSET', 'utf8mb4'); define('DB_CHARSET', 'utf8mb4');
// Applicatie instellingen // Applicatie instellingen
define('APP_NAME', 'Fociz'); define('APP_NAME', 'Fociz');
define('APP_URL', getenv('APP_URL') ?: ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . ($_SERVER['HTTP_HOST'] ?? 'localhost'))); define('APP_URL', fociz_env('APP_URL', ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . ($_SERVER['HTTP_HOST'] ?? 'localhost'))));
define('APP_SECRET', getenv('APP_SECRET') ?: 'fociz-maria-db-super-secret-key-998877'); define('APP_SECRET', fociz_env('APP_SECRET', 'fociz-maria-db-super-secret-key-998877'));
define('APP_ENV', fociz_env('APP_ENV', 'development')); // 'development' of 'live'
// Tijdzone // Tijdzone
date_default_timezone_set('Europe/Amsterdam'); date_default_timezone_set('Europe/Amsterdam');
+8 -13
View File
@@ -9,23 +9,19 @@
* - OP DE DEV OMGEVING: $environment = 'development'; (Rood met 'Development' + radar ping) * - OP DE DEV OMGEVING: $environment = 'development'; (Rood met 'Development' + radar ping)
* - OP DE MAIN OMGEVING: $environment = 'live'; (Groen met 'Live' + radar ping) * - OP DE MAIN OMGEVING: $environment = 'live'; (Groen met 'Live' + radar ping)
* *
* U kunt de waarde hieronder aanpassen, of instellen via APP_ENV in .env. * U kunt dit hieronder aanpassen, of instellen via APP_ENV in .env / .env.example!
*/ */
// Bepaal de omgeving: 'development' of 'live' (of 'main') require_once __DIR__ . '/../config/config.php';
$environment = 'development'; // <-- ZET DIT OP DE MAIN SERVER OP 'live'
// Optionele fallback als het in .env gedefinieerd is // Standaard waarde (wordt overschreven als APP_ENV in .env staat)
if (getenv('APP_ENV')) { $environment = defined('APP_ENV') ? APP_ENV : 'development';
$envVal = strtolower(trim(getenv('APP_ENV')));
if (in_array($envVal, ['live', 'production', 'prod', 'main'])) { if (function_exists('fociz_env') && fociz_env('APP_ENV')) {
$environment = 'live'; $environment = fociz_env('APP_ENV');
} elseif (in_array($envVal, ['dev', 'development', 'staging', 'test'])) {
$environment = 'development';
}
} }
$isLive = in_array(strtolower($environment), ['live', 'production', 'prod', 'main']); $isLive = in_array(strtolower(trim($environment)), ['live', 'production', 'prod', 'main']);
?> ?>
<!-- FLASHY OMGEVINGSINDICATOR LINKS BOVENIN --> <!-- FLASHY OMGEVINGSINDICATOR LINKS BOVENIN -->
@@ -66,4 +62,3 @@ $isLive = in_array(strtolower($environment), ['live', 'production', 'prod', 'mai
</div> </div>
<?php endif; ?> <?php endif; ?>
</div> </div>