class Route private segment1 private segment2 private segment3 private

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
}
}