2008-06-06 06:37:07 +02:00
|
|
|
<?php
|
2008-08-11 00:52:52 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-11 00:52:52 +02:00
|
|
|
* @subpackage dev
|
|
|
|
*/
|
|
|
|
|
2008-06-06 06:37:07 +02:00
|
|
|
/**
|
|
|
|
* A basic HTML wrapper for stylish rendering of a developement info view.
|
|
|
|
* Used to output error messages, and test results.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-06 06:37:07 +02:00
|
|
|
* @subpackage dev
|
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
class DebugView extends Object
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-05 04:37:06 +02:00
|
|
|
/**
|
|
|
|
* Column size to wrap long strings to
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $columns = 100;
|
2008-06-06 06:37:07 +02:00
|
|
|
|
2008-08-13 10:40:02 +02:00
|
|
|
protected static $error_types = array(
|
|
|
|
E_USER_ERROR => array(
|
|
|
|
'title' => 'User Error',
|
|
|
|
'class' => 'error'
|
|
|
|
),
|
|
|
|
E_CORE_ERROR => array(
|
|
|
|
'title' => 'Core Error',
|
|
|
|
'class' => 'error'
|
|
|
|
),
|
|
|
|
E_NOTICE => array(
|
|
|
|
'title' => 'Notice',
|
|
|
|
'class' => 'notice'
|
|
|
|
),
|
2009-08-19 08:03:57 +02:00
|
|
|
E_USER_NOTICE => array(
|
2009-08-19 08:20:25 +02:00
|
|
|
'title' => 'User Notice',
|
2009-08-19 08:03:57 +02:00
|
|
|
'class' => 'notice'
|
|
|
|
),
|
2012-03-27 11:54:13 +02:00
|
|
|
E_DEPRECATED => array(
|
2012-04-03 00:28:07 +02:00
|
|
|
'title' => 'Deprecated',
|
2012-03-27 11:54:13 +02:00
|
|
|
'class' => 'notice'
|
|
|
|
),
|
2012-03-24 04:34:10 +01:00
|
|
|
E_USER_DEPRECATED => array(
|
2012-04-03 00:28:07 +02:00
|
|
|
'title' => 'User Deprecated',
|
2012-03-24 04:34:10 +01:00
|
|
|
'class' => 'notice'
|
|
|
|
),
|
2008-08-13 10:40:02 +02:00
|
|
|
E_CORE_ERROR => array(
|
|
|
|
'title' => 'Core Error',
|
|
|
|
'class' => 'error'
|
|
|
|
),
|
|
|
|
E_WARNING => array(
|
|
|
|
'title' => 'Warning',
|
|
|
|
'class' => 'warning'
|
|
|
|
),
|
|
|
|
E_CORE_WARNING => array(
|
|
|
|
'title' => 'Core Warning',
|
|
|
|
'class' => 'warning'
|
|
|
|
),
|
|
|
|
E_USER_WARNING => array(
|
2009-08-19 08:20:25 +02:00
|
|
|
'title' => 'User Warning',
|
2008-08-13 10:40:02 +02:00
|
|
|
'class' => 'warning'
|
2012-04-11 04:39:02 +02:00
|
|
|
),
|
|
|
|
E_STRICT => array(
|
|
|
|
'title' => 'Strict Notice',
|
|
|
|
'class' => 'notice'
|
2014-03-15 12:52:01 +01:00
|
|
|
),
|
|
|
|
E_RECOVERABLE_ERROR => array(
|
|
|
|
'title' => 'Recoverable Error',
|
|
|
|
'class' => 'warning'
|
2008-08-13 10:40:02 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-03-15 12:52:01 +01:00
|
|
|
protected static $unknown_error = array(
|
|
|
|
'title' => 'Unknown Error',
|
|
|
|
'class' => 'error'
|
|
|
|
);
|
|
|
|
|
2008-08-11 01:03:35 +02:00
|
|
|
/**
|
|
|
|
* Generate breadcrumb links to the URL path being displayed
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Breadcrumbs() {
|
|
|
|
$basePath = str_replace(Director::protocolAndHost(), '', Director::absoluteBaseURL());
|
2012-09-26 23:34:00 +02:00
|
|
|
$relPath = parse_url(substr($_SERVER['REQUEST_URI'], strlen($basePath), strlen($_SERVER['REQUEST_URI'])),
|
|
|
|
PHP_URL_PATH);
|
2008-10-10 15:05:37 +02:00
|
|
|
$parts = explode('/', $relPath);
|
2008-08-11 01:03:35 +02:00
|
|
|
$base = Director::absoluteBaseURL();
|
|
|
|
$pathPart = "";
|
2008-10-10 15:05:37 +02:00
|
|
|
$pathLinks = array();
|
2008-08-11 01:03:35 +02:00
|
|
|
foreach($parts as $part) {
|
|
|
|
if ($part != '') {
|
|
|
|
$pathPart .= "$part/";
|
2008-10-10 15:05:37 +02:00
|
|
|
$pathLinks[] = "<a href=\"$base$pathPart\">$part</a>";
|
2008-08-11 01:03:35 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 01:58:52 +02:00
|
|
|
return implode(' → ', $pathLinks);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2008-06-06 06:37:07 +02:00
|
|
|
/**
|
2015-07-27 02:19:55 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderHeader() instead
|
2008-06-06 06:37:07 +02:00
|
|
|
*/
|
|
|
|
public function writeHeader() {
|
2015-07-27 02:19:55 +02:00
|
|
|
Deprecation::notice('4.0', 'Use renderHeader() instead');
|
|
|
|
echo $this->renderHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderInfo() instead
|
|
|
|
*/
|
|
|
|
public function writeInfo($title, $subtitle, $description=false) {
|
|
|
|
Deprecation::notice('4.0', 'Use renderInfo() instead');
|
|
|
|
echo $this->renderInfo($title, $subtitle, $description);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderFooter() instead
|
|
|
|
*/
|
|
|
|
public function writeFooter() {
|
|
|
|
Deprecation::notice('4.0', 'Use renderFooter() instead');
|
|
|
|
echo $this->renderFooter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderError() instead
|
|
|
|
*/
|
|
|
|
public function writeError($httpRequest, $errno, $errstr, $errfile, $errline) {
|
|
|
|
Deprecation::notice('4.0', 'Use renderError() instead');
|
|
|
|
echo $this->renderError($httpRequest, $errno, $errstr, $errfile, $errline);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderSourceFragment() instead
|
|
|
|
*/
|
|
|
|
public function writeSourceFragment($lines, $errline) {
|
|
|
|
Deprecation::notice('4.0', 'Use renderSourceFragment() instead');
|
|
|
|
echo $this->renderSourceFragment($lines, $errline);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderTrace() instead
|
|
|
|
*/
|
|
|
|
public function writeTrace($trace) {
|
|
|
|
Deprecation::notice('4.0', 'Use renderTrace() instead');
|
|
|
|
echo $this->renderTrace($trace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 4.0.0:5.0.0 Use renderVariable() instead
|
|
|
|
*/
|
|
|
|
public function writeVariable($val, $caller) {
|
|
|
|
Deprecation::notice('4.0', 'Use renderVariable() instead');
|
|
|
|
echo $this->renderVariable($val, $caller);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render HTML header for development views
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function renderHeader() {
|
2010-12-08 00:53:42 +01:00
|
|
|
$url = htmlentities(
|
2014-08-15 08:53:05 +02:00
|
|
|
$_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'],
|
|
|
|
ENT_COMPAT,
|
2010-12-08 00:53:42 +01:00
|
|
|
'UTF-8'
|
|
|
|
);
|
2012-06-29 06:52:29 +02:00
|
|
|
|
|
|
|
$debugCSS = Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
FRAMEWORK_DIR,
|
|
|
|
'css/debug.css'
|
|
|
|
);
|
|
|
|
|
2015-07-27 02:19:55 +02:00
|
|
|
$output = '<!DOCTYPE html><html><head><title>' . $url . '</title>';
|
|
|
|
$output .= '<link rel="stylesheet" type="text/css" href="'. $debugCSS .'" />';
|
|
|
|
$output .= '</head>';
|
|
|
|
$output .= '<body>';
|
|
|
|
|
|
|
|
return $output;
|
2008-06-06 06:37:07 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 01:03:35 +02:00
|
|
|
/**
|
|
|
|
* Render the information header for the view
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-07-27 02:19:55 +02:00
|
|
|
* @param string $title The main title
|
|
|
|
* @param string $subtitle The subtitle
|
|
|
|
* @param string|false $description The description to show
|
|
|
|
* @return string
|
2008-08-11 01:03:35 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderInfo($title, $subtitle, $description=false) {
|
|
|
|
$output = '<div class="info">';
|
|
|
|
$output .= "<h1>" . Convert::raw2xml($title) . "</h1>";
|
|
|
|
if($subtitle) $output .= "<h3>" . Convert::raw2xml($subtitle) . "</h3>";
|
2008-08-11 01:03:35 +02:00
|
|
|
if ($description) {
|
2015-07-27 02:19:55 +02:00
|
|
|
$output .= "<p>$description</p>";
|
2008-08-11 01:03:35 +02:00
|
|
|
} else {
|
2015-07-27 02:19:55 +02:00
|
|
|
$output .= $this->Breadcrumbs();
|
2008-08-11 01:03:35 +02:00
|
|
|
}
|
2015-07-27 02:19:55 +02:00
|
|
|
$output .= '</div>';
|
|
|
|
|
|
|
|
return $output;
|
2008-08-11 01:03:35 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-06-06 06:37:07 +02:00
|
|
|
/**
|
|
|
|
* Render HTML footer for development views
|
2015-07-27 02:19:55 +02:00
|
|
|
* @return string
|
2008-06-06 06:37:07 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderFooter() {
|
|
|
|
return "</body></html>";
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2008-08-11 06:02:32 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-27 02:19:55 +02:00
|
|
|
* Render an error.
|
|
|
|
*
|
|
|
|
* @param string $httpRequest the kind of request
|
|
|
|
* @param int $errno Codenumber of the error
|
|
|
|
* @param string $errstr The error message
|
|
|
|
* @param string $errfile The name of the soruce code file where the error occurred
|
|
|
|
* @param int $errline The line number on which the error occured
|
|
|
|
* @return string
|
2008-08-11 06:02:32 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) {
|
2014-03-15 12:52:01 +01:00
|
|
|
$errorType = isset(self::$error_types[$errno]) ? self::$error_types[$errno] : self::$unknown_error;
|
2011-11-29 10:43:06 +01:00
|
|
|
$httpRequestEnt = htmlentities($httpRequest, ENT_COMPAT, 'UTF-8');
|
2014-03-15 02:38:54 +01:00
|
|
|
if (ini_get('html_errors')) {
|
|
|
|
$errstr = strip_tags($errstr);
|
|
|
|
} else {
|
|
|
|
$errstr = Convert::raw2xml($errstr);
|
|
|
|
}
|
2015-07-27 02:19:55 +02:00
|
|
|
$output = '<div class="info ' . $errorType['class'] . '">';
|
|
|
|
$output .= "<h1>[" . $errorType['title'] . '] ' . $errstr . "</h1>";
|
|
|
|
$output .= "<h3>$httpRequestEnt</h3>";
|
|
|
|
$output .= "<p>Line <strong>$errline</strong> in <strong>$errfile</strong></p>";
|
|
|
|
$output .= '</div>';
|
|
|
|
|
|
|
|
return $output;
|
2008-08-11 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-27 02:19:55 +02:00
|
|
|
* Render 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 The line of the error
|
|
|
|
* @return string
|
2008-08-11 06:02:32 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderSourceFragment($lines, $errline) {
|
|
|
|
$output = '<div class="trace"><h3>Source</h3>';
|
|
|
|
$output .= '<pre>';
|
2008-08-11 06:02:32 +02:00
|
|
|
foreach($lines as $offset => $line) {
|
2010-12-08 00:53:42 +01:00
|
|
|
$line = htmlentities($line, ENT_COMPAT, 'UTF-8');
|
2008-08-11 06:02:32 +02:00
|
|
|
if ($offset == $errline) {
|
2015-07-27 02:19:55 +02:00
|
|
|
$output .= "<span>$offset</span> <span class=\"error\">$line</span>";
|
2008-08-11 06:02:32 +02:00
|
|
|
} else {
|
2015-07-27 02:19:55 +02:00
|
|
|
$output .= "<span>$offset</span> $line";
|
2008-08-11 06:02:32 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 02:19:55 +02:00
|
|
|
$output .= '</pre>';
|
|
|
|
|
|
|
|
return $output;
|
2008-08-11 06:02:32 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 06:02:32 +02:00
|
|
|
/**
|
2015-07-27 02:19:55 +02:00
|
|
|
* Render a call track
|
|
|
|
*
|
|
|
|
* @param array $trace The debug_backtrace() array
|
|
|
|
* @return string
|
2008-08-11 06:02:32 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderTrace($trace) {
|
|
|
|
$output = '<h3>Trace</h3>';
|
|
|
|
$output .= SS_Backtrace::get_rendered_backtrace($trace);
|
|
|
|
$output .= '</div>';
|
|
|
|
|
|
|
|
return $output;
|
2008-08-11 06:02:32 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-01 16:43:43 +02:00
|
|
|
/**
|
2015-07-27 02:19:55 +02:00
|
|
|
* Render an arbitrary paragraph.
|
|
|
|
*
|
|
|
|
* @param string $text The HTML-escaped text to render
|
|
|
|
* @return string
|
2008-10-01 16:43:43 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderParagraph($text) {
|
|
|
|
return '<p class="info">' . $text . '</p>';
|
2008-10-01 16:43:43 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-05 04:37:06 +02:00
|
|
|
/**
|
|
|
|
* Formats the caller of a method
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-07-27 02:19:55 +02:00
|
|
|
* @param array $caller
|
2014-07-05 04:37:06 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatCaller($caller) {
|
|
|
|
$return = basename($caller['file']) . ":" . $caller['line'];
|
|
|
|
if(!empty($caller['class']) && !empty($caller['function'])) {
|
|
|
|
$return .= " - {$caller['class']}::{$caller['function']}()";
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-05 04:37:06 +02:00
|
|
|
/**
|
|
|
|
* Outputs a variable in a user presentable way
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-07-27 02:19:55 +02:00
|
|
|
* @param object $val
|
|
|
|
* @param array $caller Caller information
|
|
|
|
* @return string
|
2014-07-05 04:37:06 +02:00
|
|
|
*/
|
2015-07-27 02:19:55 +02:00
|
|
|
public function renderVariable($val, $caller) {
|
|
|
|
$output = '<pre style="background-color:#ccc;padding:5px;font-size:14px;line-height:18px;">';
|
|
|
|
$output .= "<span style=\"font-size: 12px;color:#666;\">" . $this->formatCaller($caller). " - </span>\n";
|
|
|
|
if (is_string($val)) $output .= wordwrap($val, self::config()->columns);
|
|
|
|
else $output .= var_export($val, true);
|
|
|
|
$output .= '</pre>';
|
|
|
|
|
|
|
|
return $output;
|
2014-07-05 04:37:06 +02:00
|
|
|
}
|
2008-06-06 06:37:07 +02:00
|
|
|
}
|