Reworked most file to respect PSR-2

This commit is contained in:
Florian Bouillon 2019-03-29 00:10:10 +01:00
parent fb3249fa74
commit 5d522a2064
13 changed files with 394 additions and 132 deletions

6
.phpcs.xml Normal file
View 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>

View File

@ -13,6 +13,19 @@
"psr-4": { "psr-4": {
"AdminPanel\\": "src/AdminPanel", "AdminPanel\\": "src/AdminPanel",
"": "src/Modules" "": "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"
} }
} }

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

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

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

View File

@ -2,18 +2,36 @@
namespace AdminPanel\Classes; namespace AdminPanel\Classes;
class Controller { use Twig\Loader\FilesystemLoader;
use Twig\Environment;
class Controller
{
protected $urlArguments = array(); protected $urlArguments = array();
protected $moduleRoot = null;
public function setUrlArguments($args) { public function setUrlArguments($args)
{
$this->urlArguments = $args; $this->urlArguments = $args;
return $this;
} }
protected function getUrlArguments() { protected function getUrlArguments()
{
return $this->urlArguments; 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
} }

View File

@ -2,12 +2,14 @@
namespace AdminPanel\Classes\Enum; namespace AdminPanel\Classes\Enum;
abstract class Enum { abstract class Enum
{
/** @var null|array $constCacheArray */ /** @var null|array $constCacheArray */
private static $constCacheArray = NULL; private static $constCacheArray = null;
private static function getConstants() { private static function getConstants()
if (self::$constCacheArray == NULL) { {
if (self::$constCacheArray == null) {
self::$constCacheArray = []; self::$constCacheArray = [];
} }
$calledClass = get_called_class(); $calledClass = get_called_class();
@ -18,7 +20,8 @@ abstract class Enum {
return self::$constCacheArray[$calledClass]; return self::$constCacheArray[$calledClass];
} }
public static function isValidName($name, $strict = false) { public static function isValidName($name, $strict = false)
{
$constants = self::getConstants(); $constants = self::getConstants();
if ($strict) { if ($strict) {
@ -29,18 +32,21 @@ abstract class Enum {
return in_array(strtolower($name), $keys); 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()); $values = array_values(self::getConstants());
return in_array($value, $values, $strict); return in_array($value, $values, $strict);
} }
} }
// abstract class DaysOfWeek extends BasicEnum { /*
// const Sunday = 0; abstract class DaysOfWeek extends Enum {
// const Monday = 1; const Sunday = 0;
// const Tuesday = 2; const Monday = 1;
// const Wednesday = 3; const Tuesday = 2;
// const Thursday = 4; const Wednesday = 3;
// const Friday = 5; const Thursday = 4;
// const Saturday = 6; const Friday = 5;
// } const Saturday = 6;
}
*/

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

View File

@ -4,7 +4,7 @@ namespace AdminLib\Classes;
use AdminPanel\Classes\Controller; use AdminPanel\Classes\Controller;
class AdminController extends Controller
class AdminController extends Controller { {
} }

View File

@ -4,10 +4,10 @@ namespace AdminLib\Controller;
use AdminLib\Classes\AdminController; use AdminLib\Classes\AdminController;
class IndexController extends AdminController { class IndexController extends AdminController
{
public function index() { public function index()
{
return "Hello Administrator!"; return "Hello Administrator!";
} }
} }

View File

@ -3,15 +3,21 @@
namespace Index\Controller; namespace Index\Controller;
use AdminPanel\Classes\Controller; use AdminPanel\Classes\Controller;
use AdminPanel\Classes\AdminPanel;
class IndexController extends Controller { class IndexController extends Controller
{
public function index() { public function index()
return "hello world!"; {
return AdminPanel::getInstance()->getTwig()->render("@Index/index.twig", [
"title" => "Coming Soon"
]);
// return file_get_contents($this->getModuleRoot() . "/index.html");
} }
public function test() { public function test()
var_dump($this->getUrlArguments()); {
return "test working!"; return "hello " . $this->getUrlArguments()["slug"];
} }
} }

View File

@ -3,11 +3,27 @@
namespace ModuleName\Controller; namespace ModuleName\Controller;
use AdminPanel\Classes\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() { public function index()
return "hello Controller!"; {
return "Hellow Example Controller!";
}
public function isLoggedIn()
{
if (Authentificator::getInstance()->isLoggedIn()) {
return "test is false!";
} else {
return "test is true";
}
} }
} }

View File

@ -1,19 +1,29 @@
<?php <?php
use AdminPanel\Classes\AdminPanel;
use AdminPanel\Classes\Cache;
session_start();
ini_set('display_errors', 'On'); ini_set('display_errors', 'On');
define("ROOT", __DIR__);
use AdminPanel\Classes\Controller;
/** @var Composer\Autoload\ClassLoader $loader */ /** @var Composer\Autoload\ClassLoader $loader */
$loader = require_once ROOT . "/../vendor/autoload.php"; $loader = require_once __DIR__ . "/../vendor/autoload.php";
// var_dump($_SERVER["REQUEST_URI"] . "/"); // var_dump($_SERVER["REQUEST_URI"] . "/");
// $_SERVER["REQUEST_URI"] = "/test/"; // $_SERVER["REQUEST_URI"] = "/test/";
//get all dirs //get all dirs
$modulesDIR = ROOT . "/Modules"; $modulesDIR = __DIR__ . "/Modules";
$modules = array_diff(scandir($modulesDIR), array('..', '.')); $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 */ /** @var string $module */
foreach ($modules as $module) { foreach ($modules as $module) {
$moduleDIR = $modulesDIR . "/" . $module; $moduleDIR = $modulesDIR . "/" . $module;
@ -22,12 +32,26 @@ foreach ($modules as $module) {
foreach ($json->routes as $routeName => $routeArgs) { foreach ($json->routes as $routeName => $routeArgs) {
$args = isset($routeArgs->args) ? $routeArgs->args : new stdClass(); $args = isset($routeArgs->args) ? $routeArgs->args : new stdClass();
$composants = slugEqualToURI($routeArgs->path, $_SERVER["REQUEST_URI"], $args); $composants = slugEqualToURI($routeArgs->path, $_SERVER["REQUEST_URI"], $args);
// dump($composants !== false);
if ($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); $loader->loadClass($routeArgs->controller);
$function = $routeArgs->function; $function = $routeArgs->function;
// dump($function);
/** @var AdminPanel\Classes\Controller $controller */ /** @var AdminPanel\Classes\Controller $controller */
$controller = new $routeArgs->controller; $controller = new $routeArgs->controller;
$controller->setUrlArguments($composants); $controller->setUrlArguments($composants);
$controller->setModuleRoot($moduleDIR);
// if(isset($json->templateFolder)) $controller->loadTwig($json->templateFolder);
echo $controller->$function(); echo $controller->$function();
die; die;
} }
@ -35,47 +59,7 @@ foreach ($modules as $module) {
} }
} }
http_response_code(404);
// echo Controller::example(); // dd();
echo "404";
// require_once ROOT . "/system/router.php"; die;
// 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;
}