53e0de50 |
<?php
namespace elanpl\L3;
|
2eec4418 |
define('_L3_ROUTE_404_NAME', '_L3_404');
|
53e0de50 |
class Router{
|
a2bd90a3 |
protected $routes; // The defined routes collection
protected $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
|
8e695a75 |
public $parsedParameters; // Parameters parsed from Request Path
public $depth; // Number of nested nodes in Request Path
|
c6173b1c |
public $routeInfo; // The RouteInfo object if the route was matched
|
a2bd90a3 |
public function __construct($routing)
|
53e0de50 |
{
|
a2bd90a3 |
$this->routes = array();
$this->routeNameIndex = array();
//Set the routing from configuration object
$routing->set($this);
|
53e0de50 |
}
|
c6173b1c |
public function add($method, $path, $result, $name = ''){
$this->routes[] = new RouteInfo($method, $path, $result, $name);
if(isset($name)&&$name!='') $this->routeNameIndex[$name] = &$this->routes[count($this->routes)-1];
|
a2bd90a3 |
return $this;
|
53e0de50 |
}
|
2eec4418 |
public function add404($result){
return $this->add('404', '', $result, _L3_ROUTE_404_NAME);
}
|
c6173b1c |
public function get($path, $result, $name = ''){
return $this->add('GET', $path, $result, $name);
|
53e0de50 |
}
|
c6173b1c |
public function post($path, $result, $name = ''){
return $this->add('POST', $path, $result, $name);
|
53e0de50 |
}
|
c6173b1c |
public function put($path, $result, $name = ''){
return $this->add('PUT', $path, $result, $name);
|
53e0de50 |
}
|
c6173b1c |
public function patch($path, $result, $name = ''){
return $this->add('PATCH', $path, $result, $name);
|
53e0de50 |
}
|
c6173b1c |
public function delete($path, $result, $name = ''){
return $this->add('DELETE', $path, $result, $name);
|
53e0de50 |
}
|
c6173b1c |
public function any($path, $result, $name = ''){
return $this->add('ANY', $path, $result, $name);
|
53e0de50 |
}
|
c6173b1c |
public function addBeforeAction($event_handler){
$this->routes[count($this->routes)-1]->addBeforeAction($event_handler);
|
a2bd90a3 |
return $this;
|
53e0de50 |
}
|
c6173b1c |
public function addAfterAction($event_handler){
$this->routes[count($this->routes)-1]->addAfterAction($event_handler);
|
a2bd90a3 |
return $this;
|
53e0de50 |
}
|
c6173b1c |
public function addAfterResult($event_handler){
$this->routes[count($this->routes)-1]->addAfterAction($event_handler);
|
a2bd90a3 |
return $this;
|
53e0de50 |
}
|
c6173b1c |
public function match($request){
|
53e0de50 |
|
a48f73c2 |
$auri = explode('/', trim($request->path, "/ \t\n\r\0\x0B"));
|
53e0de50 |
$curi = count($auri);
|
a2bd90a3 |
foreach ($this->routes as $routeInfo) {
|
53e0de50 |
|
c6173b1c |
$route = $routeInfo->path;
$method = $routeInfo->method;
if($method=='ANY' || strpos($request->method,$method)!==false){
|
53e0de50 |
$aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
//print_r($aroute);
if($curi==count($aroute)){ //compare path element count
//optimistic assumption :)
$matchResult = true;
for($i = 0; $i<$curi; $i++){
$pathPartName = trim($aroute[$i],'{}');
if($aroute[$i]==$pathPartName){
if($auri[$i]!=$pathPartName){
//echo "diffrence found";
$matchResult = false;
break;
}
}
else{ // {...} found -> catch $uri variable
$value = $auri[$i];
$valueKey = explode(':', $pathPartName);
//validation
if(isset($valueKey[1]) && $valueKey[1]=='int'){
$value = intval($value);
}
//value store...
|
a2bd90a3 |
$this->parsedParameters[$valueKey[0]] = $value;
|
53e0de50 |
}
}
if($matchResult){ // match found
|
a2bd90a3 |
$this->depth = $curi;
|
c6173b1c |
$this->routeInfo = $routeInfo;
|
a48f73c2 |
return $routeInfo->result;
|
53e0de50 |
}
}
}
}
return false;
}
|
2eec4418 |
public function getRouteInfo($name){
if(isset($this->routeNameIndex[$name]))
return $this->routeNameIndex[$name];
else
return false;
}
public function calculateRequestDepth($request){
return count(explode('/',trim($request->path, "/ \t\n\r\0\x0B")));
}
public function setMatch404($request){
if($routeInfo404 = $this->getRouteInfo(_L3_ROUTE_404_NAME)){
$this->depth = $this->calculateRequestDepth($request);
$this->routeInfo = $routeInfo404;
return $routeInfo404->result;
}
else{
return false;
}
}
|
9317964e |
public function is_Match404(){
return isset($this->routeInfo) && ($this->routeInfo->name === _L3_ROUTE_404_NAME);
}
|
a2bd90a3 |
public function link($name, $parameters){
$route = $this->routeNameIndex[$name];
|
53e0de50 |
$fields = array_keys($parameters);
$values = array_values($parameters);
array_walk($fields, function (&$item, $key){
$item = "/\{".$item."\}/";
});
return preg_replace($fields, $values, $route->Path);
}
|
c6173b1c |
public function getParameter($name){
|
a2bd90a3 |
return $this->parsedParameters[$name];
|
53e0de50 |
}
}
|