forked from lekoala/silverstripe-debugbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_config.php
158 lines (141 loc) · 5.17 KB
/
_config.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
<?php
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
// Add a simple utility that leverages Symfony VarDumper and cleans buffer to avoid debug messages
if (!function_exists('d')) {
/**
* Helpful debugging helper. Pass as many arguments as you need.
* Keep the call on one line to be able to output arguments names
* Without arguments, it will display all object instances in the backtrace
*
* @return void
*/
function d()
{
$args = func_get_args();
$doExit = true;
$isPlain = Director::is_ajax() || Director::is_cli();
// Allow testing the helper
if (isset($args[0]) && $args[0] instanceof \SilverStripe\Dev\SapphireTest) {
$doExit = false;
array_shift($args);
}
// Clean buffer that may be in the way
if (ob_get_contents()) {
ob_end_clean();
}
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
// Where is d called is the first element of the backtrace
$line = $bt[0]['line'];
$file = $bt[0]['file'];
// Caller
$caller_function = isset($bt[1]['function']) ? $bt[1]['function'] : null;
$caller_class = isset($bt[1]['class']) ? $bt[1]['class'] : null;
$caller = $caller_function;
if ($caller_class) {
$caller = $caller_class . '::' . $caller_function;
}
// Probably best to avoid using this in live websites...
if (Director::isLive()) {
Injector::inst()->get(LoggerInterface::class)->info("Please remove call to d() in $file:$line");
return;
}
// Arguments passed to the function are stored in matches
$src = file($file);
$src_line = $src[$line - 1];
preg_match("/d\((.+)\)/", $src_line, $matches);
// Find all arguments, ignore variables within parenthesis
$arguments_name = array();
if (!empty($matches[1])) {
$arguments_name = array_map('trim', preg_split("/(?![^(]*\)),/", $matches[1]));
}
// Display data nicely according to context
$print = function () use ($isPlain) {
$args = func_get_args();
if (!$isPlain) {
echo '<pre>';
}
foreach ($args as $arg) {
if ($isPlain && $arg === "") {
echo "(empty)";
continue;
}
if ($isPlain && $arg === null) {
echo "(null)";
continue;
}
if (is_string($arg)) {
echo $arg;
} else {
// Avoid print_r on object as it can cause massive recursion
if (is_object($arg)) {
echo get_class($arg) . "\n";
}
echo json_encode($arg, JSON_PRETTY_PRINT, 5);
}
echo "\n";
}
if (!$isPlain) {
echo '</pre>';
}
};
// Display caller info
$print("$file:$line ($caller)");
// Display data in a friendly manner
if (empty($args)) {
$arguments_name = array();
foreach ($bt as $trace) {
if (!empty($trace['object'])) {
$line = isset($trace['line']) ? $trace['line'] : 0;
$function = isset($trace['function']) ? $trace['function'] : 'unknown function';
$arguments_name[] = $function . ':' . $line;
$args[] = $trace['object'];
}
}
}
$i = 0;
foreach ($args as $arg) {
// Echo name of the variable
$len = 20;
$varname = isset($arguments_name[$i]) ? $arguments_name[$i] : null;
if ($varname) {
$print('Value for: ' . $varname);
$len = strlen($varname);
}
// For ajax and cli requests, a good old print_r is much better
if ($isPlain || !function_exists('dump')) {
$print($arg);
// Make a nice line between variables for readability
if (count($args) > 1) {
$print(str_repeat('-', $len));
}
} else {
if ($varname && is_string($arg) && strpos($varname, 'sql') !== false) {
echo SqlFormatter::format($arg);
} else {
dump($arg);
}
}
$i++;
}
if ($doExit) {
exit();
}
}
}
// Add a simple log helper that provides a default priority
if (!function_exists('l')) {
function l($message, $priority = Logger::DEBUG, $extras = [])
{
if (!is_string($message)) {
$message = json_encode((array) $message);
}
if (is_array($priority)) {
$extras = $priority;
$priority = Logger::DEBUG;
}
Injector::inst()->get(LoggerInterface::class)->log($priority, $message, $extras);
}
}