5 Commits
1.0.0 ... 1.0.5

Author SHA1 Message Date
Gregory Letellier
de03a1f215 renaming package 2023-11-20 10:02:06 +01:00
Gregory Letellier
01e9fd2de3 fix export des config 2023-11-15 09:27:27 +01:00
Gregory Letellier
6363ad1b4d add config system 2023-11-15 09:18:09 +01:00
Gregory Letellier
ae19bba912 beautify, gestion charset mysql 2023-11-14 16:49:10 +01:00
Gregory Letellier
2ae0f14b99 passage en mode composer sur le gitea 2023-11-14 14:47:21 +01:00
8 changed files with 153 additions and 42 deletions

View File

@@ -2,6 +2,5 @@
<ruleset> <ruleset>
<rule ref="PSR12"/> <rule ref="PSR12"/>
<file>src/</file> <file>src/</file>
<file>console</file>
<arg name="colors"/> <arg name="colors"/>
</ruleset> </ruleset>

View File

@@ -1,11 +1,11 @@
{ {
"name": "greg/framework", "name": "kletellier/framework",
"description": "Mini MVC component", "description": "Mini MVC component",
"type": "project", "type": "project",
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "~8.1", "php": "~8.1",
"kletellier/pdowrapper": "dev-master", "kletellier/pdowrapper": "^1.0",
"symfony/http-kernel": "^6.3", "symfony/http-kernel": "^6.3",
"bramus/router": "~1.6", "bramus/router": "~1.6",
"filp/whoops": "^2.15", "filp/whoops": "^2.15",
@@ -15,8 +15,8 @@
}, },
"repositories": [ "repositories": [
{ {
"type": "git", "type": "composer",
"url": "https://git.kletellier.xyz/greg/PdoWrapper.git" "url": "https://git.kletellier.xyz/api/packages/greg/composer"
} }
], ],
"require-dev": { "require-dev": {

View File

@@ -0,0 +1,24 @@
<?php
namespace Kletellier\MiniWeb\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends Command
{
protected static $defaultName = 'app:test';
protected static $defaultDescription = 'Test.';
protected function configure(): void
{
$this->setHelp('Test');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln("test");
return Command::SUCCESS;
}
}

16
src/Controllers/Home.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
namespace Kletellier\MiniWeb\Controllers;
use Kletellier\MiniWeb\Core\Controllers\Controller;
use DateTimeZone;
class Home extends Controller
{
public function index()
{
$date = new \DateTime('now', new DateTimeZone("Europe/Paris"));
$html = $this->renderView("index", ["date" => $date->format("d/m/Y H:i:s")]);
$this->render($html);
}
}

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,32 +46,35 @@ 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") {
$charset = Config::get($this->getKey("CHARSET", $key));
if ($charset !== null) {
$conn["CHARSET"] = $charset;
}
}
$ret[$connection] = $conn; $ret[$connection] = $conn;
} }
} }

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 " . 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);
}
}

View File

@@ -7,11 +7,13 @@ class Container
private static $instance; private static $instance;
private array $container; private array $container;
private function __construct() { private function __construct()
{
$this->container = []; $this->container = [];
} }
public static function getInstance() { public static function getInstance()
{
if (self::$instance === null) { if (self::$instance === null) {
self::$instance = new self(); self::$instance = new self();
} }
@@ -19,11 +21,13 @@ class Container
return self::$instance; return self::$instance;
} }
public function setValue($key, $value) { public function setValue($key, $value)
{
$this->container[$key] = $value; $this->container[$key] = $value;
} }
public function getValue($key) { public function getValue($key)
{
return isset($this->container[$key]) ? $this->container[$key] : null; return isset($this->container[$key]) ? $this->container[$key] : null;
} }

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;
} }