This commit is contained in:
Gregory Letellier
2023-11-14 11:18:23 +01:00
commit 13c80f5642
15 changed files with 509 additions and 0 deletions

0
src/.gitkeep Normal file
View File

View File

@@ -0,0 +1,112 @@
<?php
namespace Kletellier\MiniWeb\Core\Controllers;
use Kletellier\MiniWeb\Core\Kernel\Templating;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
abstract class Controller
{
protected $_cookies;
protected $_cookiestodelete;
protected Request $_request;
/**
* Controller constructor
*
* @param String $controller controller name
* @param String $action action to execute
*/
function __construct()
{
$this->_cookies = array();
$this->_cookiestodelete = array();
$this->_request = Request::createFromGlobals();
}
public function render($content, $status = 200, $headers = array('Content-Type' => 'text/html')): void
{
$this->getResponse($content, $status, $headers)->send();
}
/**
* Return instance of Symfony Response Component
* @return type
*/
private function getResponse($content, $status = 200, $headers = array('Content-Type' => 'text/html')): Response
{
$response = new Response($content, $status, $headers);
foreach ($this->_cookies as $cookie) {
$response->headers->setCookie($cookie);
}
foreach ($this->_cookiestodelete as $cookie) {
$response->headers->clearCookie($cookie);
}
return $response;
}
/**
* Return instance of Symfony Request component
*/
public function request(): Request
{
return $this->_request;
}
/**
* Return Html from view
*/
public function renderView($view, $params = []): string
{
return Templating::getInstance()->render($view, $params);
}
/**
* Add cookie to response
* @param type \Symfony\Component\HttpFoundation\Cookie $cookie
* @return void
*/
public function addCookie(\Symfony\Component\HttpFoundation\Cookie $cookie)
{
array_push($this->_cookies, $cookie);
}
/**
* Cookie to remove in response
* @param string $cookie cookie name to delete
* @return void
*/
public function removeCookie($cookie)
{
array_push($this->_cookiestodelete, $cookie);
}
/**
* Return a Json Response
*
* @param object $var object to be serialized in JSON
*/
public function renderJSON($var)
{
$response = new Response(json_encode($var));
$response->headers->set('Content-Type', 'application/json');
$response->send();
}
public function redirecting($url)
{
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($url);
foreach ($this->_cookies as $cookie) {
$response->headers->setCookie($cookie);
}
foreach ($this->_cookiestodelete as $cookie) {
$response->headers->clearCookie($cookie);
}
$response->send();
}
function __destruct()
{
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace Kletellier\MiniWeb\Core\Kernel;
use Kletellier\MiniWeb\Core\Routing\Router;
use Kletellier\MiniWeb\Core\Kernel\Container;
use Dotenv\Dotenv;
use Kletellier\PdoWrapper\Connection;
class Application
{
protected Router $router;
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();
}
if ($this->isDebug()) {
$this->_whoops = new \Whoops\Run();
$handler = new \Whoops\Handler\PrettyPageHandler();
$this->_whoops->pushHandler($handler);
$this->_whoops->register();
}
// create container
Container::set("ROOT",$this->_root);
$db_config = $this->getDatabaseConfig();
if (!empty($db_config)) {
// Initialize DB system
Connection::init($db_config, array_keys($db_config));
}
// Init router
$this->router = new Router();
}
private function isDebug(): bool
{
$ret = true;
if (isset($_ENV["DEBUG"])) {
$ret = ($_ENV["DEBUG"] == "true");
}
return $ret;
}
private function getDatabaseConfig(): array
{
$ret = [];
$connections = isset($_ENV["CONNECTIONS"]) ? explode(",", $_ENV["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;
if ($conn["TYPE"] == "sqlite") {
if (!is_file($conn["DATABASE"])) {
$conn["DATABASE"] = $this->_root . DIRECTORY_SEPARATOR . "databases" . DIRECTORY_SEPARATOR . $conn["DATABASE"];
}
}
$ret[$connection] = $conn;
}
}
return $ret;
}
private function getKey(string $type, string $name): string
{
return "DB_" . $name . "_" . $type;
}
/**
* Enable/Disable error reporting to output buffer
*/
private function setReporting()
{
error_reporting(E_ALL);
ini_set('display_errors', 'On');
}
public function handle()
{
try {
$this->router->run();
} catch (\Exception $ex) {
e($ex->getMessage());
die();
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Kletellier\MiniWeb\Core\Kernel;
class Container
{
private static $instance;
private array $container;
private function __construct() {
$this->container = [];
}
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

@@ -0,0 +1,45 @@
<?php
namespace Kletellier\MiniWeb\Core\Kernel;
use eftec\bladeone\BladeOne;
class Templating
{
private $_views;
private $_cache;
private static $_instance = null;
private ?BladeOne $_blade = null;
private $_root;
private function __construct()
{
$this->_root = Container::get("ROOT");
$this->_views = $this->_root . DIRECTORY_SEPARATOR . "views";
$this->_cache = $this->_root . DIRECTORY_SEPARATOR . "cache";
$mode = ($this->isDebug()) ? BladeOne::MODE_AUTO : BladeOne::MODE_FAST;
$this->_blade = new BladeOne($this->_views, $this->_cache, $mode);
}
public static function getInstance()
{
if (self::$_instance == null) {
self::$_instance = new Templating();
}
return self::$_instance;
}
private function isDebug(): bool
{
$ret = true;
if (isset($_ENV["DEBUG"])) {
$ret = ($_ENV["DEBUG"] == "true");
}
return $ret;
}
public function render(string $template, array $params = []): string
{
return $this->_blade->run($template, $params);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Kletellier\MiniWeb\Core\Routing;
use Bramus\Router\Router as RouterRouter;
use Kletellier\MiniWeb\Core\Kernel\Container;
class Router
{
private $_router;
private $_root;
public function __construct()
{
$this->_root = Container::get("ROOT");
$this->init();
}
public function run()
{
$this->_router->run();
}
private function init()
{
$this->_router = new RouterRouter();
$this->loadFromFile($this->_root . DIRECTORY_SEPARATOR . "routes");
$this->_router->set404("404 - Not found");
}
private function loadFromFile($path)
{
if (is_dir($path)) {
$router = $this->_router;
require $path . DIRECTORY_SEPARATOR . "routes.php";
}
}
}

15
src/helpers.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
if (! function_exists('e')) {
/**
* Encode HTML special characters in a string.
*
* @param string|null $value
* @param bool $doubleEncode
* @return string
*/
function e($value, $doubleEncode = true)
{
return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
}
}