Init
This commit is contained in:
0
src/.gitkeep
Normal file
0
src/.gitkeep
Normal file
14
src/Controllers/Home.php
Normal file
14
src/Controllers/Home.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Controllers\Controller;
|
||||
|
||||
class Home extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$html = $this->renderView("index", []);
|
||||
$this->render($html);
|
||||
}
|
||||
}
|
||||
119
src/Core/Controllers/Controller.php
Normal file
119
src/Core/Controllers/Controller.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core\Controllers;
|
||||
|
||||
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
|
||||
{
|
||||
extract($params);
|
||||
$path = ROOT . DS . "views" . DS . str_replace(".", DS, $view) . ".php";
|
||||
ob_start();
|
||||
try {
|
||||
include $path;
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
$buffer = ltrim(ob_get_clean());
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
}
|
||||
}
|
||||
99
src/Core/Kernel/Application.php
Normal file
99
src/Core/Kernel/Application.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core\Kernel;
|
||||
|
||||
use App\Core\Routing\Router;
|
||||
use Dotenv\Dotenv;
|
||||
use Kletellier\PdoWrapper\Connection;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Application
|
||||
{
|
||||
protected Router $router;
|
||||
protected $_whoops;
|
||||
public function __construct()
|
||||
{
|
||||
// enable error reporting
|
||||
$this->setReporting();
|
||||
|
||||
try {
|
||||
$dotenv = Dotenv::createImmutable(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();
|
||||
}
|
||||
|
||||
$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;
|
||||
$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()
|
||||
{
|
||||
ob_start(null, 0, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE);
|
||||
try {
|
||||
$this->router->run();
|
||||
} catch (\Exception $ex) {
|
||||
e($ex->getMessage());
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
src/Core/Routing/Router.php
Normal file
35
src/Core/Routing/Router.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core\Routing;
|
||||
|
||||
use Bramus\Router\Router as RouterRouter;
|
||||
|
||||
class Router
|
||||
{
|
||||
private $_router;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->_router->run();
|
||||
}
|
||||
|
||||
private function init()
|
||||
{
|
||||
$this->_router = new RouterRouter();
|
||||
$this->loadFromFile(ROOT . DS . "routes");
|
||||
$this->_router->set404("404 - Not found");
|
||||
}
|
||||
|
||||
private function loadFromFile($path)
|
||||
{
|
||||
if (is_dir($path)) {
|
||||
$router = $this->_router;
|
||||
require $path . DS . "routes.php";
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/helpers.php
Normal file
15
src/helpers.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user