Initial commit: Slim Framework 4 API Retribusi dengan modular architecture
This commit is contained in:
41
src/Config/AppConfig.php
Normal file
41
src/Config/AppConfig.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
class AppConfig
|
||||
{
|
||||
/**
|
||||
* Load environment variables from .env file
|
||||
* Safe if .env file doesn't exist
|
||||
*
|
||||
* @param string $rootPath
|
||||
* @return void
|
||||
*/
|
||||
public static function loadEnv(string $rootPath): void
|
||||
{
|
||||
$envPath = $rootPath . DIRECTORY_SEPARATOR . '.env';
|
||||
|
||||
if (file_exists($envPath)) {
|
||||
$dotenv = Dotenv::createImmutable($rootPath);
|
||||
$dotenv->load();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variable with default value
|
||||
*
|
||||
* @param string $key
|
||||
* @param string|null $default
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get(string $key, ?string $default = null): ?string
|
||||
{
|
||||
$value = $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key);
|
||||
return $value !== false ? (string) $value : $default;
|
||||
}
|
||||
}
|
||||
|
||||
28
src/Config/app.php
Normal file
28
src/Config/app.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
class AppConfig
|
||||
{
|
||||
/**
|
||||
* Load environment variables from .env file
|
||||
* Safe if .env file doesn't exist
|
||||
*
|
||||
* @param string $rootPath
|
||||
* @return void
|
||||
*/
|
||||
public static function loadEnv(string $rootPath): void
|
||||
{
|
||||
$envPath = $rootPath . DIRECTORY_SEPARATOR . '.env';
|
||||
|
||||
if (file_exists($envPath)) {
|
||||
$dotenv = Dotenv::createImmutable($rootPath);
|
||||
$dotenv->load();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user