application_class.php
5.6 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
<?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
*/
/**
* application类
*
* @author Tiny
* @class App
*/
abstract class App extends Module implements Application
{
//应用名称
public $name="My Application";
//编码
public $charset = "UTF-8";
//语言
public $language = "en_us";
//应用的路径
private $basePath;
//运行时的路径
private $runtimePath;
//运行时的url
private $runtimeUrl = null;
//安全码
private $safeCode='';
/**
* 构造函数
*
* @access public
* @param mixed $config
* @return mixed
*/
public function __construct($config=null)
{
parent::__construct(null,null,$config);
Tiny::setApp($this);
if(is_string($config)) $config = require($config);
if(!defined('APP_ROOT')) define('APP_ROOT',dirname(dirname(__file__)).DIRECTORY_SEPARATOR);
$this->setBasePath(APP_ROOT.'protected'.DIRECTORY_SEPARATOR);
if(isset($config['extConfig']))ExtensionFactory::setExtConfig($config['extConfig']);
if(isset($config['urlFormat']))Url::setUrlFormat($config['urlFormat']);
else Url::setUrlFormat('get');
//应用url路径
define('APP_URL',Url::baseDir());
if(isset($config['route']))Url::setRoute($config['route']);
if(isset($config['name']))$this->name=$config['name'];
//设置一些基本路径
if(!defined('APP_URL')) define('APP_URL',Url::baseDir());
//扩展的目录
Tiny::setPath('ext',APP_CODE_ROOT.'extensions');
//项目的缓存目录
Tiny::setPath('cache',APP_ROOT.'cache');
//项目的数据目录 主要是存放 uploads thumb database等目录
$data_path = APP_ROOT.'data/';
$data_url = APP_URL.'data/';
Tiny::setPath('data',$data_path);
Tiny::setPath('data_url',$data_url);
Tiny::setPath('database',$data_path.'database/');
Tiny::setPath('database_url',$data_url.'database/');
Tiny::setPath('uploads',$data_path.'uploads/');
Tiny::setPath('uploads_url',$data_url.'uploads/');
Tiny::setPath('thumb',$data_path.'thumb/');
Tiny::setPath('thumb_url',$data_url.'thumb/');
Tiny::setPath('theme',APP_ROOT.'themes/default/');
//配制一些要引用的文件
//加载用户自己扩展的class类,默认为用户项目下的classes目录
if(isset($config['classes']))Tiny::setClasses($config['classes']);
else Tiny::setClasses('classes.*');
if(isset($config['imports']))Tiny::setImports($config['imports']);
//设置安全码
if(isset($config['safeCode']))$this->safeCode=$config['safeCode'];
//加载主题
if(isset($config['theme'])){
Tiny::app()->setTheme($config['theme']);
Tiny::setPath('theme',APP_ROOT.'themes/'.$config['theme'].'/');
}
//加载主题下的皮肤
if(isset($config['skin']))Tiny::app()->setSkin($config['skin']);
//用户项目里的错误处理的controller类
if(isset($config['errorsController']))Tiny::setErrorsController($config['errorsController']);
else Tiny::setErrorsController("Error");
//用户自定义的调试模式
if(isset($config['debug']))Tiny::setDebug($config['debug']);
if(isset($config['timezone']))
{
$timezone = $config['timezone'];
date_default_timezone_set($timezone);
}
unset($config);
}
/**
* 虚函数doRequest,让子类来实现
*
* @access public
* @return mixed
*/
abstract public function doRequest();
/**
* 应用启动运行的函数
*
* @access public
* @return mixed
*/
public function run()
{
//实现对Application的扩展
Tiny::$_iserror = true;
$appExtension = ExtensionFactory::getFactory('appExtension');
if($appExtension !== null )
{
$appExtension->before();
$this->doRequest();
$appExtension->after();
}
else $this->doRequest();
Tiny::$_iserror = false;
}
/**
* 取得安全码
*
* @access public
* @return String
*/
public function getSafeCode()
{
return $this->safeCode;
}
/**
* 设置应用的基本路径
*
* @access public
* @param mixed $basePath
* @return mixed
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* 取得应用的基本路径
*
* @access public
* @return String
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* 取得编译后的URL路径
*
* @access public
* @return mixed
*/
public function getRuntimeUrl()
{
if($this->runtimeUrl === null) $this->runtimeUrl = APP_URL.'runtime';
return $this->runtimeUrl;
}
/**
* 取得编译后的运行路径
*
* @access public
* @return mixed
*/
public function getRuntimePath()
{
if($this->runtimePath!==null) return $this->runtimePath;
else
{
if(($theme = Tiny::app()->getTheme())!==null) $this->setRuntimePath('runtime'.DIRECTORY_SEPARATOR.$theme->getName());
else
$this->setRuntimePath('runtime');
return $this->runtimePath;
}
}
/**
* 设置编译行路径
*
* @access public
* @param mixed $path
* @return mixed
*/
public function setRuntimePath($path)
{
$this->runtimePath = APP_ROOT.$path;
}
public function end()
{
exit();
}
}