forked from ideawu/iphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.php
161 lines (150 loc) · 3.48 KB
/
loader.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
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
<?php
define('APP_TIME_START', microtime(true));
include_once(dirname(__FILE__) . '/functions.php');
$AUTOLOAD_PATH = array(
dirname(__FILE__) . '/framework',
APP_PATH . '/models',
APP_PATH . '/classes',
);
function __autoload($cls){
global $AUTOLOAD_PATH;
foreach($AUTOLOAD_PATH as $dir){
$file = $dir . '/' . $cls . '.php';
if(file_exists($file)){
require_once($file);
break;
}
}
// 有很多代码会使用 class_exists(), 需要和它们兼容, 所以不能在这里 throw
# if(!class_exists($cls, false)){
# throw new Exception("Class $cls not found!");
# }
}
function include_paths(){
static $paths = array();
if(!$paths){
$path = base_path();
if(strlen($path) == 0){
$ps = array('index');
}else{
$ps = explode('/', $path);
}
$act = $ps[count($ps) - 1];
if($act == 'new'){
$act = 'create';
}
$paths[] = array(
'base' => join('/', array_slice($ps, 0, -1)),
'action' => $act,
);
$paths[] = array(
'base' => join('/', $ps),
'action' => 'index',
);
if($act != 'index'){
$paths[] = array(
'base' => join('/', $ps) . '/index',
'action' => 'index',
);
$paths[] = array(
'base' => join('/', array_slice($ps, 0, -1)) . '/index',
'action' => $act,
);
}
}
return $paths;
}
function load_controller($base, $action){
$dir = APP_PATH . '/controllers/' . $base;
$file = $dir . '.php';
#echo join(', ', array($base, $action, $file)) . "\n";
if(file_exists($file)){
include($file);
$ps = explode('/', $base);
$controller = ucfirst($ps[count($ps) - 1]);
$cls = "{$controller}Controller";
if(!class_exists($cls)){
throw new Exception("Controller $cls not found!");
}
$ins = new $cls();
$found = false;
if(method_exists($ins, $action)){
$ins->action = $action;
$found = true;
}
if($found){
Logger::trace("Controller: $file");
return $ins;
}
}
return false;
}
function route(){
foreach(include_paths() as $path){
$base = $path['base'];
$action = $path['action'];
$controller = load_controller($base, $action);
if($controller){
if(strpos($base, '/index') === strlen($base) - 6){
$base = substr($base, 0, strlen($base) - 6);
}
$controller->module = ($base == 'index')? '' : $base;
break;
}
}
if(!$controller){
$path = base_path();
Logger::trace("No route for $path!");
throw new App404Exception("No route for $path!", 404);
}
return array($base, $controller, $action);
}
function find_layout_file(){
$layout = 'layout';
$path = base_path();
if(App::$controller->layout === false){
return false;
}
if(App::$controller->layout){
$layout = App::$controller->layout;
}
foreach(App::$controller->view_path as $view_path){
$ps = explode('/', $path);
while(1){
$base = join('/', $ps);
$file = APP_PATH . "/$view_path/$base/$layout.tpl.php";
if(file_exists($file)){
Logger::trace("Layout: $file");
return $file;
}
if(!$ps){
break;
}
array_pop($ps);
}
}
return false;
}
function find_view_file(){
foreach(include_paths() as $path){
// 由 Controller 指定模板的名字
if(App::$controller->action && App::$controller->action != 'index'){
$action = App::$controller->action;
}else{
$action = $path['action'];
}
$base = $path['base'];
foreach(App::$controller->view_path as $view_path){
$dir = rtrim(APP_PATH . "/$view_path/$base", '/');
if($action == 'index'){
$file = $dir . '.tpl.php';
}else{
$file = $dir . "/$action.tpl.php";
}
if(file_exists($file)){
return $file;
}
}
}
return false;
}