Public feed
#604114
<?php
class request {
private static $data_given = 0;
private static $default_c;
private static $default_m;
private static $routes = array();
public $controller_name;
public $method_name;
public $params = array();
public $data;
public $route;
public function __construct($route = ''){
$this->route = $route;
}
public function init(){
try{
$route = $this->route;
$segments = $this->parse($route);
$this->controller_name = array_shift($segments);
$this->controller_name = (empty($this->controller_name))?self::$default_c:$this->controller_name;
$this->method_name = array_shift($segments);
$this->method_name = (empty($this->method_name))?self::$default_m:$this->method_name;
$this->params = $segments;
$controller_file = CONTS .DS. $this->controller_name.EXT;
if(!file_exists($controller_file)) throw new Error(E404);
require_once LIB .DS. 'controller'.EXT;
require_once $controller_file;
if(!class_exists($this->controller_name)) throw new Error(E404);
$controller = new $this->controller_name;
$controller->set_params($this->params);
// LOAD MODEL
if($controller->autoload_model){
}
// END LOAD MODEL
if(get_parent_class($this->controller_name) != 'controller' and substr($this->method_name, 0, 1) == '_' and !method_exists($controller, $this->method_name)) throw new Error(E404);
ob_start();
call_user_method($this->method_name, $controller);
throw new Correct(ob_get_clean());
}catch(Error $e){
$this->data = '<b>Error: </b>' . $e->getMessage();
}catch(Correct $e){
$this->data = $e->getMessage();
}
}
public static function set($array){
if(self::$data_given) return;
self::$data_given = 1;
if(!empty($array['default_c'])):
self::$default_c = $array['default_c'];
unset($array['default_c']);
endif;
if(!empty($array['default_m'])):
self::$default_m = $array['default_m'];
unset($array['default_m']);
endif;
if(empty(self::$default_c) or empty(self::$default_m)) show_error('Sättimata on default kontroller ja/või meetod');
self::$routes = $array;
return;
}
oo
private function parse ($uri){
$route = $uri;
if(substr($route, 0, 1) == '/') $route = substr($route, 1, strlen($route) -1);
if(substr($route, strlen($route) -1, strlen($route)) == '/') $route = substr($route, 0, strlen($route) -1);
$uri = $route;
$segments = explode('/', $uri);
// START CUSTOM
if(count(self::$routes) > 0){
foreach(self::$routes as $route => $routed){
if(count($segments) == count(explode('/', $route))){
$custom_segments = explode('/', $route);
$matched = array();
foreach($custom_segments as $num => $custom_segment){
$custom_segment = ($custom_segment == ':num')?'([0-9]+)':$custom_segment;
$custom_segment = ($custom_segment == ':any')?'([a-z0-9_]+)':$custom_segment;
$reg_exp = '/' . $custom_segment . '/i';
if(preg_match($reg_exp, $segments[$num], $matches)){
if(!empty($matches[1])) $matched[] = $matches[1];
}else{
break;
}
}
preg_match_all('/(\$[0-9]+)/', $routed, $routed_matches);
if(count($routed_matches[1]) == count($matched)){
foreach($matched as $num => $match){
$var = $num +1;
$reg_exp = '/\$'. $var . '/';
$routed = preg_replace($reg_exp, $match, $routed);
}
}else{
$routed = '';
continue;
}
}else{
$routed = '';
continue;
}
}
}
// END CUSTOM
$uri = (!empty($routed))?$routed:$uri;
return explode('/', $uri);
}
}
?>