iwa-panda1

Manage Weather Data by International Weather Agency (Version 1)
Log | Files | Refs

commit 9f2a2116c322ae004eb5bbe3a6d337227c9e3d87
parent 40c95c72106432bfb53c32d9f319247357a6656a
Author: Friedel Schon <[email protected]>
Date:   Tue, 18 Apr 2023 17:01:04 +0200

routing

Diffstat:
Mindex.php | 117++++++++++++++++---------------------------------------------------------------
Dutils/Router.php | 60------------------------------------------------------------
Autils/router.php | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 94 insertions(+), 154 deletions(-)

diff --git a/index.php b/index.php @@ -1,137 +1,65 @@ <?php -include "utils/Router.php"; -$router = new Router\Router(); +require "utils/router.php"; -$router->get('/', function () { - include 'views/homepage.php'; -}); +$router = new Router(); + +$router->addRoute(['GET'], '/', 'views/homepage.php'); //views //add contract -$router->get('/add_contract', function () { - include 'views/add_contract.php'; -}); -$router->post('/add_contract', function () { - include 'views/add_contract.php'; -}); +$router->addRoute(['GET', 'POST'], '/add_contract', 'views/add_contract.php'); //add customer -$router->get('/add_customer', function () { - include 'views/add_customer.php'; -}); -$router->post('/add_customer', function () { - include 'views/add_customer.php'; -}); +$router->addRoute(['GET', 'POST'], '/add_customer', 'views/add_customer.php'); //add user -$router->get('/add_user', function () { - include 'views/add_user.php'; -}); -$router->post('/add_user', function () { - include 'views/add_user.php'; -}); +$router->addRoute(['GET', 'POST'], '/add_user', 'views/add_user.php'); //alter_contract -$router->get('/alter_contract ', function () { - include 'views/alter_contract .php'; -}); -$router->post('/alter_contract ', function () { - include 'views/alter_contract .php'; -}); +$router->addRoute(['GET', 'POST'], '/alter_contract ', 'views/alter_contract .php'); //alter_customer -$router->get('/alter_customer ', function () { - include 'views/alter_customer .php'; -}); -$router->post('/alter_customer ', function () { - include 'views/alter_customer .php'; -}); +$router->addRoute(['GET', 'POST'], '/alter_customer ', 'views/alter_customer .php'); //alter_user -$router->get('/alter_user', function () { - include 'views/alter_user.php'; -}); -$router->post('/alter_user', function () { - include 'views/alter_user.php'; -}); +$router->addRoute(['GET', 'POST'], '/alter_user', 'views/alter_user.php'); //dashboard -$router->get('/dashboard', function () { - include 'views/dashboard.php'; -}); +$router->addRoute(['GET'], '/dashboard', 'views/dashboard.php'); //homepage -$router->get('/homepage', function () { - include 'views/homepage.php'; -}); +$router->addRoute(['GET'], '/homepage', 'views/homepage.php'); //navbar -$router->get('/navbar', function () { - include 'views/navbar.php'; -}); +$router->addRoute(['GET'], '/navbar', 'views/navbar.php'); //search_contract -$router->get('/search_contract', function () { - include 'views/search_contract.php'; -}); -$router->post('/search_contract', function () { - include 'views/search_contract.php'; -}); +$router->addRoute(['GET', 'POST'], '/search_contract', 'views/search_contract.php'); //search_customer -$router->get('/search_customer', function () { - include 'views/search_customer.php'; -}); -$router->post('/search_customer', function () { - include 'views/search_customer.php'; -}); +$router->addRoute(['GET', 'POST'], '/search_customer', 'views/search_customer.php'); //search_data -$router->get('/search_data', function () { - include 'views/search_data.php'; -}); -$router->post('/search_data', function () { - include 'views/search_data.php'; -}); +$router->addRoute(['GET', 'POST'], '/search_data', 'views/search_data.php'); //search_user -$router->get('/search_user', function () { - include 'views/search_user.php'; -}); -$router->post('/search_user', function () { - include 'views/search_user.php'; -}); +$router->addRoute(['GET', 'POST'], '/search_user', 'views/search_user.php'); //logic //login_handler -$router->get('/login_handler', function () { - include 'logic/login_handler.php'; -}); -$router->post('/login_handler', function () { - include 'logic/login_handler.php'; -}); +$router->addRoute(['GET', 'POST'], '/login_handler', 'logic/login_handler.php'); //data inserter -$router->post('/datainserter', function () { - include 'logic/datainserter.php'; -}); +$router->addRoute(['POST'], '/datainserter', 'logic/datainserter.php'); //log-out -$router->post('/logout', function () { - include 'logic/logout.php'; -}); +$router->addRoute(['POST'], '/logout', 'logic/logout.php'); //post weather data -$router->post('/weather_data', function () { - include 'logic/postWeatherData.php'; -}); +$router->addRoute(['POST'], '/weather_data', 'logic/postWeatherData.php'); //404 -$router->addNotFoundHandler(function (){ - include 'views/404.html'; -}); - -$router->run(); +$router->route('views/404.html'); +\ No newline at end of file diff --git a/utils/Router.php b/utils/Router.php @@ -1,60 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Router; - -class Router -{ - private $handlers; - private $notFoundHandler; - private const METHOD_POST = 'POST'; - private const METHOD_GET = 'GET'; - - public function get(string $path, $handler): void - { - $this->addHandler(self::METHOD_GET, $path, $handler); - } - public function post(string $path, $handler): void - { - $this->addHandler(self::METHOD_POST, $path, $handler); - } - - public function addNotFoundHandler($handler): void - { - $this->notFoundHandler = $handler; - } - - private function addHandler(string $method, string $path, $handler): void - { - $this->handlers[$method . $path] = [ - 'path' => $path, - 'method' => $method, - 'handler' => $handler - ]; - } - public function run() - { - $requestUri = parse_url($_SERVER['REQUEST_URI']); - $requestPath = $requestUri['path']; - $method = $_SERVER['REQUEST_METHOD']; - - $callback = null; - foreach ($this->handlers as $handler){ - if ($handler['path'] === $requestPath && $method === $handler['method']){ - $callback = $handler['handler']; - } - } - - if (!$callback){ - header("HTTP/1.0 404 Not Found"); - if (!empty($this->notFoundHandler)) { - $callback = $this->notFoundHandler; - } - } - - call_user_func_array($callback, [ - array_merge($_GET, $_POST) - ]); - } -} diff --git a/utils/router.php b/utils/router.php @@ -0,0 +1,70 @@ +<?php + +class Router +{ + protected array $routes = []; + protected string $path; + + protected function match(string $match, array &$route_vars): bool + { + $route_split = explode('/', $this->path); + $match_split = explode('/', $match); + + if (sizeof($route_split) != sizeof($match_split)) { + return false; + } + + foreach ($match_split as $index => $m) { + if (str_starts_with($m, ':')) { + $route_vars[substr($m, 1)] = $route_split[$index]; + } else if ($m != $route_split[$index]) { + return false; + } + } + return true; + } + + + function addRoute(string|array $method, string $match, string|callable $func) + { + if (is_string($method)) + $method = [$method]; + + + $this->routes[] = array( + "method" => $method, + "match" => $match, + "func" => $func, + ); + } + + function includeRoute(string $path, array $_PARAM) + { + if (is_callable($path)) + return $path($_PARAM); + else + include $path; + } + + function route(string|callable $not_found_handler) + { + $this->path = $_SERVER["REQUEST_URI"]; + + if (strpos($this->path, '?')) + $this->path = explode('?', $this->path)[0]; + + $method = $_SERVER["REQUEST_METHOD"]; + + foreach ($this->routes as $route) { + if ($route["method"] != null && !in_array($method, $route["method"])) + continue; + + $vars = []; + if ($this->match($route["match"], $vars)) { + return $this->includeRoute($route["func"], $vars); + } + } + + return $this->includeRoute($not_found_handler, $vars); + } +} +\ No newline at end of file