class Router
{
private $chain = array();
public function run($route)
{
uasort($this->chain, function ($a, $b) {
$left = $a['priority'];
$right = $b['priority'];
if ($left == $right) {
return 0;
} else {
return $left < $right ? -1 : 1;
}
}
);
foreach($this->chain as $pattern => $r){
if(preg_match($pattern, $route)){
call_user_func($r['controller']);
}
}
}
public function addRoute($pattern, Callable $callback, $priority = 0)
{
$this->chain[$pattern] = array(
'controller' => $callback,
'priority' => $priority
);
return $this;
}
}