Compare commits
8 Commits
13baf7660c
...
1.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f024cceb6b | ||
|
|
8bd8e86748 | ||
|
|
db32b2e046 | ||
|
|
a597f2e883 | ||
|
|
9e6afc997a | ||
|
|
9eeb3d353d | ||
|
|
610ed871bb | ||
|
|
8d697f6f4f |
@@ -2,5 +2,6 @@
|
|||||||
<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>
|
||||||
24
app/Commands/TestCommand.php
Normal file
24
app/Commands/TestCommand.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Controllers;
|
namespace App\Controllers;
|
||||||
|
|
||||||
use App\Core\Controllers\Controller;
|
use Kletellier\Framework\Core\Controllers\Controller;
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
|
|
||||||
class Home extends Controller
|
class Home extends Controller
|
||||||
{
|
{
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$date = new \DateTime('now', new DateTimeZone("Europe/Paris"));
|
$date = new \DateTime('now', new DateTimeZone("Europe/Paris"));
|
||||||
$html = $this->renderView("index", ["date" => $date->format("d/m/Y H:i:s")]);
|
$html = $this->renderView("index", ["date" => $date->format("d/m/Y H:i:s")]);
|
||||||
$this->render($html);
|
$this->render($html);
|
||||||
@@ -1,22 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "kletellier/web",
|
"name": "kletellier/miniweb",
|
||||||
"description": "Mini website template",
|
"description": "Mini website template",
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "~8.1",
|
"php": "~8.1",
|
||||||
"kletellier/pdowrapper": "dev-master",
|
"greg/framework": "^1.0",
|
||||||
"symfony/http-kernel": "^6.3",
|
"kletellier/pdowrapper": "^1.0"
|
||||||
"bramus/router": "~1.6",
|
|
||||||
"filp/whoops": "^2.15",
|
|
||||||
"symfony/var-dumper": "^6.3",
|
|
||||||
"eftec/bladeone": "^4.9"
|
|
||||||
},
|
},
|
||||||
"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": {
|
||||||
"squizlabs/php_codesniffer": "^3.4",
|
"squizlabs/php_codesniffer": "^3.4",
|
||||||
@@ -25,9 +21,8 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"App\\": "src"
|
"App\\": "app"
|
||||||
},
|
}
|
||||||
"files": ["src/helpers.php"]
|
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
1630
composer.lock
generated
1630
composer.lock
generated
File diff suppressed because it is too large
Load Diff
38
console
Normal file
38
console
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Symfony\Component\Console\Application;
|
||||||
|
|
||||||
|
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loading path constant
|
||||||
|
*/
|
||||||
|
define('DS', DIRECTORY_SEPARATOR);
|
||||||
|
define('ROOT', dirname(__FILE__));
|
||||||
|
|
||||||
|
function findCommandClasses($directory)
|
||||||
|
{
|
||||||
|
$commandClasses = [];
|
||||||
|
$files = glob($directory . '/*.php');
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$commandClasses[] = basename($file, '.php');
|
||||||
|
}
|
||||||
|
return $commandClasses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable autoload
|
||||||
|
*/
|
||||||
|
require_once ROOT . DIRECTORY_SEPARATOR . 'vendor'. DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
|
|
||||||
|
$application = new Application();
|
||||||
|
// Load all commands in Commands folder
|
||||||
|
$command_directory = ROOT . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . "Commands";
|
||||||
|
$commandClasses = findCommandClasses($command_directory);
|
||||||
|
foreach ($commandClasses as $commandClass) {
|
||||||
|
$clsname = "App\\Commands\\$commandClass";
|
||||||
|
$application->add(new $clsname());
|
||||||
|
}
|
||||||
|
$application->run();
|
||||||
@@ -2,19 +2,16 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Loading path constant
|
* Loading path constant
|
||||||
*/
|
*/
|
||||||
define('DS', DIRECTORY_SEPARATOR);
|
define('ROOT', dirname(dirname(__FILE__)));
|
||||||
define('ROOT', dirname(dirname(__FILE__)));
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable autoload
|
* Enable autoload
|
||||||
*/
|
*/
|
||||||
require_once ROOT . DS . 'vendor'. DS . 'autoload.php';
|
require_once ROOT . DIRECTORY_SEPARATOR . 'vendor'. DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
|
|
||||||
|
|
||||||
// start application
|
// start application
|
||||||
$app = new \App\Core\Kernel\Application();
|
$app = new \Kletellier\Framework\Core\Kernel\Application(ROOT);
|
||||||
|
|
||||||
// Handle Url and render Response
|
// Handle Url and render Response
|
||||||
$app->handle();
|
$app->handle();
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Core\Controllers;
|
|
||||||
|
|
||||||
use App\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()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
<?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;
|
|
||||||
if($conn["TYPE"] == "sqlite")
|
|
||||||
{
|
|
||||||
if(!is_file($conn["DATABASE"]))
|
|
||||||
{
|
|
||||||
$conn["DATABASE"] = ROOT . DS . "databases" . DS . $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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Core\Kernel;
|
|
||||||
|
|
||||||
use eftec\bladeone\BladeOne;
|
|
||||||
|
|
||||||
class Templating
|
|
||||||
{
|
|
||||||
private $_views;
|
|
||||||
private $_cache;
|
|
||||||
private static $_instance = null;
|
|
||||||
private ?BladeOne $_blade = null;
|
|
||||||
|
|
||||||
private function __construct()
|
|
||||||
{
|
|
||||||
$this->_views = ROOT . DS . "views";
|
|
||||||
$this->_cache = ROOT . DS . "cache";
|
|
||||||
$this->_blade = new BladeOne($this->_views, $this->_cache, BladeOne::MODE_AUTO);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getInstance()
|
|
||||||
{
|
|
||||||
if (self::$_instance == null) {
|
|
||||||
self::$_instance = new Templating();
|
|
||||||
}
|
|
||||||
return self::$_instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function render(string $template, array $params = []): string
|
|
||||||
{
|
|
||||||
return $this->_blade->run($template, $params);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
<?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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
<?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