class Route {
private $segment1;
private $segment2;
private $segment3;
private $controller_path;
private $controller;
private $action;
private $notfound = false;
public function getRoute($route) {
$query = substr($route, 1);
$ex = explode('?', $query);
$query = $ex[0];
$rules = '';
$rules_dir = DATA_PATH.'/rules';
$dir = opendir($rules_dir);
while($f = readdir($dir)) {
if (strstr($f, '.rules'))
$rules .= trim(file_get_contents($rules_dir .'/'. $f)) . PHP_EOL;
}
$rules .= '([a-z0-9_\-]*)([\.a-z0-9]*)#segment1=$1'. PHP_EOL;
$rules .= '([a-z0-9_\-]*)/([a-z0-9_\-]*)([\.a-z0-9]*)#segment1=$1&segment2=$2'. PHP_EOL;
$rules .= '([a-z0-9_\-]*)/([a-z0-9_\-]*)/([a-z0-9_\-]*)([\.a-z0-9]*)#segment1=$1&segment2=$2&segment3=$3'. PHP_EOL;
$rules = explode("\n", $rules);
foreach($rules as $rule) {
$rule = trim($rule);
if (strpos($rule, '#') == 0 or $rule == '')
continue;
$ex = explode('#', $rule);
if (preg_match('~^'. $ex[0] .'$~', $query)) {
$result = preg_replace('~^'. $ex[0] .'$~', $ex[1], $query, 1);
$vars = explode('&', $result);
foreach($vars as $var) {
$_ex = explode('=', $var);
$_GET[$_ex[0]] = $_ex[1];
}
break;
}
}
}
public function startRoute() {
if ($this->check_segments()) {
// если нет сегментов
if (empty($this->segment1)) {
$file = CONTROLLER_PATH . '/' . DEFAULT_CONTROLLER . '.php';
if (file_exists($file)) {
$this->controller_path = $file;
$this->controller = DEFAULT_CONTROLLER;
$this->action = DEFAULT_ACTION;
} else
$this->notfound = true;
}
// если указаны все 3 сегмента
if (! empty($this->segment1) && ! empty($this->segment2) && ! empty($this->segment3)) {
$file = CONTROLLER_PATH . '/' . $this->segment1 .'_' . $this->segment2 . '.php';
if (file_exists($file)) {
$this->controller_path = $file;
$this->controller = $this->segment1 . '_' . $this->segment2;
$this->action = $this->segment3;
} else
$this->notfound = true;
// если указаны 2 сегмента
} elseif (! empty($this->segment1) && ! empty($this->segment2)) {
$file = CONTROLLER_PATH . '/' . $this->segment1 .'_' . $this->segment2 . '.php';
$file_one = CONTROLLER_PATH . '/' . $this->segment1 . '.php';
if (file_exists($file)) {
$this->controller_path = $file;
$this->controller = $this->segment1 . '_' . $this->segment2;
$this->action = null;
} elseif (file_exists($file_one)) {
$this->controller_path = $file_one;
$this->controller = $this->segment1;
$this->action = $this->segment2;
} else
$this->notfound = true;
// если указан только 1 сегмент
} elseif (! empty($this->segment1)) {
$file = CONTROLLER_PATH . '/' . $this->segment1 . '.php';
if (file_exists($file)) {
$this->controller_path = $file;
$this->controller = $this->segment1;
$this->action = null;
} else
$this->notfound = true;
}
$this->runRoute();
}
}
public function runRoute() {
if ($this->notfound) {
$class = DEFAULT_CONTROLLER;
$action = 'notfound';
$this->controller_path = CONTROLLER_PATH . '/' . $class . '.php';
} else {
$class = $this->controller;
$action = $this->action;
}
if (! $action)
$action = DEFAULT_ACTION;
require $this->controller_path;
if (! class_exists($class)) {
$class = DEFAULT_CONTROLLER;
$action = 'notfound';
}
$controller = new $class;
if (! method_exists($controller, $action)) {
if ($class != DEFAULT_CONTROLLER) {
$class = DEFAULT_CONTROLLER;
require CONTROLLER . '/' . $class . '.php';
$controller = new $class;
}
$action = 'notfound';
}
$controller->$action();
}
protected function check_segments() {
$check_segments = true;
if (! empty($_GET['segment1'])) {
if (preg_match('~^[0-9a-z_-]*$~', $_GET['segment1']))
$this->segment1 = $_GET['segment1'];
else
$check_segments = false;
}
if (! empty($_GET['segment2'])) {
if (preg_match('~^[0-9a-z_-]*$~', $_GET['segment2']))
$this->segment2 = $_GET['segment2'];
else
$check_segments = false;
}
if (! empty($_GET['segment3'])) {
if (preg_match('~^[0-9a-z_-]*$~', $_GET['segment3']))
$this->segment3 = $_GET['segment3'];
else
$check_segments = false;
}
if (! $check_segments)
exit('Ошибка регистрации сегментов!');
return true;
}
}