mirror of
https://github.com/Aviortheking/DeltaCMS.git
synced 2025-04-23 03:12:14 +00:00
Reworked most file to respect PSR-2
This commit is contained in:
parent
fb3249fa74
commit
5d522a2064
6
.phpcs.xml
Normal file
6
.phpcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PSR2" xsi:noNamespaceSchemaLocation="../../../phpcs.xsd">
|
||||
<description>The PSR-2 coding standard.</description>
|
||||
<arg name="tab-width" value="4"/>
|
||||
<rule ref="PSR1"/>
|
||||
<rule ref="PSR2"/>
|
||||
</ruleset>
|
@ -13,6 +13,19 @@
|
||||
"psr-4": {
|
||||
"AdminPanel\\": "src/AdminPanel",
|
||||
"": "src/Modules"
|
||||
}
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"src/Modules"
|
||||
],
|
||||
"files": [
|
||||
"src/AdminPanel/Functions.php"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"twig/twig": "^2.7"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.4",
|
||||
"symfony/var-dumper": "^4.2"
|
||||
}
|
||||
}
|
||||
|
43
src/AdminPanel/Classes/AdminPanel.php
Normal file
43
src/AdminPanel/Classes/AdminPanel.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace AdminPanel\Classes;
|
||||
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
use Twig\Environment;
|
||||
|
||||
class AdminPanel
|
||||
{
|
||||
/** @var AdminPanel $instance */
|
||||
private static $instance = null;
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!isset(AdminPanel::$instance)) {
|
||||
AdminPanel::$instance = new self();
|
||||
Cache::getInstance()->addTemplateFolder("/AdminPanel/Twig");
|
||||
AdminPanel::$instance->addLoaderFolder(ROOT . "/AdminPanel/Twig");
|
||||
}
|
||||
return AdminPanel::$instance;
|
||||
}
|
||||
|
||||
private $loader;
|
||||
public function getLoader()
|
||||
{
|
||||
return $this->loader;
|
||||
}
|
||||
public function addLoaderFolder(String $path, String $prefix = "AdminPanel")
|
||||
{
|
||||
$this->loader->addPath($path, $prefix);
|
||||
}
|
||||
|
||||
/** @var \Twig\Environment $twig */
|
||||
private $twig;
|
||||
public function getTwig()
|
||||
{
|
||||
return isset($this->twig) ? $this->twig : $this->twig = new Environment($this->loader);
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loader = new FilesystemLoader();
|
||||
}
|
||||
}
|
23
src/AdminPanel/Classes/Authentificator.php
Normal file
23
src/AdminPanel/Classes/Authentificator.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace AdminPanel\Classes;
|
||||
|
||||
class Authentificator
|
||||
{
|
||||
|
||||
/** @var Authentificator $instance */
|
||||
private static $instance = null;
|
||||
private $isLoggedIn = false;
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!isset(Authentificator::$instance)) {
|
||||
Authentificator::$instance = new self();
|
||||
}
|
||||
return Authentificator::$instance;
|
||||
}
|
||||
|
||||
public function isLoggedIn()
|
||||
{
|
||||
return $this->isLoggedIn;
|
||||
}
|
||||
}
|
48
src/AdminPanel/Classes/Cache.php
Normal file
48
src/AdminPanel/Classes/Cache.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace AdminPanel\Classes;
|
||||
|
||||
class Cache
|
||||
{
|
||||
/** @var Cache $instance */
|
||||
private static $instance = null;
|
||||
private static $folder = "../cache/";
|
||||
private static $tpFileName = "templates.json";
|
||||
private static $templates;
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!isset(Cache::$instance)) {
|
||||
Cache::$instance = new self();
|
||||
}
|
||||
return Cache::$instance;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (!is_dir(Cache::$folder)) {
|
||||
mkdir(Cache::$folder);
|
||||
}
|
||||
Cache::$templates = Cache::$folder . Cache::$tpFileName;
|
||||
if (!is_file(Cache::$templates)) {
|
||||
$fp = fopen(Cache::$templates, "wb");
|
||||
fwrite($fp, "{}");
|
||||
fclose($fp);
|
||||
}
|
||||
}
|
||||
|
||||
public function addTemplateFolder(string $folder, string $namespace = "AdminPanel")
|
||||
{
|
||||
$fp = fopen(Cache::$templates, "wb");
|
||||
$json = file_get_contents(Cache::$templates);
|
||||
// $json = json_decode(fread($fp, filesize(Cache::$templates)));
|
||||
$json[$folder] = $namespace;
|
||||
fwrite($fp, json_encode($json));
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
public function addRoute(string $name, string $path, string $controller, string $function, $options = array())
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
}
|
@ -2,18 +2,36 @@
|
||||
|
||||
namespace AdminPanel\Classes;
|
||||
|
||||
class Controller {
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
use Twig\Environment;
|
||||
|
||||
class Controller
|
||||
{
|
||||
|
||||
protected $urlArguments = array();
|
||||
protected $moduleRoot = null;
|
||||
|
||||
public function setUrlArguments($args) {
|
||||
public function setUrlArguments($args)
|
||||
{
|
||||
$this->urlArguments = $args;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getUrlArguments() {
|
||||
protected function getUrlArguments()
|
||||
{
|
||||
return $this->urlArguments;
|
||||
}
|
||||
|
||||
//TODO implements functions and variables to add functionnalities to controllers
|
||||
|
||||
public function setModuleRoot($root)
|
||||
{
|
||||
$this->moduleRoot = $root;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getModuleRoot()
|
||||
{
|
||||
return $this->moduleRoot;
|
||||
}
|
||||
|
||||
//TODO implements functions and variables to add functionnalities to controllers
|
||||
}
|
||||
|
@ -2,12 +2,14 @@
|
||||
|
||||
namespace AdminPanel\Classes\Enum;
|
||||
|
||||
abstract class Enum {
|
||||
abstract class Enum
|
||||
{
|
||||
/** @var null|array $constCacheArray */
|
||||
private static $constCacheArray = NULL;
|
||||
private static $constCacheArray = null;
|
||||
|
||||
private static function getConstants() {
|
||||
if (self::$constCacheArray == NULL) {
|
||||
private static function getConstants()
|
||||
{
|
||||
if (self::$constCacheArray == null) {
|
||||
self::$constCacheArray = [];
|
||||
}
|
||||
$calledClass = get_called_class();
|
||||
@ -18,7 +20,8 @@ abstract class Enum {
|
||||
return self::$constCacheArray[$calledClass];
|
||||
}
|
||||
|
||||
public static function isValidName($name, $strict = false) {
|
||||
public static function isValidName($name, $strict = false)
|
||||
{
|
||||
$constants = self::getConstants();
|
||||
|
||||
if ($strict) {
|
||||
@ -29,18 +32,21 @@ abstract class Enum {
|
||||
return in_array(strtolower($name), $keys);
|
||||
}
|
||||
|
||||
public static function isValidValue($value, $strict = true) {
|
||||
public static function isValidValue($value, $strict = true)
|
||||
{
|
||||
$values = array_values(self::getConstants());
|
||||
return in_array($value, $values, $strict);
|
||||
}
|
||||
}
|
||||
|
||||
// abstract class DaysOfWeek extends BasicEnum {
|
||||
// const Sunday = 0;
|
||||
// const Monday = 1;
|
||||
// const Tuesday = 2;
|
||||
// const Wednesday = 3;
|
||||
// const Thursday = 4;
|
||||
// const Friday = 5;
|
||||
// const Saturday = 6;
|
||||
// }
|
||||
/*
|
||||
abstract class DaysOfWeek extends Enum {
|
||||
const Sunday = 0;
|
||||
const Monday = 1;
|
||||
const Tuesday = 2;
|
||||
const Wednesday = 3;
|
||||
const Thursday = 4;
|
||||
const Friday = 5;
|
||||
const Saturday = 6;
|
||||
}
|
||||
*/
|
||||
|
99
src/AdminPanel/Functions.php
Normal file
99
src/AdminPanel/Functions.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
use AdminPanel\Classes\Cache;
|
||||
|
||||
function ping()
|
||||
{
|
||||
return "pong";
|
||||
}
|
||||
|
||||
function startsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
return (substr($haystack, 0, $length) === $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param string $slug
|
||||
* @param object $options options->regex &| options->setting
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
function slugEqualToURI($slug, $uri, $options)
|
||||
{
|
||||
$uri = explode("/", trim($uri, "\/"));
|
||||
$slug = explode("/", trim($slug, '\/'));
|
||||
$return = array();
|
||||
|
||||
if (count($uri) != count($slug)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($slug as $key => $value) {
|
||||
if (preg_match("/{.+}/", $value)) {
|
||||
$elemnt = preg_replace("/{|}/", "", $value);
|
||||
$elOptions = $options->$elemnt;
|
||||
if ($elOptions->regex != null && preg_match($elOptions->regex, $uri[$key])) {
|
||||
$return[$elemnt] = $uri[$key];
|
||||
continue;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
//TODO correspond with module settings
|
||||
} else {
|
||||
if ($value == $uri[$key]) {
|
||||
continue;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
function getModulesJSON()
|
||||
{
|
||||
$t = array();
|
||||
$modulesDIR = "./Modules";
|
||||
$modules = array_diff(scandir($modulesDIR), array('..', '.'));
|
||||
foreach ($modules as $module) {
|
||||
$moduleDIR = $modulesDIR . "/" . $module;
|
||||
$file = $moduleDIR . "/" . strtolower($module) . ".json";
|
||||
if (is_dir($moduleDIR) && is_file($file)) {
|
||||
$json = json_decode(file_get_contents($file), true);
|
||||
if ($json) {
|
||||
$t[$module] = $json;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $t;
|
||||
}
|
||||
|
||||
function initCache()
|
||||
{
|
||||
$json = getModulesJSON();
|
||||
foreach ($json as $moduleName => $moduleValues) {
|
||||
if (isset($moduleValues["routes"])) {
|
||||
//TODO
|
||||
}
|
||||
if (isset($moduleValues["templateFolder"])) {
|
||||
Cache::getInstance()->addTemplateFolder(ROOT . "/Modules/" . $moduleName . $moduleValues["templateFolder"], $moduleName);
|
||||
}
|
||||
// return $moduleValues;
|
||||
}
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define constant.
|
||||
* (well really it's just an hack for my linter I really don't why we can't define constant in the main process)
|
||||
*
|
||||
* @param string $name constant name
|
||||
* @param mixed $value contant value
|
||||
*/
|
||||
function dconst(string $name, $value)
|
||||
{
|
||||
define($name, $value);
|
||||
}
|
@ -4,7 +4,7 @@ namespace AdminLib\Classes;
|
||||
|
||||
use AdminPanel\Classes\Controller;
|
||||
|
||||
|
||||
class AdminController extends Controller {
|
||||
class AdminController extends Controller
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -4,10 +4,10 @@ namespace AdminLib\Controller;
|
||||
|
||||
use AdminLib\Classes\AdminController;
|
||||
|
||||
class IndexController extends AdminController {
|
||||
|
||||
public function index() {
|
||||
class IndexController extends AdminController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return "Hello Administrator!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,15 +3,21 @@
|
||||
namespace Index\Controller;
|
||||
|
||||
use AdminPanel\Classes\Controller;
|
||||
use AdminPanel\Classes\AdminPanel;
|
||||
|
||||
class IndexController extends Controller {
|
||||
class IndexController extends Controller
|
||||
{
|
||||
|
||||
public function index() {
|
||||
return "hello world!";
|
||||
public function index()
|
||||
{
|
||||
return AdminPanel::getInstance()->getTwig()->render("@Index/index.twig", [
|
||||
"title" => "Coming Soon"
|
||||
]);
|
||||
// return file_get_contents($this->getModuleRoot() . "/index.html");
|
||||
}
|
||||
|
||||
public function test() {
|
||||
var_dump($this->getUrlArguments());
|
||||
return "test working!";
|
||||
public function test()
|
||||
{
|
||||
return "hello " . $this->getUrlArguments()["slug"];
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,27 @@
|
||||
namespace ModuleName\Controller;
|
||||
|
||||
use AdminPanel\Classes\Controller;
|
||||
use AdminPanel\Classes\Authentificator;
|
||||
|
||||
class ExampleController extends Controller
|
||||
{
|
||||
|
||||
class ExampleController extends Controller {
|
||||
public function example()
|
||||
{
|
||||
return "hello " . $this->getUrlArguments()["arg"] . "!";
|
||||
}
|
||||
|
||||
public function example() {
|
||||
return "hello Controller!";
|
||||
public function index()
|
||||
{
|
||||
return "Hellow Example Controller!";
|
||||
}
|
||||
|
||||
public function isLoggedIn()
|
||||
{
|
||||
if (Authentificator::getInstance()->isLoggedIn()) {
|
||||
return "test is false!";
|
||||
} else {
|
||||
return "test is true";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,29 @@
|
||||
<?php
|
||||
|
||||
use AdminPanel\Classes\AdminPanel;
|
||||
use AdminPanel\Classes\Cache;
|
||||
|
||||
session_start();
|
||||
ini_set('display_errors', 'On');
|
||||
|
||||
define("ROOT", __DIR__);
|
||||
|
||||
use AdminPanel\Classes\Controller;
|
||||
|
||||
/** @var Composer\Autoload\ClassLoader $loader */
|
||||
$loader = require_once ROOT . "/../vendor/autoload.php";
|
||||
$loader = require_once __DIR__ . "/../vendor/autoload.php";
|
||||
// var_dump($_SERVER["REQUEST_URI"] . "/");
|
||||
// $_SERVER["REQUEST_URI"] = "/test/";
|
||||
//get all dirs
|
||||
$modulesDIR = ROOT . "/Modules";
|
||||
$modulesDIR = __DIR__ . "/Modules";
|
||||
$modules = array_diff(scandir($modulesDIR), array('..', '.'));
|
||||
|
||||
dconst("ROOT", __DIR__);
|
||||
|
||||
initCache();
|
||||
/*
|
||||
1: get all the template folders
|
||||
2: match routes directly with modules
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/** @var string $module */
|
||||
foreach ($modules as $module) {
|
||||
$moduleDIR = $modulesDIR . "/" . $module;
|
||||
@ -22,12 +32,26 @@ foreach ($modules as $module) {
|
||||
foreach ($json->routes as $routeName => $routeArgs) {
|
||||
$args = isset($routeArgs->args) ? $routeArgs->args : new stdClass();
|
||||
$composants = slugEqualToURI($routeArgs->path, $_SERVER["REQUEST_URI"], $args);
|
||||
// dump($composants !== false);
|
||||
if ($composants !== false) {
|
||||
if (isset($json->templateFolder)) {
|
||||
AdminPanel::getInstance()->addLoaderFolder($moduleDIR . $json->templateFolder, $module);
|
||||
}
|
||||
if (isset($routeArgs->file)) {
|
||||
if (isset($routeArgs->type)) {
|
||||
header("Content-Type: " . $routeArgs->type . "; charset=UTF-8");
|
||||
}
|
||||
echo file_get_contents($moduleDIR . $routeArgs->file);
|
||||
die;
|
||||
}
|
||||
$loader->loadClass($routeArgs->controller);
|
||||
$function = $routeArgs->function;
|
||||
// dump($function);
|
||||
/** @var AdminPanel\Classes\Controller $controller */
|
||||
$controller = new $routeArgs->controller;
|
||||
$controller->setUrlArguments($composants);
|
||||
$controller->setModuleRoot($moduleDIR);
|
||||
// if(isset($json->templateFolder)) $controller->loadTwig($json->templateFolder);
|
||||
echo $controller->$function();
|
||||
die;
|
||||
}
|
||||
@ -35,47 +59,7 @@ foreach ($modules as $module) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// echo Controller::example();
|
||||
|
||||
// require_once ROOT . "/system/router.php";
|
||||
|
||||
// define("ROUTER", Router::getRouter());
|
||||
function startsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
return (substr($haystack, 0, $length) === $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param string $slug
|
||||
* @param object $options options->regex &| options->setting
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
function slugEqualToURI($slug, $uri, $options) {
|
||||
$uri = explode("/", trim($uri, "\/"));
|
||||
$slug = explode("/", trim($slug, '\/'));
|
||||
$return = array();
|
||||
|
||||
if(count($uri) != count($slug)) return false;
|
||||
|
||||
foreach ($slug as $key => $value) {
|
||||
|
||||
if(preg_match("/{.+}/", $value)) {
|
||||
$elemnt = preg_replace("/{|}/", "", $value);
|
||||
$elOptions = $options->$elemnt;
|
||||
if($elOptions->regex != null && preg_match($elOptions->regex, $uri[$key])) {
|
||||
$return[$elemnt] = $uri[$key];
|
||||
continue;
|
||||
}
|
||||
else return false;
|
||||
//TODO correspond with module settings
|
||||
} else {
|
||||
if($value == $uri[$key]) continue;
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
http_response_code(404);
|
||||
// dd();
|
||||
echo "404";
|
||||
die;
|
||||
|
Loading…
x
Reference in New Issue
Block a user