-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
init.php
106 lines (93 loc) · 2.29 KB
/
init.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
<?php
error_reporting(E_ALL & ~E_NOTICE);
date_default_timezone_set('PRC');
define('TIME', microtime(true));
define('ROOT', str_replace("\\", "/", dirname(__FILE__)) . '/');
//__autoload方法
function i_autoload($className) {
if (is_int(strripos($className, '..'))) {
return;
}
$file = ROOT . 'lib/' . $className . '.php';
if (file_exists($file)) {
include $file;
}
}
spl_autoload_register('i_autoload');
/**
* config('name');
* config('name@file');
* config('@file');
*/
!defined('CONFIG_PATH') && define('CONFIG_PATH', ROOT . 'config/');
function config($key) {
static $configs = array();
list($key, $file) = explode('@', $key, 2);
$file = empty($file) ? 'base' : $file;
$file_name = CONFIG_PATH . $file . '.php';
//读取配置
if (empty($configs[$file]) AND file_exists($file_name)) {
$configs[$file] = @include $file_name;
}
if (func_num_args() === 2) {
$value = func_get_arg(1);
//写入配置
if (!empty($key)) {
$configs[$file] = (array) $configs[$file];
if (is_null($value)) {
unset($configs[$file][$key]);
} else {
$configs[$file][$key] = $value;
}
} else {
if (is_null($value)) {
return unlink($file_name);
} else {
$configs[$file] = $value;
}
}
file_put_contents($file_name, "<?php return " . var_export($configs[$file], true) . ";", LOCK_EX);
} else {
//返回结果
if (!empty($key)) {
return $configs[$file][$key];
}
return $configs[$file];
}
}
/**
* config('name');
* config('name@file');
* config('@file');
*/
!defined('CACHE_PATH') && define('CACHE_PATH', ROOT . 'cache/');
function cache($key, $value = null, $time = 86400) {
$file = CACHE_PATH . md5($key) . '.php';
if (is_null($value)) {
$cache = @include $file;
if ($cache['time'] > TIME) {
return $cache['data'];
} else {
@unlink($file);
}
} else {
file_put_contents($file, "<?php return " . var_export(array('data' => $value, 'time' => TIME + $time), true) . ";", LOCK_EX);
return $value;
}
}
if (!function_exists('db')) {
function db($table) {
return db::table($table);
}
}
!defined('VIEW_PATH') && define('VIEW_PATH', ROOT . 'view/');
if (!function_exists('view')) {
function view($file, $set = null) {
return view::load($file, $set = null);
}
}
if (!function_exists('_')) {
function _($str) {
return htmlspecialchars($str);
}
}