This commit is contained in:
Gregory Letellier
2023-11-08 16:59:13 +01:00
commit 80aeacdade
19 changed files with 5673 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Core\Kernel;
use App\Core\Routing\Router;
use Dotenv\Dotenv;
use Kletellier\PdoWrapper\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Application
{
protected Router $router;
protected $_whoops;
public function __construct()
{
// enable error reporting
$this->setReporting();
try {
$dotenv = Dotenv::createImmutable(ROOT);
$dotenv->load();
} catch (\Throwable $th) {
e($th->getMessage());
die();
}
if ($this->isDebug()) {
$this->_whoops = new \Whoops\Run();
$handler = new \Whoops\Handler\PrettyPageHandler();
$this->_whoops->pushHandler($handler);
$this->_whoops->register();
}
$db_config = $this->getDatabaseConfig();
if (!empty($db_config)) {
// Initialize DB system
Connection::init($db_config, array_keys($db_config));
}
// Init router
$this->router = new Router();
}
private function isDebug(): bool
{
$ret = true;
if (isset($_ENV["DEBUG"])) {
$ret = ($_ENV["DEBUG"] == "true");
}
return $ret;
}
private function getDatabaseConfig(): array
{
$ret = [];
$connections = isset($_ENV["CONNECTIONS"]) ? explode(",", $_ENV["CONNECTIONS"]) : [];
foreach ($connections as $connection) {
if ($connection !== "") {
$conn = [];
$key = (strtoupper(trim($connection)));
$conn["TYPE"] = isset($_ENV[$this->getKey("TYPE", $key)]) ? $_ENV[$this->getKey("TYPE", $key)] : null;
$conn["USER"] = isset($_ENV[$this->getKey("USER", $key)]) ? $_ENV[$this->getKey("USER", $key)] : null;
$conn["HOST"] = isset($_ENV[$this->getKey("HOST", $key)]) ? $_ENV[$this->getKey("HOST", $key)] : null;
$conn["PASSWORD"] = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null;
$conn["DATABASE"] = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null;
$conn["PORT"] = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null;
$ret[$connection] = $conn;
}
}
return $ret;
}
private function getKey(string $type, string $name): string
{
return "DB_" . $name . "_" . $type;
}
/**
* Enable/Disable error reporting to output buffer
*/
private function setReporting()
{
error_reporting(E_ALL);
ini_set('display_errors', 'On');
}
public function handle()
{
ob_start(null, 0, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE);
try {
$this->router->run();
} catch (\Exception $ex) {
e($ex->getMessage());
die();
}
}
}