router update
This commit is contained in:
parent
44c4a122f7
commit
79b79bdfa5
@ -1,65 +1,65 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Mini Router
|
* Mini Router
|
||||||
* @author github.com/Xinatorus
|
* @author github.com/Xinatorus
|
||||||
* https://github.com/Xinatorus/MiniRouter
|
* https://github.com/Xinatorus/MiniRouter
|
||||||
*/
|
*/
|
||||||
class Router{
|
class Router{
|
||||||
|
|
||||||
private $routes;
|
private $routes;
|
||||||
private $defaultFunction;
|
private $defaultFunction;
|
||||||
private $params;
|
private $params;
|
||||||
private $function;
|
private $function;
|
||||||
|
|
||||||
public function __construct(){
|
public function __construct(){
|
||||||
$this->routes = [];
|
$this->routes = [];
|
||||||
$this->function = NULL;
|
$this->function = NULL;
|
||||||
$this->defaultFunction = NULL;
|
$this->defaultFunction = NULL;
|
||||||
$this->params = [];
|
$this->params = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add($method, $pattern, $function){
|
public function add($method, $pattern, $function){
|
||||||
$this->routes[] = [
|
$this->routes[] = [
|
||||||
'method' => $method,
|
'method' => $method,
|
||||||
'pattern' => $pattern,
|
'pattern' => $pattern,
|
||||||
'function' => $function,
|
'function' => $function,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function any($pattern, $function){
|
public function any($pattern, $function){
|
||||||
$this->add(NULL, $pattern, $function);
|
$this->add(NULL, $pattern, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($pattern, $function){
|
public function get($pattern, $function){
|
||||||
$this->add('GET', $pattern, $function);
|
$this->add('GET', $pattern, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function post($pattern, $function){
|
public function post($pattern, $function){
|
||||||
$this->add('POST', $pattern, $function);
|
$this->add('POST', $pattern, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function put($pattern, $function){
|
public function put($pattern, $function){
|
||||||
$this->add('PUT', $pattern, $function);
|
$this->add('PUT', $pattern, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function patch($pattern, $function){
|
public function patch($pattern, $function){
|
||||||
$this->add('PATCH', $pattern, $function);
|
$this->add('PATCH', $pattern, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete($pattern, $function){
|
public function delete($pattern, $function){
|
||||||
$this->add('DELETE', $pattern, $function);
|
$this->add('DELETE', $pattern, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDefault($function){
|
public function setDefault($function){
|
||||||
$this->defaultFunction = $function;
|
$this->defaultFunction = $function;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run($method, $uri){
|
public function run($method, $uri){
|
||||||
if(!$this->matchRoute($method, $uri)){
|
if(!$this->matchRoute($method, $uri)){
|
||||||
$this->function = $this->defaultFunction;
|
$this->function = $this->defaultFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->function !== NULL){
|
if($this->function !== NULL){
|
||||||
if(is_string($this->function)){
|
if(is_string($this->function)){
|
||||||
if(strpos($this->function, '@') !== false){
|
if(strpos($this->function, '@') !== false){
|
||||||
list($class, $function) = explode('@', $this->function);
|
list($class, $function) = explode('@', $this->function);
|
||||||
@ -71,33 +71,35 @@ class Router{
|
|||||||
}
|
}
|
||||||
}else if(class_exists($this->function)){
|
}else if(class_exists($this->function)){
|
||||||
new $this->function(...$this->params);
|
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)) {
|
}else if (is_callable($this->function)) {
|
||||||
call_user_func_array($this->function, $this->params);
|
call_user_func_array($this->function, $this->params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function matchRoute($method, $uri){
|
private function matchRoute($method, $uri){
|
||||||
foreach($this->routes as $route){
|
foreach($this->routes as $route){
|
||||||
$patternRegex = $this->convertPatternToRegex($route['pattern']);
|
$patternRegex = $this->convertPatternToRegex($route['pattern']);
|
||||||
$params = [];
|
$params = [];
|
||||||
if(($route['method'] === NULL || $route['method'] === $method) && preg_match($patternRegex, $uri, $params)){
|
if(($route['method'] === NULL || $route['method'] === $method) && preg_match($patternRegex, $uri, $params)){
|
||||||
array_shift($params);
|
array_shift($params);
|
||||||
$this->params = $params;
|
$this->params = $params;
|
||||||
$this->function = $route['function'];
|
$this->function = $route['function'];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function convertPatternToRegex($pattern){
|
private function convertPatternToRegex($pattern){
|
||||||
$regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern);
|
$regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern);
|
||||||
if ( substr($regex, -1) === '/' ) {
|
if ( substr($regex, -1) === '/' ) {
|
||||||
$regex .= '?';
|
$regex .= '?';
|
||||||
}
|
}
|
||||||
return '@^' . $regex . '$@';
|
return '@^' . $regex . '$@';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user