From 5d522a20642d49e2f25e80044ba6bd8c1328ba62 Mon Sep 17 00:00:00 2001 From: Avior Date: Fri, 29 Mar 2019 00:10:10 +0100 Subject: [PATCH] Reworked most file to respect PSR-2 --- .phpcs.xml | 6 + composer.json | 15 ++- src/AdminPanel/Classes/AdminPanel.php | 43 +++++++ src/AdminPanel/Classes/Authentificator.php | 23 ++++ src/AdminPanel/Classes/Cache.php | 48 +++++++ src/AdminPanel/Classes/Controller.php | 36 ++++-- src/AdminPanel/Classes/Enum.php | 76 +++++------ src/AdminPanel/Functions.php | 99 +++++++++++++++ .../AdminLib/Classes/AdminController.php | 4 +- .../AdminLib/Controller/IndexController.php | 12 +- .../Index/Controller/IndexController.php | 22 ++-- .../Controller/ExampleController.php | 24 +++- src/index.php | 118 ++++++++---------- 13 files changed, 394 insertions(+), 132 deletions(-) create mode 100644 .phpcs.xml create mode 100644 src/AdminPanel/Classes/AdminPanel.php create mode 100644 src/AdminPanel/Classes/Authentificator.php create mode 100644 src/AdminPanel/Classes/Cache.php create mode 100644 src/AdminPanel/Functions.php diff --git a/.phpcs.xml b/.phpcs.xml new file mode 100644 index 0000000..39e33e8 --- /dev/null +++ b/.phpcs.xml @@ -0,0 +1,6 @@ + + The PSR-2 coding standard. + + + + diff --git a/composer.json b/composer.json index a8ef97d..efec081 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/src/AdminPanel/Classes/AdminPanel.php b/src/AdminPanel/Classes/AdminPanel.php new file mode 100644 index 0000000..5370cfa --- /dev/null +++ b/src/AdminPanel/Classes/AdminPanel.php @@ -0,0 +1,43 @@ +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(); + } +} diff --git a/src/AdminPanel/Classes/Authentificator.php b/src/AdminPanel/Classes/Authentificator.php new file mode 100644 index 0000000..fb0da5e --- /dev/null +++ b/src/AdminPanel/Classes/Authentificator.php @@ -0,0 +1,23 @@ +isLoggedIn; + } +} diff --git a/src/AdminPanel/Classes/Cache.php b/src/AdminPanel/Classes/Cache.php new file mode 100644 index 0000000..eb6b299 --- /dev/null +++ b/src/AdminPanel/Classes/Cache.php @@ -0,0 +1,48 @@ +urlArguments = $args; - } + protected $urlArguments = array(); + protected $moduleRoot = null; - protected function getUrlArguments() { - return $this->urlArguments; - } + public function setUrlArguments($args) + { + $this->urlArguments = $args; + return $this; + } + + protected function getUrlArguments() + { + return $this->urlArguments; + } + + 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 - } diff --git a/src/AdminPanel/Classes/Enum.php b/src/AdminPanel/Classes/Enum.php index 62d1534..22d13e9 100644 --- a/src/AdminPanel/Classes/Enum.php +++ b/src/AdminPanel/Classes/Enum.php @@ -2,45 +2,51 @@ namespace AdminPanel\Classes\Enum; -abstract class Enum { - /** @var null|array $constCacheArray */ - private static $constCacheArray = NULL; +abstract class Enum +{ + /** @var null|array $constCacheArray */ + private static $constCacheArray = null; - private static function getConstants() { - if (self::$constCacheArray == NULL) { - self::$constCacheArray = []; - } - $calledClass = get_called_class(); - if (!array_key_exists($calledClass, self::$constCacheArray)) { - $reflect = new ReflectionClass($calledClass); - self::$constCacheArray[$calledClass] = $reflect->getConstants(); - } - return self::$constCacheArray[$calledClass]; - } + private static function getConstants() + { + if (self::$constCacheArray == null) { + self::$constCacheArray = []; + } + $calledClass = get_called_class(); + if (!array_key_exists($calledClass, self::$constCacheArray)) { + $reflect = new ReflectionClass($calledClass); + self::$constCacheArray[$calledClass] = $reflect->getConstants(); + } + return self::$constCacheArray[$calledClass]; + } - public static function isValidName($name, $strict = false) { - $constants = self::getConstants(); + public static function isValidName($name, $strict = false) + { + $constants = self::getConstants(); - if ($strict) { - return array_key_exists($name, $constants); - } + if ($strict) { + return array_key_exists($name, $constants); + } - $keys = array_map('strtolower', array_keys($constants)); - return in_array(strtolower($name), $keys); - } + $keys = array_map('strtolower', array_keys($constants)); + return in_array(strtolower($name), $keys); + } - public static function isValidValue($value, $strict = true) { - $values = array_values(self::getConstants()); - return in_array($value, $values, $strict); - } + 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; +} +*/ diff --git a/src/AdminPanel/Functions.php b/src/AdminPanel/Functions.php new file mode 100644 index 0000000..161b261 --- /dev/null +++ b/src/AdminPanel/Functions.php @@ -0,0 +1,99 @@ +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); +} diff --git a/src/Modules/AdminLib/Classes/AdminController.php b/src/Modules/AdminLib/Classes/AdminController.php index 0021097..dce4d86 100644 --- a/src/Modules/AdminLib/Classes/AdminController.php +++ b/src/Modules/AdminLib/Classes/AdminController.php @@ -4,7 +4,7 @@ namespace AdminLib\Classes; use AdminPanel\Classes\Controller; - -class AdminController extends Controller { +class AdminController extends Controller +{ } diff --git a/src/Modules/AdminLib/Controller/IndexController.php b/src/Modules/AdminLib/Controller/IndexController.php index f3b284d..d9501c7 100644 --- a/src/Modules/AdminLib/Controller/IndexController.php +++ b/src/Modules/AdminLib/Controller/IndexController.php @@ -4,10 +4,10 @@ namespace AdminLib\Controller; use AdminLib\Classes\AdminController; -class IndexController extends AdminController { - - public function index() { - return "Hello Administrator!"; - } - +class IndexController extends AdminController +{ + public function index() + { + return "Hello Administrator!"; + } } diff --git a/src/Modules/Index/Controller/IndexController.php b/src/Modules/Index/Controller/IndexController.php index 5506984..0bf7512 100644 --- a/src/Modules/Index/Controller/IndexController.php +++ b/src/Modules/Index/Controller/IndexController.php @@ -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"]; + } } diff --git a/src/Modules/ModuleName/Controller/ExampleController.php b/src/Modules/ModuleName/Controller/ExampleController.php index c20dc64..3ca4b81 100644 --- a/src/Modules/ModuleName/Controller/ExampleController.php +++ b/src/Modules/ModuleName/Controller/ExampleController.php @@ -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"; + } + } } diff --git a/src/index.php b/src/index.php index 5006824..4d80e41 100644 --- a/src/index.php +++ b/src/index.php @@ -1,81 +1,65 @@ routes as $routeName => $routeArgs) { - $args = isset($routeArgs->args) ? $routeArgs->args : new stdClass(); - $composants = slugEqualToURI($routeArgs->path, $_SERVER["REQUEST_URI"], $args); - if($composants !== false) { - $loader->loadClass($routeArgs->controller); - $function = $routeArgs->function; - /** @var AdminPanel\Classes\Controller $controller */ - $controller = new $routeArgs->controller; - $controller->setUrlArguments($composants); - echo $controller->$function(); - die; - } - } - } + $moduleDIR = $modulesDIR . "/" . $module; + if (is_dir($moduleDIR)) { + $json = json_decode(file_get_contents($moduleDIR . "/" . strtolower($module) . ".json")); + 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; + } + } + } } - -// 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;