-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpVarDump.php
160 lines (139 loc) · 3.21 KB
/
phpVarDump.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
<?php
/**
* PHP Variable Dump
*
* @author Chinmay G. Palekar [ChinmayP79]
* @copyright (C) 2022-forever, Chinmay G. Palekar [ChinmayP79]
* @license GPL-3.0
**/
/**
* PHP Variable Dump Class
*/
class phpVarDump
{
/**
* Edit only public properties after this :
**/
/** @var string Dump Message */
public string $dump_msg = 'Variable Dump';
/**
* Do not edit anything after this :
**/
/** @var bool Store the dump */
public bool $dump_store = false;
/** @var string New line */
private string $nl = "";
/** @var string Dump */
private string $dump = '';
/**
* Set public property of same type
*
* @param string $name
* @param mixed $value
* @access public
**/
final public function propSet(string $name, $value)
{
$prop = new ReflectionProperty($this, $name);
if ($prop->isPublic() && $prop->getType()->isBuiltin() && $prop->getType()->getName() === gettype($value)) $this->$name = $value;
unset($prop);
return $this;
}
/**
* PHP Variable Dump
*
* @param mixed ...$vars
* @access private
**/
final private function php_var_dump(...$vars)
{
$dbt = @debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
if (!empty($dbt))
{
$this->dump .= 'In '.$dbt['file'].':'.$dbt['line'].':';
$this->dump .= !empty($dbt['class']) ? $dbt['class'] : '';
$this->dump .= !empty($dbt['type']) ? $dbt['type'] : '';
$this->dump .= !empty($dbt['function']) ? $dbt['function'].':' : '';
$this->dump .= $this->nl;
}
unset($dbt);
ini_set('xdebug.var_display_max_children', '-1');
ini_set('xdebug.var_display_max_data', '-1');
ini_set('xdebug.var_display_max_depth', '-1');
ob_start();
var_dump(...$vars); $vd_line = __LINE__;
$this->dump .= ob_get_contents();
ob_end_clean();
ini_restore('xdebug.var_display_max_children');
ini_restore('xdebug.var_display_max_data');
ini_restore('xdebug.var_display_max_depth');
// Remove first mis-info line for Xdebug var_dump
$vd_line = preg_quote(str_replace(__DIR__ . DIRECTORY_SEPARATOR,'',__FILE__) .':'. $vd_line);
$this->dump = preg_replace('/\<small\>(.*?)'.$vd_line.'\:\<\/small\>/', '', $this->dump);
}
/**
* Variable Dump wrapped inside HTML `<pre><code>` tags
*
* @param mixed ...$vars
* @access public
**/
final public function preFormat(...$vars)
{
$this->nl = "<br>";
$this->php_var_dump(...$vars);
if (!$this->dump_store)
{
echo('<pre><code>' . $this->dump . '</code></pre>');
$this->dump = '';
}
}
/**
* Variable Dump to file
*
* @param mixed ...$vars
* @access public
**/
final public function file(...$vars)
{
$this->nl = "\n";
$this->php_var_dump(...$vars);
if (!$this->dump_store)
{
// ToDo file
$this->dump = '';
}
}
/**
* Variable Dump to Email
*
* @param mixed ...$vars
* @access public
**/
final public function email(...$vars)
{
$this->nl = "<br>";
$this->php_var_dump(...$vars);
if (!$this->dump_store)
{
// ToDo mail
$this->dump = '';
}
}
/**
* Variable Dump to PHP Error Log
*
* @param mixed ...$vars
* @access public
**/
final public function errorLog(...$vars)
{
$this->nl = "\n";
$this->php_var_dump(...$vars);
if (!$this->dump_store)
{
// ToDo error_log
$this->dump = '';
}
}
}
?>