Skip to content

Commit

Permalink
Automatically convert d() and db() to print_r() or echo when …
Browse files Browse the repository at this point in the history
…called via CLI
  • Loading branch information
adrianbj committed Jul 11, 2024
1 parent 7d0c69c commit 5936b20
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 42 deletions.
6 changes: 3 additions & 3 deletions TracyDebugger.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function getModuleInfo() {
'summary' => __('Tracy debugger from Nette with many PW specific custom tools.', __FILE__),
'author' => 'Adrian Jones',
'href' => 'https://processwire.com/talk/forum/58-tracy-debugger/',
'version' => '4.26.32',
'version' => '4.26.33',
'autoload' => 100000, // in PW 3.0.114+ higher numbers are loaded first - we want Tracy first
'singular' => true,
'requires' => 'ProcessWire>=2.7.2, PHP>=5.4.4',
Expand Down Expand Up @@ -4295,7 +4295,7 @@ public function getModuleConfigInputfields(array $data) {
$f->attr('name', 'excludedPwLogFiles');
$f->label = __('Excluded PW log files', __FILE__);
$f->description = __('Select log files to be excluded', __FILE__);
$f->notes = __('Useful if you have logs that are written to regularly on user interaction that are overwhelming more useful alert/error/warning logs.', __FILE__);
$f->notes = __('Useful if you have logs that are written to regularly on user interaction that are overwhelming more useful alert/warning/error logs.', __FILE__);
$f->columnWidth = 50;
$f->setAsmSelectOption('sortable', false);
foreach($this->wire('log')->getLogs() as $k => $v) {
Expand All @@ -4308,7 +4308,7 @@ public function getModuleConfigInputfields(array $data) {
$f->attr('name', 'excludedTracyLogFiles');
$f->label = __('Excluded Tracy log files', __FILE__);
$f->description = __('Select log files to be excluded', __FILE__);
$f->notes = __('Useful if you have logs that are written to regularly on user interaction that are overwhelming more useful alert/error/warning logs.', __FILE__);
$f->notes = __('Useful if you have logs that are written to regularly on user interaction that are overwhelming more useful alert/warning/error logs.', __FILE__);
$f->columnWidth = 50;
$f->setAsmSelectOption('sortable', false);
foreach((new \TracyLogsPanel())->getLogs() as $k => $v) {
Expand Down
8 changes: 4 additions & 4 deletions includes/ShortcutMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function barDumpBig($var, $title = NULL) {
*/
if(!function_exists('dump') && in_array('dump', $this->data['enabledShortcutMethods'])) {
function dump($var, $title = NULL, array $options = NULL) {
if(tracyUnavailable()) return false;
if(tracyUnavailable() && PHP_SAPI !== 'cli') return false;
return TD::dump($var, $title, $options);
}
}
Expand All @@ -65,7 +65,7 @@ function dump($var, $title = NULL, array $options = NULL) {
*/
if(!function_exists('dumpBig') && in_array('dumpBig', $this->data['enabledShortcutMethods'])) {
function dumpBig($var, $title = NULL, array $options = NULL) {
if(tracyUnavailable()) return false;
if(tracyUnavailable() && PHP_SAPI !== 'cli') return false;
return TD::dumpBig($var, $title, $options);
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ function bdb($var, $title = NULL) {
*/
if(!function_exists('d') && in_array('d', $this->data['enabledShortcutMethods'])) {
function d($var, $title = NULL, array $options = NULL) {
if(tracyUnavailable()) return false;
if(tracyUnavailable() && PHP_SAPI !== 'cli') return false;
return TD::dump($var, $title, $options);
}
}
Expand All @@ -181,7 +181,7 @@ function d($var, $title = NULL, array $options = NULL) {
*/
if(!function_exists('db') && in_array('db', $this->data['enabledShortcutMethods'])) {
function db($var, $title = NULL, array $options = NULL) {
if(tracyUnavailable()) return false;
if(tracyUnavailable() && PHP_SAPI !== 'cli') return false;
return TD::dumpBig($var, $title, $options);
}
}
Expand Down
98 changes: 63 additions & 35 deletions includes/TD.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,59 +109,87 @@ public static function barDumpBig($var, $title = NULL, array $options = NULL) {
* @tracySkipLocation
*/
public static function dump($var, $title = NULL, array $options = NULL) {
if(self::tracyUnavailable()) return false;
if(self::tracyUnavailable() && PHP_SAPI !== 'cli') return false;
if(is_array($title)) {
$options = $title;
$title = NULL;
}
if(isset($options) && is_array($options) && !static::has_string_keys($options)) {
if(isset($options[0])) $options['maxDepth'] = $options[0];
if(isset($options[1])) $options['maxLength'] = $options[1];
if(isset($options[2])) $options['maxItems'] = $options[2];

if(PHP_SAPI === 'cli') {
echo $title . PHP_EOL;
if(is_array($var) || is_object($var)) {
print_r($var);
}
else {
echo $var;
}
return false;
}
$options[Dumper::DEPTH] = isset($options['maxDepth']) ? $options['maxDepth'] : \TracyDebugger::getDataValue('maxDepth');
$options[Dumper::TRUNCATE] = isset($options['maxLength']) ? $options['maxLength'] : \TracyDebugger::getDataValue('maxLength');
if(defined('\Tracy\Dumper::ITEMS')) {
$options[Dumper::ITEMS] = isset($options['maxItems']) ? $options['maxItems'] : \TracyDebugger::getDataValue('maxItems');
else {

if(isset($options) && is_array($options) && !static::has_string_keys($options)) {
if(isset($options[0])) $options['maxDepth'] = $options[0];
if(isset($options[1])) $options['maxLength'] = $options[1];
if(isset($options[2])) $options['maxItems'] = $options[2];
}
$options[Dumper::DEPTH] = isset($options['maxDepth']) ? $options['maxDepth'] : \TracyDebugger::getDataValue('maxDepth');
$options[Dumper::TRUNCATE] = isset($options['maxLength']) ? $options['maxLength'] : \TracyDebugger::getDataValue('maxLength');
if(defined('\Tracy\Dumper::ITEMS')) {
$options[Dumper::ITEMS] = isset($options['maxItems']) ? $options['maxItems'] : \TracyDebugger::getDataValue('maxItems');
}
$options[Dumper::LOCATION] = \TracyDebugger::$fromConsole ? false : Debugger::$showLocation;
if(version_compare(Debugger::VERSION, '2.6.0', '>=')) $options[Dumper::LAZY] = false;
echo '
<div class="tracy-inner" style="height:auto !important">
<div class="tracy-DumpPanel">';
if($title) echo '<h2>'.$title.'</h2>';
echo static::generateDump($var, $options) .
' </div>
</div>';
}
$options[Dumper::LOCATION] = \TracyDebugger::$fromConsole ? false : Debugger::$showLocation;
if(version_compare(Debugger::VERSION, '2.6.0', '>=')) $options[Dumper::LAZY] = false;
echo '
<div class="tracy-inner" style="height:auto !important">
<div class="tracy-DumpPanel">';
if($title) echo '<h2>'.$title.'</h2>';
echo static::generateDump($var, $options) .
' </div>
</div>';
}

/**
* Tracy\Debugger::dumpBig() shortcut dumping with maxDepth = 6, maxLength = 9999 and maxItems = 250.
* @tracySkipLocation
*/
public static function dumpBig($var, $title = NULL, array $options = NULL) {
if(self::tracyUnavailable()) return false;
if(self::tracyUnavailable() && PHP_SAPI !== 'cli') return false;
if(is_array($title)) {
$options = $title;
$title = NULL;
}
if(isset($options) && is_array($options) && !static::has_string_keys($options)) {
if(isset($options[0])) $options['maxDepth'] = $options[0];
if(isset($options[1])) $options['maxLength'] = $options[1];
if(isset($options[2])) $options['maxItems'] = $options[2];

if(PHP_SAPI === 'cli') {
echo $title . PHP_EOL;
if(is_array($var) || is_object($var)) {
print_r($var);
}
else {
echo $var;
}
return false;
}
else {

if(isset($options) && is_array($options) && !static::has_string_keys($options)) {
if(isset($options[0])) $options['maxDepth'] = $options[0];
if(isset($options[1])) $options['maxLength'] = $options[1];
if(isset($options[2])) $options['maxItems'] = $options[2];
}
$options[Dumper::DEPTH] = 6;
$options[Dumper::TRUNCATE] = 9999;
if(defined('\Tracy\Dumper::ITEMS')) $options[Dumper::ITEMS] = 250;
$options[Dumper::LOCATION] = \TracyDebugger::$fromConsole ? false : Debugger::$showLocation;
if(version_compare(Debugger::VERSION, '2.6.0', '>=')) $options[Dumper::LAZY] = false;
echo '
<div class="tracy-inner" style="height:auto !important">
<div class="tracy-DumpPanel">';
if($title) echo '<h2>'.$title.'</h2>';
echo static::generateDump($var, $options) .
' </div>
</div>';
}
$options[Dumper::DEPTH] = 6;
$options[Dumper::TRUNCATE] = 9999;
if(defined('\Tracy\Dumper::ITEMS')) $options[Dumper::ITEMS] = 250;
$options[Dumper::LOCATION] = \TracyDebugger::$fromConsole ? false : Debugger::$showLocation;
if(version_compare(Debugger::VERSION, '2.6.0', '>=')) $options[Dumper::LAZY] = false;
echo '
<div class="tracy-inner" style="height:auto !important">
<div class="tracy-DumpPanel">';
if($title) echo '<h2>'.$title.'</h2>';
echo static::generateDump($var, $options) .
' </div>
</div>';
}

/**
Expand Down

0 comments on commit 5936b20

Please sign in to comment.