Env loader
This commit is contained in:
parent
afa8a341bd
commit
2a4a422722
7 changed files with 464 additions and 11 deletions
63
src/Env/Env.php
Normal file
63
src/Env/Env.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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])) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue