-
Notifications
You must be signed in to change notification settings - Fork 27
/
_config.php
181 lines (165 loc) · 6.15 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
use Psr\Log\LoggerInterface;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
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);
} else {
// 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'] ?? 0;
$file = $bt[0]['file'] ?? "unknown 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);
if (!$src) {
return;
}
$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])) {
$split = preg_split("/(?![^(]*\)),/", $matches[1]);
if ($split) {
$arguments_name = array_map('trim', $split);
}
}
// Display data nicely according to context
$print = function (...$args) use ($isPlain) {
if (!$isPlain) {
echo '<pre>';
}
foreach ($args as $arg) {
if ($isPlain && $arg === "") {
$arg = "(empty)";
} elseif ($isPlain && $arg === null) {
$arg = "(null)";
} elseif (!is_string($arg)) {
// Avoid print_r on object as it can cause massive recursion
if (is_object($arg)) {
$arg = get_class($arg);
} else {
$arg = json_encode($arg, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR, 5);
}
}
$arg = trim($arg);
if (strlen($arg) > 255) {
$arg = substr($arg, 0, 252) . "...";
}
echo $arg . "\n";
}
if (!$isPlain) {
echo '</pre>';
}
};
// Display caller info
$fileline = "$file:$line";
if (!$isPlain) {
// Allow opening in ide
$idePrefix = Environment::getEnv('IDE_PROTOCOL');
if (!$idePrefix) {
$idePrefix = 'vscode';
}
$idePlaceholder = $idePrefix . '://file/{file}:{line}';
$ideLink = str_replace(['{file}', '{line}'], [$file, $line], $idePlaceholder);
$fileline = "<a href=\"$ideLink\">$fileline</a>";
}
$print("$fileline ($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')) {
/**
* @param string|array<mixed> $message
* @param mixed $priority This can be skipped array can be used instead for extras
* @param array<mixed> $extras
* @return void
*/
function l($message, $priority = \Monolog\Level::Debug, $extras = [])
{
if (!is_string($message)) {
$message = json_encode((array) $message, JSON_THROW_ON_ERROR);
}
if (is_array($priority)) {
$extras = $priority;
$priority = \Monolog\Level::Debug;
}
/** @var LoggerInterface $inst */
$inst = Injector::inst()->get(LoggerInterface::class);
$inst->log($priority, $message, $extras);
}
}