PHP_SMART_HOME_V3/library/Router.php

107 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2020-04-20 19:49:30 +00:00
<?php
/**
2020-04-21 12:04:01 +00:00
* Mini Router
* @author github.com/Xinatorus
* https://github.com/Xinatorus/MiniRouter
*/
2020-04-20 19:49:30 +00:00
class Router{
2020-04-21 12:04:01 +00:00
private $routes;
private $defaultFunction;
private $params;
private $function;
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function __construct(){
$this->routes = [];
$this->function = NULL;
$this->defaultFunction = NULL;
$this->params = [];
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function add($method, $pattern, $function){
$this->routes[] = [
'method' => $method,
'pattern' => $pattern,
'function' => $function,
];
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function any($pattern, $function){
$this->add(NULL, $pattern, $function);
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function get($pattern, $function){
$this->add('GET', $pattern, $function);
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function post($pattern, $function){
$this->add('POST', $pattern, $function);
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function put($pattern, $function){
$this->add('PUT', $pattern, $function);
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function patch($pattern, $function){
$this->add('PATCH', $pattern, $function);
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function delete($pattern, $function){
$this->add('DELETE', $pattern, $function);
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function setDefault($function){
$this->defaultFunction = $function;
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
public function run($method, $uri){
if(!$this->matchRoute($method, $uri)){
$this->function = $this->defaultFunction;
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
if($this->function !== NULL){
2020-05-22 20:15:49 +00:00
Debugger::flag('execution');
2020-04-21 12:01:31 +00:00
if(is_string($this->function)){
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);
2020-04-21 12:04:01 +00:00
}else if (is_callable($this->function)) {
call_user_func_array($this->function, $this->params);
2020-04-21 12:01:31 +00:00
}
2020-04-21 12:04:01 +00:00
}else if (is_callable($this->function)) {
call_user_func_array($this->function, $this->params);
}
}
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
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;
}
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
return false;
}
2020-04-20 19:49:30 +00:00
2020-04-21 12:04:01 +00:00
private function convertPatternToRegex($pattern){
$regex = preg_replace('@{\w+}@', '([\w\d\-\.,]+)', $pattern);
if ( substr($regex, -1) === '/' ) {
$regex .= '?';
}
return '@^' . $regex . '$@';
}
2020-04-20 19:49:30 +00:00
}