webapp_class.php
5.66 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* web应用类
*
* @author Tiny
* @class WebApp
*/
class WebApp extends App
{
//应用对应的控制器
public $defaultController = 'index';
//主要布局
public $layout = 'index';
//控制器明映射
public $controllerMap=array();
//控制器路径
private $controllerPath;
//视图路径
private $viewPath;
//布局路径
private $layoutPath;
//控制器
public $controller;
//主题
private $theme;
//皮肤
private $skin;
//共存数据
private $datas = array();
//请求堆栈
private static $requestStack=array();
/**
* 构造函数
*
* @access public
* @param mixed $config
*/
public function __construct($config=null)
{
parent::__construct($config);
}
/**
* 压入堆栈
*
* @access public
* @param mixed $key
* @return void
*/
public function pushRequestStack($key)
{
self::$requestStack[$key] = $key;
}
/**
* 取出堆栈的内容
*
* @access public
* @param mixed $key
* @return mixed
*/
public function popRequestStack($key)
{
if(isset(self::$requestStack[$key])) return self::$requestStack[$key];
else false;
}
/**
* 修改当前的控制器
*
* @access public
* @param mixed $controller
* @return mixed
*/
public function setController($controller)
{
$this->controller = $controller;
}
/**
* 取得当前的控制器
*
* @access public
* @return mixed
*/
public function getController()
{
return $this->controller;
}
/**
* 处理所有请求
*
* @access public
* @return mixed
*/
public function doRequest()
{
Url::urlReWrite();
$this->runController();
}
/**
* 创建控制器
*
* @access public
* @return mixed
*/
public function createController()
{
$controllerName = Req::args('con')!==null?ucfirst(Req::args('con')):$this->defaultController;
$controllerClass = $controllerName.'Controller';
$widgetClass = $controllerName.'Widget';
if(class_exists($controllerClass))
{
return new $controllerClass(strtolower($controllerName),$this);
}
else if(class_exists($widgetClass))
{
return new $widgetClass($controllerName,$this);
}
else if(Tiny::getErrorsController()!==null)
{
$errorsController = Tiny::getErrorsController();
return $errorsController;
}
else
{
return new Controller($controllerName,$this);
}
}
/**
* 运行控制器
*
* @access public
* @return mixed
*/
public function runController()
{
$this->controller = $this->createController();
$this->controller->run();
}
/**
* 取得当前主题类
*
* @access public
* @return mixed
*/
public function getTheme()
{
if(is_string($this->theme)) $this->theme = $this->getThemeManager()->getTheme($this->theme);
return $this->theme;
}
/**
* 设定主题的名称
*
* @access public
* @param mixed $value
* @return mixed
*/
public function setTheme($value)
{
$this->theme = $value;
}
/**
* 设定皮肤的名称
*
* @access public
* @param mixed $skin
* @return mixed
*/
public function setSkin($skin)
{
$this->skin = $skin;
}
/**
* 取得皮肤
*
* @access public
* @return mixed
*/
public function getSkin()
{
return $this->skin;
}
/**
* 取得主题管理类
*
* @access public
* @return mixed
*/
public function getThemeManager()
{
return ThemeManager::getInstance();
}
/**
* 得到视图路径
*
* @access public
* @return mixed
*/
public function getViewPath()
{
if($this->viewPath !== null) return $this->viewPath;
else return $this->viewPath = $this->getBasePath().DIRECTORY_SEPARATOR.'views';
}
/**
* 得到布局路径
*
* @access public
* @return mixed
*/
public function getLayoutPath()
{
$this->getViewPath().DIRECTORY_SEPARATOR.'layouts';
}
/**
* 最得数据
*
* @access public
* @param mixed $name
* @return mixed
*/
public function getDatas($name=null)
{
if($name ===null) return $this->datas;
else if(isset($this->datas[$name])) return $this->datas[$name];
}
/**
* 设定数据,app中可访问的
*
* @access public
* @param mixed $name
* @param string $value
* @return mixed
*/
public function setDatas($name,$value='')
{
if(is_array($name)) $this->datas = array_merge($this->datas,$name);
else $this->datas[$name] = $value;
}
/**
* 创建令牌
*
* @access public
* @param string $key
* @return mixed
*/
public function getToken($key=''){
$key = "tiny_token_".$key;
$token = Session::get($key);
if($token == null || strlen($token)!=32){
$token = CHash::random(32,'char');
Session::set($key,$token);
}
return $token;
}
/**
* 验证令牌并销毁
*
* @access public
* @param string $key
* @return mixed
*/
public function checkToken($key='')
{
$key = "tiny_token_".$key;
$token = Req::args($key);
$rel_token = Session::get($key);
Session::clear($key);
return ($token !=null && $token==$rel_token);
}
}