46 lines
1.6 KiB
PHP
46 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Loads .env from the project root and defines DB_* constants.
|
|
*
|
|
* Running via PHP dev server (php -S localhost:8000 from web/):
|
|
* The DB container port is exposed on localhost:3306, so DB_HOST resolves
|
|
* to 'localhost' automatically when not inside Docker.
|
|
*
|
|
* Running via Docker Compose (docker compose up):
|
|
* DB_HOST stays as 'db' (the service name) from the .env file.
|
|
*/
|
|
|
|
// Parse .env from project root (one level above web/)
|
|
$envFile = __DIR__ . '/../.env';
|
|
if (is_readable($envFile)) {
|
|
foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || $line[0] === '#' || !str_contains($line, '=')) {
|
|
continue;
|
|
}
|
|
[$key, $val] = explode('=', $line, 2);
|
|
$_ENV[trim($key)] = trim($val);
|
|
}
|
|
}
|
|
|
|
// When running the PHP built-in dev server on the host, 'db' won't resolve —
|
|
// fall back to localhost (the container port is exposed via docker-compose).
|
|
$host = $_ENV['MYSQL_HOST'] ?? 'localhost';
|
|
if ($host === 'db' && !file_exists('/.dockerenv')) {
|
|
$host = 'localhost';
|
|
}
|
|
|
|
define('DB_HOST', $host);
|
|
define('DB_NAME', $_ENV['MYSQL_DATABASE'] ?? 'imf_sms');
|
|
define('DB_USER', $_ENV['MYSQL_USER'] ?? 'imf_user');
|
|
define('DB_PASS', $_ENV['MYSQL_PASSWORD'] ?? '');
|
|
|
|
// SMS worker URL — falls back to localhost when running outside Docker.
|
|
$workerUrl = $_ENV['WORKER_URL'] ?? 'http://worker:5000';
|
|
if (str_contains($workerUrl, '//worker') && !file_exists('/.dockerenv')) {
|
|
$workerUrl = 'http://localhost:5000';
|
|
}
|
|
define('WORKER_URL', $workerUrl);
|