Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01e9fd2de3 | ||
|
|
6363ad1b4d | ||
|
|
ae19bba912 | ||
|
|
2ae0f14b99 |
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset>
|
||||
<rule ref="PSR12"/>
|
||||
<file>src/</file>
|
||||
<file>console</file>
|
||||
<file>src/</file>
|
||||
<arg name="colors"/>
|
||||
</ruleset>
|
||||
@@ -5,7 +5,7 @@
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "~8.1",
|
||||
"kletellier/pdowrapper": "dev-master",
|
||||
"kletellier/pdowrapper": "^1.0",
|
||||
"symfony/http-kernel": "^6.3",
|
||||
"bramus/router": "~1.6",
|
||||
"filp/whoops": "^2.15",
|
||||
@@ -15,8 +15,8 @@
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://git.kletellier.xyz/greg/PdoWrapper.git"
|
||||
"type": "composer",
|
||||
"url": "https://git.kletellier.xyz/api/packages/greg/composer"
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
|
||||
24
src/Commands/TestCommand.php
Normal file
24
src/Commands/TestCommand.php
Normal 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
16
src/Controllers/Home.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -5,28 +5,26 @@ namespace Kletellier\Framework\Core\Kernel;
|
||||
use Kletellier\Framework\Core\Routing\Router;
|
||||
use Kletellier\Framework\Core\Kernel\Container;
|
||||
use Dotenv\Dotenv;
|
||||
use Kletellier\PdoWrapper\Connection;
|
||||
use Kletellier\PdoWrapper\Connection;
|
||||
|
||||
class Application
|
||||
{
|
||||
protected Router $router;
|
||||
protected $_whoops;
|
||||
protected $_whoops;
|
||||
private $_root;
|
||||
|
||||
public function __construct(string $root)
|
||||
{
|
||||
{
|
||||
$this->_root = $root;
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,18 @@
|
||||
|
||||
namespace Kletellier\Framework\Core\Kernel;
|
||||
|
||||
class Container
|
||||
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,23 +21,25 @@ 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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