forked from hetao29/slightphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRoute.php
More file actions
65 lines (62 loc) · 3.12 KB
/
SRoute.php
File metadata and controls
65 lines (62 loc) · 3.12 KB
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
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
/**
* @package SlightPHP
*/
class SRoute{
private static $_RouteConfigFile;
private static $_Routes=array();
static function setConfigFile($file){
self::$_RouteConfigFile= $file;
self::$_Routes = array_merge(self::$_Routes,parse_ini_file(self::$_RouteConfigFile,true));
self::parse();
}
static function getConfigFile(){
return self::$_RouteConfigFile;
}
static function set(array $route){
self::$_Routes[] = $route;
self::parse();
}
private static function parse(){
$splitFlag = SlightPHP::getSplitFlag();
$splitFlag = $splitFlag{0};
foreach(self::$_Routes as $route){
$pattern = $route['pattern'];
foreach($route as $k=>$v){
if(preg_match("/:\w+/",$k)){
$pattern = str_replace("$k","($v)",$pattern);
}
}
if(preg_match_all("/$pattern/sm",!empty($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:$_SERVER['REQUEST_URI'],$_m)){
array_shift($_m);
$params = array();
if(!empty($_m)){
foreach($_m as $_m2){
$params[]=$_m2[0];
}
}
$params=implode($splitFlag,$params);
$zone = empty($route['zone']) ? SlightPHP::getDefaultZone() : $route['zone'];
$page = empty($route['page']) ? SlightPHP::getDefaultPage() : $route['page'];
$entry = empty($route['entry']) ? SlightPHP::getDefaultEntry() : $route['entry'];
$PATH_INFO = "{$zone}{$splitFlag}{$page}{$splitFlag}{$entry}{$splitFlag}{$params}";
SlightPHP::setPathInfo($PATH_INFO);
break;
}
}
}
}