Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de03a1f215 | ||
|
|
01e9fd2de3 | ||
|
|
6363ad1b4d | ||
|
|
ae19bba912 |
@@ -2,6 +2,5 @@
|
||||
<ruleset>
|
||||
<rule ref="PSR12"/>
|
||||
<file>src/</file>
|
||||
<file>console</file>
|
||||
<arg name="colors"/>
|
||||
</ruleset>
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "greg/framework",
|
||||
"name": "kletellier/framework",
|
||||
"description": "Mini MVC component",
|
||||
"type": "project",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -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,32 +46,35 @@ 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 = Config::get($this->getKey("CHARSET", $key));
|
||||
if ($charset !== null) {
|
||||
$conn["CHARSET"] = $charset;
|
||||
}
|
||||
}
|
||||
$ret[$connection] = $conn;
|
||||
}
|
||||
}
|
||||
|
||||
73
src/Core/Kernel/Config.php
Normal file
73
src/Core/Kernel/Config.php
Normal 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 " . var_export($this->container, true) . "; ?>";
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,13 @@ class Container
|
||||
private static $instance;
|
||||
private array $container;
|
||||
|
||||
private function __construct() {
|
||||
private function __construct()
|
||||
{
|
||||
$this->container = [];
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
@@ -19,11 +21,13 @@ class Container
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function setValue($key, $value) {
|
||||
public function setValue($key, $value)
|
||||
{
|
||||
$this->container[$key] = $value;
|
||||
}
|
||||
|
||||
public function getValue($key) {
|
||||
public function getValue($key)
|
||||
{
|
||||
return isset($this->container[$key]) ? $this->container[$key] : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user