102 lines
3.1 KiB
PHP
102 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Kletellier\MiniWeb\Core\Kernel;
|
|
|
|
use Kletellier\MiniWeb\Core\Routing\Router;
|
|
use Dotenv\Dotenv;
|
|
use Kletellier\PdoWrapper\Connection;
|
|
|
|
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;
|
|
if ($conn["TYPE"] == "sqlite") {
|
|
if (!is_file($conn["DATABASE"])) {
|
|
$conn["DATABASE"] = ROOT . DS . "databases" . DS . $conn["DATABASE"];
|
|
}
|
|
}
|
|
$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()
|
|
{
|
|
try {
|
|
$this->router->run();
|
|
} catch (\Exception $ex) {
|
|
e($ex->getMessage());
|
|
die();
|
|
}
|
|
}
|
|
}
|