Compare commits
12 Commits
610ed871bb
...
1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f204e5f13f | ||
|
|
33f4080ec6 | ||
|
|
e4505017be | ||
|
|
3338142a7f | ||
|
|
0a16e2ea98 | ||
|
|
7cee4e1d94 | ||
|
|
f024cceb6b | ||
|
|
8bd8e86748 | ||
|
|
db32b2e046 | ||
|
|
a597f2e883 | ||
|
|
9e6afc997a | ||
|
|
9eeb3d353d |
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset>
|
||||
<rule ref="PSR12"/>
|
||||
<file>src/</file>
|
||||
<file>app/</file>
|
||||
<file>console</file>
|
||||
<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;
|
||||
|
||||
use App\Core\Controllers\Controller;
|
||||
use Kletellier\Framework\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);
|
||||
@@ -1,22 +1,17 @@
|
||||
{
|
||||
"name": "kletellier/web",
|
||||
"name": "kletellier/miniweb",
|
||||
"description": "Mini website template",
|
||||
"type": "project",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "~8.1",
|
||||
"kletellier/pdowrapper": "dev-master",
|
||||
"symfony/http-kernel": "^6.3",
|
||||
"bramus/router": "~1.6",
|
||||
"filp/whoops": "^2.15",
|
||||
"symfony/var-dumper": "^6.3",
|
||||
"eftec/bladeone": "^4.9"
|
||||
"kletellier/framework": "~1.0"
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://git.kletellier.xyz/greg/PdoWrapper.git"
|
||||
}
|
||||
"type": "composer",
|
||||
"url": "https://git.kletellier.xyz/api/packages/greg/composer"
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.4",
|
||||
@@ -25,9 +20,8 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src"
|
||||
},
|
||||
"files": ["src/helpers.php"]
|
||||
"App\\": "app"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
@@ -39,5 +33,10 @@
|
||||
"pestphp/pest-plugin": true,
|
||||
"infection/extension-installer": true
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"post-root-package-install": [
|
||||
"@php -r \"file_exists('.env') || copy('.env.default', '.env');\""
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
5239
composer.lock
generated
5239
composer.lock
generated
File diff suppressed because it is too large
Load Diff
41
console
Normal file
41
console
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/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';
|
||||
|
||||
// start application
|
||||
$kapp = new \Kletellier\Framework\Core\Kernel\Application(ROOT);
|
||||
|
||||
$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
|
||||
*/
|
||||
define('DS', DIRECTORY_SEPARATOR);
|
||||
define('ROOT', dirname(dirname(__FILE__)));
|
||||
|
||||
*/
|
||||
define('ROOT', dirname(dirname(__FILE__)));
|
||||
|
||||
/**
|
||||
* Enable autoload
|
||||
*/
|
||||
require_once ROOT . DS . 'vendor'. DS . 'autoload.php';
|
||||
|
||||
require_once ROOT . DIRECTORY_SEPARATOR . 'vendor'. DIRECTORY_SEPARATOR . 'autoload.php';
|
||||
|
||||
// start application
|
||||
$app = new \App\Core\Kernel\Application();
|
||||
$app = new \Kletellier\Framework\Core\Kernel\Application(ROOT);
|
||||
|
||||
// Handle Url and render Response
|
||||
$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,103 +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,43 +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";
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -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