-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
76 lines (67 loc) · 2.14 KB
/
Router.php
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
<?php
namespace Router;
require 'CacheInterface.php';
class Router{
protected $url;
public function __construct(){
$this->url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
public function simpleRoute(){
$ServerArray = str_replace($_SERVER['SCRIPT_NAME'].'/','',$_SERVER['PHP_SELF']);
$ServerArray = explode('/',$ServerArray);
$ArrayLength = count($ServerArray);
$controller = $ServerArray[0];
$action = $ServerArray[1];
$parmas = [];
$count = 2;
for($count = 2;$count < $ArrayLength;$count++){
$parmas[] = $ServerArray[$count];
}
unset($ArrayLength,$count,$ServerArray);
return ['controller'=>$controller,
'action'=>$action,
'parmas'=>$parmas
];
}
public function make($MatchString,$link){
if(is_string($link))return $this->analysisByString($MatchString,$link);
if(is_object($link))return $this->analysistByFunction($MatchString,$link);
}
public function cacheCheck($MatchString,CacheInterface $cache){
if(preg_match("#{$MatchString}#",$_SERVER['PHP_SELF']) === 1 ){
$cache->cacheCheck();
}
}
protected function analysisByString($MatchString,$link){
if( preg_match("#{$MatchString}#",$_SERVER['PHP_SELF'],$match) === 1){
$ServerString = explode('@',$link);
$parmas = str_replace($_SERVER['SCRIPT_NAME'].$match[0].'/','',$_SERVER['PHP_SELF']);
$parmas = explode('/',$parmas);
$controller = $ServerString[0];
$action = $ServerString[1];
unset($ServerString,$match);
return ['controller'=>$controller,
'action'=>$action,
'parmas'=>$parmas
];
}
}
protected function analysistByFunction($MatchString,$link){
if( preg_match("#{$MatchString}#",$_SERVER['PHP_SELF'],$match) === 1){
$link();
$parmas = str_replace($_SERVER['SCRIPT_NAME'].$match[0].'/','',$_SERVER['PHP_SELF']);
$parmas = explode('/',$parmas);
unset($match);
return $parmas;
}
}
public function responseStatus($status,$callback){
if(http_response_code() == $status){
$callback();
}
}
}
$router = new Router();
$router->responseStatus(404,function(){
echo 'hello';
});