From 79b79bdfa5e0d257e96fa71b8b4e40321362a74d Mon Sep 17 00:00:00 2001 From: xinatorus Date: Tue, 21 Apr 2020 14:04:01 +0200 Subject: [PATCH] router update --- library/Router.php | 146 +++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 72 deletions(-) diff --git a/library/Router.php b/library/Router.php index b4ddc6a..cced691 100644 --- a/library/Router.php +++ b/library/Router.php @@ -1,65 +1,65 @@ routes = []; - $this->function = NULL; - $this->defaultFunction = NULL; - $this->params = []; - } + public function __construct(){ + $this->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 add($method, $pattern, $function){ + $this->routes[] = [ + 'method' => $method, + 'pattern' => $pattern, + 'function' => $function, + ]; + } - public function any($pattern, $function){ - $this->add(NULL, $pattern, $function); - } + public function any($pattern, $function){ + $this->add(NULL, $pattern, $function); + } - public function get($pattern, $function){ - $this->add('GET', $pattern, $function); - } + public function get($pattern, $function){ + $this->add('GET', $pattern, $function); + } - public function post($pattern, $function){ - $this->add('POST', $pattern, $function); - } + public function post($pattern, $function){ + $this->add('POST', $pattern, $function); + } - public function put($pattern, $function){ - $this->add('PUT', $pattern, $function); - } + public function put($pattern, $function){ + $this->add('PUT', $pattern, $function); + } - public function patch($pattern, $function){ - $this->add('PATCH', $pattern, $function); - } + public function patch($pattern, $function){ + $this->add('PATCH', $pattern, $function); + } - public function delete($pattern, $function){ - $this->add('DELETE', $pattern, $function); - } + public function delete($pattern, $function){ + $this->add('DELETE', $pattern, $function); + } - public function setDefault($function){ - $this->defaultFunction = $function; - } + public function setDefault($function){ + $this->defaultFunction = $function; + } - public function run($method, $uri){ - if(!$this->matchRoute($method, $uri)){ - $this->function = $this->defaultFunction; - } + public function run($method, $uri){ + if(!$this->matchRoute($method, $uri)){ + $this->function = $this->defaultFunction; + } - if($this->function !== NULL){ + if($this->function !== NULL){ if(is_string($this->function)){ if(strpos($this->function, '@') !== false){ list($class, $function) = explode('@', $this->function); @@ -71,33 +71,35 @@ class Router{ } }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); } - }else if (is_callable($this->function)) { - call_user_func_array($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; - } - } + 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; - } + return false; + } - private function convertPatternToRegex($pattern){ - $regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern); - if ( substr($regex, -1) === '/' ) { - $regex .= '?'; - } - return '@^' . $regex . '$@'; - } + private function convertPatternToRegex($pattern){ + $regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern); + if ( substr($regex, -1) === '/' ) { + $regex .= '?'; + } + return '@^' . $regex . '$@'; + } }