-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
152 lines (116 loc) · 4.6 KB
/
index.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
<?php
define('WWW_ROOT', dirname(__FILE__));
require(WWW_ROOT . '/smarty/Smarty.class.php');
require(WWW_ROOT . '/php-simulation-env/log/Log.class.php');
require(WWW_ROOT . '/php-simulation-env/util/Util.class.php');
require(WWW_ROOT . '/php-simulation-env/mock-data/Mock.class.php');
require(WWW_ROOT . '/php-simulation-env/rewrite/Rewrite.class.php');
function initSmarty($config) {
$smarty = new Smarty();
$smarty->setTemplateDir($config['smarty']['template_dir']);
$smarty->setConfigDir($config['smarty']['config_dir']);
$smarty->setLeftDelimiter($config['smarty']['left_delimiter']);
$smarty->setRightDelimiter($config['smarty']['right_delimiter']);
foreach($config['smarty']['plugins_dir'] as $pluginDir) {
$smarty->addPluginsDir($pluginDir);
}
return $smarty;
}
class TplRewirteHandle implements RewriteHandle {
private $_smarty = null;
private $_charset = 'utf-8';
public function __construct($smarty, $charset) {
$this->_smarty = $smarty;
$this->_charset = $charset;
}
public function process($file) {
$file = str_replace(WWW_ROOT . '/template/', '', $file); // rewrite ...
$file = str_replace('template/', '', $file); // template ...
header('Content-Type: text/html;charset=' . $this->_charset);
$this->_smarty->assign(Mock::getData($file));
$this->_smarty->display($file);
}
}
class JsonRewriteHandle implements RewriteHandle {
private $_charset = null;
public function __construct($charset) {
$this->_charset = $charset;
}
public function process($file) {
Log::getLogger()->info('JsonRewriteHandle file: %s', $file);
if (!file_exists($file)) {
Log::getLogger()->warn('JSON file<%s> not exists.', $file);
return;
}
$content = file_get_contents($file);
if (($this->_charset != 'utf-8' || $this->_charset != 'utf8') && Util::isUtf8($content)) {
Log::getLogger()->info('JsonRewriteHandle convert utf-8 to %s', $this->_charset);
$content = Util::convertEncoding($content, 'utf-8', $this->_charset);
}
Log::getLogger()->info('JsonRewriteHandle put the content of JSON file: %s, charset: %s', $file, $this->_charset);
header('Content-Type: text/json; charset='.$this->_charset);
echo $content;
}
}
function init($config, $smarty) {
// log init
Log::getLogger(array(
'fd' => WWW_ROOT . '/app.log',
'level' => Log::ALL,
'requestUrl' => $_SERVER['REQUEST_URI']
));
// mock init
Mock::init(WWW_ROOT, $config['encoding']);
// rewrite init
$rewrite = new Rewrite(WWW_ROOT . '/server-conf', $config['encoding']);
$rewrite->addRule(Rule::REWRITE, '@/static/.*@', '$&');
$rewrite->addRule(Rule::REWRITE, '@/favicon.ico$@', 'static/favicon.ico');
foreach(glob(WWW_ROOT . '/server-conf/**') as $configFile) {
$configFile = basename($configFile);
$rewrite->addConfigFile($configFile);
}
$rewrite->addRule(Rule::REWRITE, '@^/?$@', 'welcome.php');
$rewrite->addRewriteHandle('tpl', new TplRewirteHandle($smarty, $config['encoding']));
$rewrite->addRewriteHandle('json', new JsonRewriteHandle($config['encoding']));
$rewrite->dispatch();
}
function getConfig($uriSplit) {
$config = array(
'namespace' => $uriSplit[0],
'encoding' => 'utf-8',
'smarty' => array(
'left_delimiter' => '{%',
'right_delimiter' => '%}',
'template_dir' => './template',
'plugins_dir' => array(
'./plugin'
),
'config_dir' => './config'
)
);
$smartyConfigFile = realpath(WWW_ROOT . '/smarty.conf');
if ($smartyConfigFile) {
$smartyConfig = parse_ini_file($smartyConfigFile);
if (isset($smartyConfig['encoding'])) {
//ugly, .... @TODO
$config['encoding'] = $smartyConfig['encoding'];
unset($smartyConfig['encoding']);
}
$config['smarty'] = array_merge($config['smarty'], (array) $smartyConfig);
}
return $config;
}
function routing() {
$requestUri = $_SERVER['REQUEST_URI'];
$requestUri = preg_replace('@\?.*$@', '', $requestUri);
$requestUri = substr($requestUri, 1);
$uriSplit = explode('/', $requestUri);
$config = getConfig($uriSplit);
$smarty = initSmarty($config);
init($config, $smarty);
$tpl = $requestUri . '.tpl';
header('Content-Type: text/html;charset=' . $config['encoding']);
$smarty->assign((array)Mock::getData($tpl));
$smarty->display($tpl);
}
routing();