This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplePrinter.php
176 lines (139 loc) · 4.83 KB
/
simplePrinter.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
if (!headers_sent()) {
header('content-Type:text/html;charset=utf-8');
}
static $__print_style_out_mark = false;
if (!function_exists('spr')) {
/**
* spr === print_r 但支持传入多个参数
* @internal param string $value [description]
* @return void [type] [description]
*/
function spr()
{
$param = func_get_args();
$outString = '';
$last = array_pop($param);
___getFunctionCalledPosition();
foreach ($param as $key => $value) {
$outString .= __dump($value, false);
}
$exit = false;
if ($last === -4) {
$exit = true;
} else {
$outString .= __dump($last, false);
}
___output($outString, $exit);
}
}
if (!function_exists('svd')) {
/**
* 多个打印 === var_dump
* @return void [type] [description]
*/
function svd()
{
$param = func_get_args();
$last = array_pop($param);
$outString = '';
___getFunctionCalledPosition();
foreach ($param as $value) {
$outString .= __dump($value);
}
$exit = false;
if ($last === -4) {
$exit = true;
} else {
$outString .= __dump($last);
}
___output($outString, $exit);
}
}
function ___output($string, $exit = false)
{
if ($exit) {
exit($string);
}
echo $string;
}
function __dump($data, $hasType = true)
{
# 使用系统函数打印
$outString = ___getSystemPrintData($data, $hasType);
$outString = trim($outString);
$output = '<!-- output print start -->';
$output .= "\n<div class=\"general-print-box general-print-font general-print-shadow\">\n%s\n</div>\n";
$output .= '<!-- output print end -->';
if (preg_match('/^<pre[\s]*/i', $outString) !== 1) {
$outString = "<pre>$outString</pre>";
}
$output = sprintf($output, $outString);
return $output;
}
/**
* @param string $var 要查找的变量
* @param array $scope 要搜寻的范围
* @return mixed
*/
function get_variable_name(&$var, array $scope = null)
{
$scope = $scope ?: $GLOBALS; // 如果没有范围则在globals中找寻
// 因有可能有相同值的变量,因此先将当前变量的值保存到一个临时变量中,
// 然后再对原变量赋唯一值,以便查找出变量的名称,找到名字后,将临时变量的值重新赋值到原变量
$tmp = $var;
$var = 'tmp_value_' . mt_rand();
$name = array_search($var, $scope, true); // 根据值查找变量名称
$var = $tmp;
return $name;
}
function ___getSystemPrintData($data, $hasType = true)
{
$fun = $hasType ? 'var_dump' : 'print_r';
ob_start();
$fun($data);
$string = ob_get_clean();
$string = str_replace("=>\n ", '=>', $string);
return $string;
}
function ___getFunctionCalledPosition($backNum = 2, $separator = '#1', $return = false)
{
global $__print_style_out_mark;
ob_start();
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $backNum);
$positionInfo = ob_get_clean();
$positionInfo = strstr($positionInfo, $separator);
// if (!$phpGt54) {
// $positionInfo = strstr($positionInfo, ' called at ');
// $positionInfo = strstr($positionInfo, '#2 ', true);
// }
$positionInfo = trim(str_replace(array("\n", $separator), '', $positionInfo));
$positionInfo = str_replace('\\', '/', $positionInfo);
$root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
$positionInfo = str_replace($root, '<-ROOT->', $positionInfo);
if ($return) {
return $positionInfo;
}
if (!$__print_style_out_mark) {
$__print_style_out_mark = true;
echo ___output_css();//,loadJQuery()
}
echo "<div class=\"general-print-pos general-print-font\">本次打印调用位置:$positionInfo</div>\n";
}
function ___output_css()
{
return <<<EOF
<style>
.general-print-shadow {
box-shadow: 1px 1px 10px #E8E6E3 inset,-1px 1px 2px #BBB9A6;
-moz-box-shadow: 1px 1px 10px #E8E6E3 inset,-1px 1px 2px #BBB9A6;
-webkit-box-shadow: 1px 1px 10px #E8E6E3 inset,-1px 1px 2px #BBB9A6;
}
.general-print-pos { position: relative; padding: 5px 15px; border: 1px solid #C6C6D5; border-radius: 5px 5px 0 0; font-size: smaller; margin: 1rem 2rem 0; background-color: #3D300D; color: #EEE; }
.general-print-font {font: 13px/1.5 Menlo, Monaco, Consolas, 'Courier New', monospace; }
.general-print-box {overflow-x: auto; border: 1px solid #C6C6D5; border-top: none; display: block; line-height: 1.4; margin: 0 2rem; background-color: #FCF9F1;
color: #316476; text-shadow: 0 1px 0 #F4F4F4; font-size: 100%; white-space: nowrap; text-align: left; }
.general-print-box pre {margin: 0 !important; padding: 10px 12px; font-family: inherit; }
</style>
EOF;
}