diff --git a/src/Core/Kernel/Application.php b/src/Core/Kernel/Application.php index b60ba3f..80071fc 100644 --- a/src/Core/Kernel/Application.php +++ b/src/Core/Kernel/Application.php @@ -20,13 +20,11 @@ class Application // enable error reporting $this->setReporting(); - try { - $dotenv = Dotenv::createImmutable($this->_root); - $dotenv->load(); - } catch (\Throwable $th) { - e($th->getMessage()); - die(); - } + // create container + Container::set("ROOT", $this->_root); + + // init config + Config::getInstance(); if ($this->isDebug()) { $this->_whoops = new \Whoops\Run(); @@ -35,9 +33,6 @@ class Application $this->_whoops->register(); } - // create container - Container::set("ROOT", $this->_root); - $db_config = $this->getDatabaseConfig(); if (!empty($db_config)) { @@ -51,34 +46,31 @@ class Application private function isDebug(): bool { - $ret = true; - if (isset($_ENV["DEBUG"])) { - $ret = ($_ENV["DEBUG"] == "true"); - } + $ret = (Config::get("DEBUG") === "true"); return $ret; } private function getDatabaseConfig(): array { $ret = []; - $connections = isset($_ENV["CONNECTIONS"]) ? explode(",", $_ENV["CONNECTIONS"]) : []; + $connections = explode(",", Config::get("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; + $conn["TYPE"] = Config::get($this->getKey("TYPE", $key)); + $conn["USER"] = Config::get($this->getKey("USER", $key)); + $conn["HOST"] = Config::get($this->getKey("HOST", $key)); + $conn["PASSWORD"] = Config::get($this->getKey("PASSWORD", $key)); + $conn["DATABASE"] = Config::get($this->getKey("DATABASE", $key)); + $conn["PORT"] = Config::get($this->getKey("PORT", $key)); if ($conn["TYPE"] == "sqlite") { if (!is_file($conn["DATABASE"])) { $conn["DATABASE"] = $this->_root . DIRECTORY_SEPARATOR . "databases" . DIRECTORY_SEPARATOR . $conn["DATABASE"]; } } 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) { $conn["CHARSET"] = $charset; } diff --git a/src/Core/Kernel/Config.php b/src/Core/Kernel/Config.php new file mode 100644 index 0000000..4e53aaa --- /dev/null +++ b/src/Core/Kernel/Config.php @@ -0,0 +1,73 @@ +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 = "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); + } +} diff --git a/src/Core/Kernel/Templating.php b/src/Core/Kernel/Templating.php index 0a4df63..f077de7 100644 --- a/src/Core/Kernel/Templating.php +++ b/src/Core/Kernel/Templating.php @@ -31,10 +31,7 @@ class Templating private function isDebug(): bool { - $ret = true; - if (isset($_ENV["DEBUG"])) { - $ret = ($_ENV["DEBUG"] == "true"); - } + $ret = (Config::get("DEBUG") == "true"); return $ret; }