1 Commits
1.0.2 ... 1.0.3

Author SHA1 Message Date
Gregory Letellier
6363ad1b4d add config system 2023-11-15 09:18:09 +01:00
3 changed files with 88 additions and 26 deletions

View File

@@ -20,13 +20,11 @@ class Application
// enable error reporting // enable error reporting
$this->setReporting(); $this->setReporting();
try { // create container
$dotenv = Dotenv::createImmutable($this->_root); Container::set("ROOT", $this->_root);
$dotenv->load();
} catch (\Throwable $th) { // init config
e($th->getMessage()); Config::getInstance();
die();
}
if ($this->isDebug()) { if ($this->isDebug()) {
$this->_whoops = new \Whoops\Run(); $this->_whoops = new \Whoops\Run();
@@ -35,9 +33,6 @@ class Application
$this->_whoops->register(); $this->_whoops->register();
} }
// create container
Container::set("ROOT", $this->_root);
$db_config = $this->getDatabaseConfig(); $db_config = $this->getDatabaseConfig();
if (!empty($db_config)) { if (!empty($db_config)) {
@@ -51,34 +46,31 @@ class Application
private function isDebug(): bool private function isDebug(): bool
{ {
$ret = true; $ret = (Config::get("DEBUG") === "true");
if (isset($_ENV["DEBUG"])) {
$ret = ($_ENV["DEBUG"] == "true");
}
return $ret; return $ret;
} }
private function getDatabaseConfig(): array private function getDatabaseConfig(): array
{ {
$ret = []; $ret = [];
$connections = isset($_ENV["CONNECTIONS"]) ? explode(",", $_ENV["CONNECTIONS"]) : []; $connections = explode(",", Config::get("CONNECTIONS") ?? "") ;
foreach ($connections as $connection) { foreach ($connections as $connection) {
if ($connection !== "") { if ($connection !== "") {
$conn = []; $conn = [];
$key = (strtoupper(trim($connection))); $key = (strtoupper(trim($connection)));
$conn["TYPE"] = isset($_ENV[$this->getKey("TYPE", $key)]) ? $_ENV[$this->getKey("TYPE", $key)] : null; $conn["TYPE"] = Config::get($this->getKey("TYPE", $key));
$conn["USER"] = isset($_ENV[$this->getKey("USER", $key)]) ? $_ENV[$this->getKey("USER", $key)] : null; $conn["USER"] = Config::get($this->getKey("USER", $key));
$conn["HOST"] = isset($_ENV[$this->getKey("HOST", $key)]) ? $_ENV[$this->getKey("HOST", $key)] : null; $conn["HOST"] = Config::get($this->getKey("HOST", $key));
$conn["PASSWORD"] = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null; $conn["PASSWORD"] = Config::get($this->getKey("PASSWORD", $key));
$conn["DATABASE"] = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null; $conn["DATABASE"] = Config::get($this->getKey("DATABASE", $key));
$conn["PORT"] = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null; $conn["PORT"] = Config::get($this->getKey("PORT", $key));
if ($conn["TYPE"] == "sqlite") { if ($conn["TYPE"] == "sqlite") {
if (!is_file($conn["DATABASE"])) { if (!is_file($conn["DATABASE"])) {
$conn["DATABASE"] = $this->_root . DIRECTORY_SEPARATOR . "databases" . DIRECTORY_SEPARATOR . $conn["DATABASE"]; $conn["DATABASE"] = $this->_root . DIRECTORY_SEPARATOR . "databases" . DIRECTORY_SEPARATOR . $conn["DATABASE"];
} }
} }
if ($conn["TYPE"] == "mysql") { if ($conn["TYPE"] == "mysql") {
$charset = isset($_ENV[$this->getKey("CHARSET", $key)]) ? $_ENV[$this->getKey("CHARSET", $key)] : null; $charset = Config::get($this->getKey("CHARSET", $key));
if ($charset !== null) { if ($charset !== null) {
$conn["CHARSET"] = $charset; $conn["CHARSET"] = $charset;
} }

View File

@@ -0,0 +1,73 @@
<?php
namespace Kletellier\Framework\Core\Kernel;
class Config
{
private static $instance;
private array $container;
private string $cachepath;
private function __construct()
{
$this->cachepath = Container::get("ROOT") . DIRECTORY_SEPARATOR . "cache" . DIRECTORY_SEPARATOR . "config.php";
$this->container = [];
$this->fillValues();
}
private function createCache()
{
if (isset($this->container["DEBUG"])) {
if (($this->container["DEBUG"] == "false") && !is_file($this->cachepath)) {
// production app cache not created
$str = "<?php return " . json_encode(array_values($this->container)) . "; ?>";
file_put_contents($this->cachepath, $str);
}
}
}
private function fillValues()
{
$config = [];
if (is_file($this->cachepath)) {
$config = require $this->cachepath;
} else {
$config = \Dotenv\Dotenv::createArrayBacked(Container::get("ROOT"))->load();
}
foreach ($config as $key => $value) {
$this->container[$key] = $value;
}
$this->createCache();
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function setValue($key, $value)
{
$this->container[$key] = $value;
}
public function getValue($key)
{
return isset($this->container[$key]) ? $this->container[$key] : null;
}
public static function get(string $key)
{
$inst = self::getInstance();
return $inst->getValue($key);
}
public static function set(string $key, mixed $value)
{
$inst = self::getInstance();
return $inst->setValue($key, $value);
}
}

View File

@@ -31,10 +31,7 @@ class Templating
private function isDebug(): bool private function isDebug(): bool
{ {
$ret = true; $ret = (Config::get("DEBUG") == "true");
if (isset($_ENV["DEBUG"])) {
$ret = ($_ENV["DEBUG"] == "true");
}
return $ret; return $ret;
} }