From 74c642650035037b9060c19bb40eaf895f3cd897 Mon Sep 17 00:00:00 2001 From: xinatorus Date: Mon, 20 Apr 2020 21:49:30 +0200 Subject: [PATCH] new project structure --- .editorconfig | 4 +- README.md | 31 ++++++++++++ app/Bootstrap.php | 6 +++ app/Routes.php | 9 ++++ library/ApiController.php | 0 library/Controller.php | 0 library/Router.php | 101 ++++++++++++++++++++++++++++++++++++++ public/.htaccess | 0 public/index.php | 2 + 9 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 app/Bootstrap.php create mode 100644 app/Routes.php create mode 100644 library/ApiController.php create mode 100644 library/Controller.php create mode 100644 library/Router.php create mode 100644 public/.htaccess create mode 100644 public/index.php diff --git a/.editorconfig b/.editorconfig index 21affa7..fe9f766 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,9 +3,9 @@ root = true [*] -tab_width = 4 +tab_width = 3 -[*.{php,phpt,inc}] +[*.{php,phpt,inc,phtml}] charset = utf-8 end_of_line = lf indent_size = 2 diff --git a/README.md b/README.md index 6141a3c..8854655 100644 --- a/README.md +++ b/README.md @@ -135,3 +135,34 @@ Distributed under the MIT License. See `LICENSE` for more information. Project Link: [https://github.com/GamerClassN7/Smart_Home/](https://github.com/GamerClassN7/Smart_Home/)

Tags (ignore): ESP32 ESP12 ESP08 ESP01 ESP Smart Home Automation System PWA PHP HTML JS DHT11 DHT22 + +## Folder structure +- /app + - /controllers + - UserController.php + - /models + - UserModal.php + - /views + - /layouts + - default.phtml + - /templates + - /components + - /pages + - Bootstrap.php + - Db.php + - Routes.php +- /library + - /types + - Units.php + - /vendor + - Controller.php + - Db.php + - Router.php +- config + - config.php / +- public + - /css + - /images + - /js + - .htaccess + - index.php diff --git a/app/Bootstrap.php b/app/Bootstrap.php new file mode 100644 index 0000000..a307fb9 --- /dev/null +++ b/app/Bootstrap.php @@ -0,0 +1,6 @@ +setDefault(function(){ + echo '404'; +}); + +$router->run($_SERVER['REQUEST_METHOD'], '/'.$_GET['url']); diff --git a/library/ApiController.php b/library/ApiController.php new file mode 100644 index 0000000..e69de29 diff --git a/library/Controller.php b/library/Controller.php new file mode 100644 index 0000000..e69de29 diff --git a/library/Router.php b/library/Router.php new file mode 100644 index 0000000..ee18504 --- /dev/null +++ b/library/Router.php @@ -0,0 +1,101 @@ +routes = []; + $this->function = NULL; + $this->defaultFunction = NULL; + $this->params = []; + } + + public function add($method, $pattern, $function){ + $this->routes[] = [ + 'method' => $method, + 'pattern' => $pattern, + 'function' => $function, + ]; + } + + public function any($pattern, $function){ + $this->add(NULL, $pattern, $function); + } + + public function get($pattern, $function){ + $this->add('GET', $pattern, $function); + } + + public function post($pattern, $function){ + $this->add('POST', $pattern, $function); + } + + public function put($pattern, $function){ + $this->add('PUT', $pattern, $function); + } + + public function patch($pattern, $function){ + $this->add('PATCH', $pattern, $function); + } + + public function delete($pattern, $function){ + $this->add('DELETE', $pattern, $function); + } + + public function setDefault($function){ + $this->defaultFunction = $function; + } + + public function run($method, $uri){ + if(!$this->matchRoute($method, $uri)){ + $this->function = $this->defaultFunction; + } + + if($this->function !== NULL){ + if(strpos($this->function, '@') !== false){ + list($class, $function) = explode('@', $this->function); + $method = new ReflectionMethod($class, $function); + if($method->isStatic()){ + call_user_func_array([$class, $function], $this->params); + }else{ + call_user_func_array([new $class, $function], $this->params); + } + }else if(class_exists($this->function)){ + new $this->function(...$this->params); + }else if (is_callable($this->function)) { + call_user_func_array($this->function, $this->params); + } + } + } + + private function matchRoute($method, $uri){ + foreach($this->routes as $route){ + $patternRegex = $this->convertPatternToRegex($route['pattern']); + $params = []; + if(($route['method'] === NULL || $route['method'] === $method) && preg_match($patternRegex, $uri, $params)){ + array_shift($params); + $this->params = $params; + $this->function = $route['function']; + return true; + } + } + + return false; + } + + private function convertPatternToRegex($pattern){ + $regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern); + if ( substr($regex, -1) === '/' ) { + $regex .= '?'; + } + return '@^' . $regex . '$@'; + } +} diff --git a/public/.htaccess b/public/.htaccess new file mode 100644 index 0000000..e69de29 diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..4fef531 --- /dev/null +++ b/public/index.php @@ -0,0 +1,2 @@ +