-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.php
149 lines (138 loc) · 4.89 KB
/
function.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
<?php
use elish\core\ErrorHandler;
use elish\core\Logger;
// 如果是在vendor目录下,定义BASEPATH
if (!defined('BASEPATH') && basename(dirname(__DIR__, 2)) == 'vendor') {
define('BASEPATH', realpath(dirname(__DIR__, 3)) . DIRECTORY_SEPARATOR);
}
// 时区
if (!empty($_ENV['TZ'])) {
date_default_timezone_set($_ENV['TZ']);
}
(new ErrorHandler())->register();
function pr($val)
{
echo '<pre>';
print_r($val);
echo '</pre>';
}
/**
* @param $var
* @param int $depth 最大递归深度
*/
function dump($var, int $depth = 10)
{
if (php_sapi_name() == 'cli' || defined('STDIN')) {
var_dump($var);
} else {
$output = '';
dumpInternal($var, $depth, $output);
echo '<pre>', $output, "\n</pre>";
}
}
function dumpInternal($var, $depth = 10, &$output = '', $level = 0)
{
switch (gettype($var)) {
case 'boolean':
$output .= $var ? "<small>boolean</small> <font color=\"#75507b\">true</font>" : "<small>boolean</small> <font color=\"#75507b\">false</font>";
break;
case 'integer':
$output .= "<small>int</small> <font color=\"#4e9a06\">{$var}</font>";
break;
case 'double':
$output .= "<small>float</small> <font color=\"#f57900\">{$var}</font>";
break;
case 'string':
$output .= "<small>string</small> <font color=\"#cc0000\">'" . htmlentities($var, ENT_QUOTES, 'UTF-8') . "'</font> <i>(length=" . mb_strlen($var, 'utf-8') . ")</i>";
break;
case 'resource':
$output .= '{resource}';
break;
case 'NULL':
$output .= "<font color=\"#3465a4\">null</font>";
break;
case 'unknown type':
$output .= '{unknown}';
break;
case 'array':
if ($level >= $depth) {
$output .= "<b>array</b>\n" . str_repeat(' ', ($level + 1) * 4) . "<i><font>[...]</font></i>";
} else if (empty($var)) {
$output .= "<b>array</b>\n" . str_repeat(' ', ($level + 1) * 4) . "<i><font color=\"#888a85\">empty</font></i>";
} else {
$keys = array_keys($var);
$spaces = str_repeat(' ', ($level + 1) * 4);
$output .= "<b>array</b>";
foreach ($keys as $key) {
$output .= "\n" . $spaces;
if (is_numeric($key)) {
$output .= $key;
} else {
$output .= "'{$key}'";
}
$output .= ' <font color="#888a85">=></font> ';
dumpInternal($var[$key], $depth, $output, $level + 1);
}
}
break;
case 'object':
{
$class_name = get_class($var);
if ($level >= $depth) {
$output .= "<b>object</b>(<i>" . $class_name . "</i>)\n" . str_repeat(' ', ($level + 1) * 4) . "<i><font>(...)</font></i>";
} else {
$output .= "<b>object</b>(<i>" . $class_name . "</i>)";
$spaces = str_repeat(' ', ($level + 1) * 4);
foreach ((array)$var as $key => $value) {
$key = trim($key);
$pre = substr($key, 0, strpos($key, "\0"));
if ($pre == $class_name) {
//private
$output .= "\n{$spaces}<i>private</i> '" . substr($key, strpos($key, "\0")) . "'";
} else if ($pre == '*') {
//protected
$output .= "\n{$spaces}<i>protected</i> '" . substr($key, 1) . "'";
} else {
//public
$output .= "\n{$spaces}<i>public</i> '{$key}'";
}
$output .= ' <font color=\"#888a85\">=></font> ';
dumpInternal($value, $depth, $output, $level + 1);
}
}
break;
}
}
}
/**
* 循环调用dump后die脚本
*/
function dd()
{
array_map(function ($x) {
dump($x);
}, func_get_args());
die;
}
/**
* 渲染指定模板
* @param string $__view_file__ web目录路径
* @param array $__data__ 参数
* @return string
*/
function render(string $__view_file__, array $__data__ = []): string
{
ob_start();
extract($__data__);
$__base__path__ = defined('BASEPATH') ? BASEPATH : __DIR__ . '/';
if (file_exists($__base__path__ . 'web/' . $__view_file__ . '.php')) {
require $__base__path__ . 'web/' . $__view_file__ . '.php';
} else if (file_exists($__base__path__ . 'web/' . $__view_file__ . '/index.php')) {
require $__base__path__ . 'web/' . $__view_file__ . '/index.php';
} else {
throw new RuntimeException("视图[{$__view_file__}]不存在");
}
$content = ob_get_contents();
ob_end_clean();
return $content;
}