Initial commit
This commit is contained in:
51
app/Core/Config.php
Normal file
51
app/Core/Config.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class Config {
|
||||
private static $instance = null;
|
||||
private array $settings = [];
|
||||
|
||||
private function __construct() {
|
||||
// Map environment variables to internal keys with optional defaults
|
||||
$this->settings = [
|
||||
'db' => [
|
||||
'host' => $_ENV['DB_HOST'] ?? 'localhost',
|
||||
'name' => $_ENV['DB_NAME'] ?? 'my_app',
|
||||
'user' => $_ENV['DB_USER'] ?? 'root',
|
||||
'pass' => $_ENV['DB_PASS'] ?? '',
|
||||
],
|
||||
'app' => [
|
||||
'name' => $_ENV['APP_NAME'] ?? 'Vanilla PHP App',
|
||||
'env' => $_ENV['APP_ENV'] ?? 'production',
|
||||
'debug' => ($_ENV['APP_DEBUG'] ?? 'false') === 'true',
|
||||
'url' => $_ENV['APP_URL'] ?? 'http://localhost',
|
||||
],
|
||||
'mail' => [
|
||||
'host' => $_ENV['MAIL_HOST'] ?? 'smtp.mailtrap.io',
|
||||
'port' => $_ENV['MAIL_PORT'] ?? 2525,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function get(string $key, $default = null) {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
// Support dot notation (e.g., 'db.host')
|
||||
$keys = explode('.', $key);
|
||||
$value = self::$instance->settings;
|
||||
|
||||
foreach ($keys as $k) {
|
||||
if (!isset($value[$k])) {
|
||||
return $default;
|
||||
}
|
||||
$value = $value[$k];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user