init
This commit is contained in:
8
.env.default
Normal file
8
.env.default
Normal file
@@ -0,0 +1,8 @@
|
||||
CONNECTIONS=
|
||||
DB_DEFAULT_TYPE=mysql
|
||||
DB_DEFAULT_HOST=localhost
|
||||
DB_DEFAULT_PORT=3306
|
||||
DB_DEFAULT_DATABASE=database
|
||||
DB_DEFAULT_USER=root
|
||||
DB_DEFAULT_PASSWORD=password
|
||||
DEBUG=true
|
||||
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
vendor/
|
||||
.phpunit.result.cache
|
||||
!var/cache/.gitkeep
|
||||
var/cache/*
|
||||
!var/log/.gitkeep
|
||||
var/log/*
|
||||
.env
|
||||
.vscode
|
||||
cache/
|
||||
composer.lock
|
||||
7
.phpcs.xml
Normal file
7
.phpcs.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset>
|
||||
<rule ref="PSR12"/>
|
||||
<file>src/</file>
|
||||
<file>console</file>
|
||||
<arg name="colors"/>
|
||||
</ruleset>
|
||||
45
Makefile
Normal file
45
Makefile
Normal file
@@ -0,0 +1,45 @@
|
||||
COMPOSER ?= $(shell which composer)
|
||||
|
||||
.PHONY: init
|
||||
init:
|
||||
$(RM) -r .git
|
||||
git init
|
||||
|
||||
.PHONY: up
|
||||
up: install-vendor
|
||||
|
||||
.PHONY: install-vendor
|
||||
install-vendor:
|
||||
$(PHP) $(COMPOSER) install
|
||||
|
||||
.PHONY: clean
|
||||
clean: clean-vendor clean-composerlock
|
||||
|
||||
.PHONY: clean-vendor
|
||||
clean-vendor:
|
||||
$(RM) -r ./vendor
|
||||
|
||||
.PHONY: clean-composerlock
|
||||
clean-composerlock:
|
||||
$(RM) composer.lock
|
||||
|
||||
.PHONY: unit-tests
|
||||
unit-tests:
|
||||
./vendor/bin/pest
|
||||
|
||||
.PHONY: unit-tests-coverage
|
||||
unit-tests-coverage:
|
||||
XDEBUG_MODE=coverage ./vendor/bin/pest --coverage
|
||||
|
||||
.PHONY: mutation
|
||||
mutation:
|
||||
./vendor/bin/infection --test-framework=pest --show-mutations
|
||||
|
||||
.PHONY: code-sniffer
|
||||
code-sniffer:
|
||||
./vendor/bin/phpcs
|
||||
|
||||
.PHONY: code-sniffer-fix
|
||||
code-sniffer-fix:
|
||||
./vendor/bin/phpcbf
|
||||
|
||||
44
composer.json
Normal file
44
composer.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"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",
|
||||
"symfony/console": "^6.3"
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://git.kletellier.xyz/greg/PdoWrapper.git"
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.4",
|
||||
"pestphp/pest": "^1.21",
|
||||
"infection/infection": "^0.26.15"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Kletellier\\MiniWeb\\": "src"
|
||||
},
|
||||
"files": ["src/helpers.php"]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Kletellier\\MiniWeb\\tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"pestphp/pest-plugin": true,
|
||||
"infection/extension-installer": true
|
||||
}
|
||||
}
|
||||
}
|
||||
17
infection.json5
Normal file
17
infection.json5
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"$schema": "vendor/infection/infection/resources/schema.json",
|
||||
"source": {
|
||||
"directories": [
|
||||
"src"
|
||||
]
|
||||
},
|
||||
"phpUnit": {
|
||||
"configDir": ""
|
||||
},
|
||||
"logs": {
|
||||
"text": "./var/log/infection.log"
|
||||
},
|
||||
"mutators": {
|
||||
"@default": true
|
||||
}
|
||||
}
|
||||
18
phpunit.xml
Normal file
18
phpunit.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
cacheResultFile="./var/cache/"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Test Suite">
|
||||
<directory suffix="Test.php">./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<coverage processUncoveredFiles="true" cacheDirectory="./var/cache/">
|
||||
<include>
|
||||
<directory suffix=".php">./src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
||||
0
src/.gitkeep
Normal file
0
src/.gitkeep
Normal file
112
src/Core/Controllers/Controller.php
Normal file
112
src/Core/Controllers/Controller.php
Normal 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()
|
||||
{
|
||||
}
|
||||
}
|
||||
109
src/Core/Kernel/Application.php
Normal file
109
src/Core/Kernel/Application.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/Core/Kernel/Container.php
Normal file
41
src/Core/Kernel/Container.php
Normal 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);
|
||||
}
|
||||
}
|
||||
45
src/Core/Kernel/Templating.php
Normal file
45
src/Core/Kernel/Templating.php
Normal 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);
|
||||
}
|
||||
}
|
||||
38
src/Core/Routing/Router.php
Normal file
38
src/Core/Routing/Router.php
Normal 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
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);
|
||||
}
|
||||
}
|
||||
0
tests/Unit/.gitkeep
Normal file
0
tests/Unit/.gitkeep
Normal file
Reference in New Issue
Block a user