Env loader

This commit is contained in:
Maarten 2024-11-25 20:23:45 +01:00
parent afa8a341bd
commit 2a4a422722
7 changed files with 464 additions and 11 deletions

63
src/Env/Env.php Normal file
View file

@ -0,0 +1,63 @@
<?php
namespace Core\Env;
use Dotenv\Dotenv;
class Env
{
private static Dotenv $dotenv;
/**
* Load env
*
* @return void
*/
public static function load(): void
{
$dotenv = Dotenv::createImmutable('../');
$dotenv->load();
self::$dotenv = $dotenv;
}
/**
* Get key value
*
* @param string $key
* @return mixed
*/
public static function get(string $key): mixed
{
// Parse key
$key = strtoupper($key);
// Check if key exists
if (!self::$dotenv->ifPresent($key)) {
return false;
}
// Get value
$result = $_ENV[$key];
// Parse booleans
try {
if(self::$dotenv->required($key)->isBoolean()) {
return filter_var($result, FILTER_VALIDATE_BOOLEAN);
}
} catch (\Exception) {
// do nothing
}
// Parse integers
try {
if(self::$dotenv->required($key)->isInteger()) {
return (int) $result;
}
} catch (\Exception) {
// do nothing
}
return $result;
}
}

View file

@ -66,14 +66,13 @@ class Router
*/
public static function dispatch(): void
{
// Capture all exceptions
Exceptions::catch();
// Init request
$request = new Request();
$url = $request->url();
$method = $request->method();
$url = $request->url();
$method = $request->method();
// Capture all exceptions
Exceptions::catch($request);
try {
if (array_key_exists($url, self::$routes[$method])) {