2008-08-11 06:02:32 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Dev;
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-12-23 03:41:41 +01:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Convert;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2008-08-11 06:02:32 +02:00
|
|
|
/**
|
2021-12-13 09:05:33 +01:00
|
|
|
* A basic HTML wrapper for stylish rendering of a development info view.
|
2008-08-11 06:02:32 +02:00
|
|
|
* Used to output error messages, and test results.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-11 06:02:32 +02:00
|
|
|
* @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView?
|
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
class CliDebugView extends DebugView
|
|
|
|
{
|
2008-08-11 06:02:32 +02:00
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
2008-08-11 06:02:32 +02:00
|
|
|
|
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");
|
2015-07-27 02:19:55 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $output;
|
|
|
|
}
|
2008-08-11 06:02:32 +02:00
|
|
|
|
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";
|
2015-07-27 02:19:55 +02:00
|
|
|
|
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);
|
2015-07-27 02:19:55 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $output;
|
|
|
|
}
|
2008-08-13 03:47:05 +02:00
|
|
|
|
2017-05-11 07:38:29 +02:00
|
|
|
public function renderParagraph($text)
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return wordwrap($text ?? '', self::config()->columns ?? 0) . "\n\n";
|
2017-05-11 07:38:29 +02:00
|
|
|
}
|
|
|
|
|
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";
|
2015-07-27 02:19:55 +02:00
|
|
|
|
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;
|
2015-07-27 02:19:55 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $output;
|
|
|
|
}
|
2016-12-23 03:41:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-02-08 21:52:32 +01:00
|
|
|
if (is_object($val) && ClassInfo::hasMethod($val, 'debug')) {
|
2016-12-23 03:41:41 +01:00
|
|
|
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)) {
|
2018-09-24 12:31:06 +02:00
|
|
|
return print_r($val, true);
|
2016-12-23 03:41:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2016-12-23 03:41:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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'] ?? '');
|
2016-12-23 03:41:41 +01:00
|
|
|
$line = $caller['line'];
|
|
|
|
$header .= "Debug (line {$line} of {$file}):\n";
|
|
|
|
}
|
|
|
|
return $header . "{$message}\n\n";
|
|
|
|
}
|
2008-08-11 06:02:32 +02:00
|
|
|
}
|