silverstripe-framework/src/Dev/CliDebugView.php

207 lines
5.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Dev;
2016-09-09 08:43:05 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
/**
* A basic HTML wrapper for stylish rendering of a development info view.
* Used to output error messages, and test results.
2014-08-15 08:53:05 +02:00
*
* @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView?
*/
class CliDebugView extends DebugView
{
2016-11-29 00:31:16 +01:00
/**
* Render HTML header for development views
*
* @param HTTPRequest $httpRequest
* @return string
*/
public function renderHeader($httpRequest = null)
{
return null;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Render HTML footer for development views
*/
public function renderFooter()
{
}
2016-11-29 00:31:16 +01:00
/**
* Write information about the error to the screen
*
* @param string $httpRequest
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return string
*/
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline)
{
if (!isset(self::$error_types[$errno])) {
$errorTypeTitle = "UNKNOWN TYPE, ERRNO $errno";
} else {
$errorTypeTitle = self::$error_types[$errno]['title'];
}
$output = CLI::text("ERROR [" . $errorTypeTitle . "]: $errstr\nIN $httpRequest\n", "red", null, true);
$output .= CLI::text("Line $errline in $errfile\n\n", "red");
2016-11-29 00:31:16 +01:00
return $output;
}
2016-11-29 00:31:16 +01:00
/**
* Write a fragment of the a source file
*
* @param array $lines An array of file lines; the keys should be the original line numbers
* @param int $errline Index of the line in $lines which has the error
* @return string
*/
public function renderSourceFragment($lines, $errline)
{
$output = "Source\n======\n";
foreach ($lines as $offset => $line) {
$output .= ($offset == $errline) ? "* " : " ";
$output .= str_pad("$offset:", 5);
2022-04-14 03:12:59 +02:00
$output .= wordwrap($line ?? '', self::config()->columns ?? 0, "\n ");
2016-11-29 00:31:16 +01:00
}
$output .= "\n";
2016-11-29 00:31:16 +01:00
return $output;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Write a backtrace
*
* @param array $trace
* @return string
*/
public function renderTrace($trace = null)
{
$output = "Trace\n=====\n";
$output .= Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true);
2016-11-29 00:31:16 +01:00
return $output;
}
public function renderParagraph($text)
{
2022-04-14 03:12:59 +02:00
return wordwrap($text ?? '', self::config()->columns ?? 0) . "\n\n";
}
2016-11-29 00:31:16 +01:00
/**
* Render the information header for the view
*
* @param string $title
* @param string $subtitle
* @param string $description
* @return string
*/
public function renderInfo($title, $subtitle, $description = null)
{
2022-04-14 03:12:59 +02:00
$output = wordwrap(strtoupper($title ?? ''), self::config()->columns ?? 0) . "\n";
$output .= wordwrap($subtitle ?? '', self::config()->columns ?? 0) . "\n";
$output .= str_repeat('-', min(self::config()->columns, max(strlen($title ?? ''), strlen($subtitle ?? ''))) ?? 0) . "\n";
$output .= wordwrap($description ?? '', self::config()->columns ?? 0) . "\n\n";
2016-11-29 00:31:16 +01:00
return $output;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
public function renderVariable($val, $caller)
{
$output = PHP_EOL;
2022-04-14 03:12:59 +02:00
$output .= CLI::text(str_repeat('=', self::config()->columns ?? 0), 'green');
2016-11-29 00:31:16 +01:00
$output .= PHP_EOL;
$output .= CLI::text($this->formatCaller($caller), 'blue', null, true);
2018-01-16 19:39:30 +01:00
$output .= PHP_EOL . PHP_EOL;
2016-11-29 00:31:16 +01:00
if (is_string($val)) {
2022-04-14 03:12:59 +02:00
$output .= wordwrap($val ?? '', self::config()->columns ?? 0);
2016-11-29 00:31:16 +01:00
} else {
$output .= var_export($val, true);
}
$output .= PHP_EOL;
2022-04-14 03:12:59 +02:00
$output .= CLI::text(str_repeat('=', self::config()->columns ?? 0), 'green');
2016-11-29 00:31:16 +01:00
$output .= PHP_EOL;
2016-11-29 00:31:16 +01:00
return $output;
}
/**
* Similar to renderVariable() but respects debug() method on object if available
*
* @param mixed $val
* @param array $caller
* @param bool $showHeader
* @return string
*/
public function debugVariable($val, $caller, $showHeader = true)
{
$text = $this->debugVariableText($val);
if ($showHeader) {
$callerFormatted = $this->formatCaller($caller);
return "Debug ($callerFormatted)\n{$text}\n\n";
} else {
return $text;
}
}
/**
* Get debug text for this object
*
* @param mixed $val
* @return string
*/
public function debugVariableText($val)
{
// Check debug
if (is_object($val) && ClassInfo::hasMethod($val, 'debug')) {
return $val->debug();
}
// Format as array
if (is_array($val)) {
$result = '';
foreach ($val as $key => $valItem) {
$valText = $this->debugVariableText($valItem);
$result .= "$key = $valText\n";
}
return $result;
}
// Format object
if (is_object($val)) {
return print_r($val, true);
}
// Format bool
if (is_bool($val)) {
return '(bool) ' . ($val ? 'true' : 'false');
}
// Format text
if (is_string($val)) {
2022-04-14 03:12:59 +02:00
return wordwrap($val ?? '', self::config()->columns ?? 0);
}
// Other
return var_export($val, true);
}
public function renderMessage($message, $caller, $showHeader = true)
{
$header = '';
if ($showHeader) {
2022-04-14 03:12:59 +02:00
$file = basename($caller['file'] ?? '');
$line = $caller['line'];
$header .= "Debug (line {$line} of {$file}):\n";
}
return $header . "{$message}\n\n";
}
}