5 Commits

Author SHA1 Message Date
Gregory Letellier
8bd8e86748 remove src folder 2023-11-14 12:03:02 +01:00
Gregory Letellier
db32b2e046 split project in two part, framework and website 2023-11-14 12:02:02 +01:00
Gregory Letellier
a597f2e883 add app folder 2023-11-14 10:52:52 +01:00
Gregory Letellier
9e6afc997a ajout namespace spécifique 2023-11-14 08:58:44 +01:00
Gregory Letellier
9eeb3d353d Add new console system 2023-11-13 11:41:47 +01:00
13 changed files with 936 additions and 1094 deletions

View File

@@ -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>

View 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;
}
}

View File

@@ -2,7 +2,7 @@
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

View File

@@ -1,21 +1,21 @@
{ {
"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": "dev-master",
"symfony/http-kernel": "^6.3", "kletellier/pdowrapper": "dev-master"
"bramus/router": "~1.6",
"filp/whoops": "^2.15",
"symfony/var-dumper": "^6.3",
"eftec/bladeone": "^4.9"
}, },
"repositories": [ "repositories": [
{ {
"type": "git", "type": "git",
"url": "https://git.kletellier.xyz/greg/PdoWrapper.git" "url": "https://git.kletellier.xyz/greg/framework.git"
},
{
"type": "git",
"url": "https://git.kletellier.xyz/greg/pdowrapper.git"
} }
], ],
"require-dev": { "require-dev": {
@@ -25,9 +25,8 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"App\\": "src" "App\\": "app"
}, }
"files": ["src/helpers.php"]
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {

1621
composer.lock generated

File diff suppressed because it is too large Load Diff

38
console Normal file
View 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();

View File

@@ -3,18 +3,15 @@
/** /**
* 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();

View File

View File

@@ -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()
{
}
}

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}

View File

@@ -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";
}
}
}

View File

@@ -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);
}
}