From ebc3900c4af39ff3c46a0127b0cf681c829666a6 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 27 Jul 2015 12:19:55 +1200 Subject: [PATCH] =?UTF-8?q?NEW:=20Replace=20DebugView=E2=80=99s=20writeX()?= =?UTF-8?q?=20functions=20with=20renderX()=20functions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DebugView writes straight to STDOUT. It would be much more flexible if it returns a string instead. It’s possible that DebugView should be scuttled in favour of a 3rd party library, but in the meantime, this change makes it more useful for the logging changes. --- dev/CliDebugView.php | 79 ++++++++++-------- dev/DebugView.php | 188 ++++++++++++++++++++++++++++++++----------- 2 files changed, 186 insertions(+), 81 deletions(-) diff --git a/dev/CliDebugView.php b/dev/CliDebugView.php index bec31e511..c7ee869d8 100644 --- a/dev/CliDebugView.php +++ b/dev/CliDebugView.php @@ -9,49 +9,60 @@ * @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView? */ -class CliDebugView extends DebugView { +class CliDebugView extends DebugView +{ /** * Render HTML header for development views */ - public function writeHeader($httpRequest = null) { + public function renderHeader($httpRequest = null) { } /** * Render HTML footer for development views */ - public function writeFooter() { + public function renderFooter() { } /** * Write information about the error to the screen */ - public function writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext) { - $errorType = self::$error_types[$errno]; - echo SS_Cli::text("ERROR [" . $errorType['title'] . "]: $errstr\nIN $httpRequest\n", "red", null, true); - echo SS_Cli::text("Line $errline in $errfile\n\n", "red"); + 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 = SS_Cli::text("ERROR [" . $errorTypeTitle . "]: $errstr\nIN $httpRequest\n", "red", null, true); + $output .= SS_Cli::text("Line $errline in $errfile\n\n", "red"); + + return $output; } /** * Write a fragment of the a source file * @param $lines An array of file lines; the keys should be the original line numbers */ - public function writeSourceFragment($lines, $errline) { - echo "Source\n======\n"; + public function renderSourceFragment($lines, $errline) { + $output = "Source\n======\n"; foreach($lines as $offset => $line) { - echo ($offset == $errline) ? "* " : " "; - echo str_pad("$offset:",5); - echo wordwrap($line, self::config()->columns, "\n "); + $output .= ($offset == $errline) ? "* " : " "; + $output .= str_pad("$offset:", 5); + $output .= wordwrap($line, self::config()->columns, "\n "); } - echo "\n"; + $output .= "\n"; + + return $output; } /** * Write a backtrace */ - public function writeTrace($trace = null) { - echo "Trace\n=====\n"; - echo SS_Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true); + public function renderTrace($trace = null) { + $output = "Trace\n=====\n"; + $output .= SS_Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true); + + return $output; } /** @@ -60,26 +71,30 @@ class CliDebugView extends DebugView { * @param string $title * @param string $title */ - public function writeInfo($title, $subtitle, $description=false) { - echo wordwrap(strtoupper($title),self::config()->columns) . "\n"; - echo wordwrap($subtitle,self::config()->columns) . "\n"; - echo str_repeat('-',min(self::config()->columns,max(strlen($title),strlen($subtitle)))) . "\n"; - echo wordwrap($description,self::config()->columns) . "\n\n"; + public function renderInfo($title, $subtitle, $description=false) { + $output = wordwrap(strtoupper($title), self::config()->columns) . "\n"; + $output .= wordwrap($subtitle, self::config()->columns) . "\n"; + $output .= str_repeat('-', min(self::config()->columns, max(strlen($title), strlen($subtitle)))) . "\n"; + $output .= wordwrap($description, self::config()->columns) . "\n\n"; + + return $output; } - public function writeVariable($val, $caller) { - echo PHP_EOL; - echo SS_Cli::text(str_repeat('=', self::config()->columns), 'green'); - echo PHP_EOL; - echo SS_Cli::text($this->formatCaller($caller), 'blue', null, true); - echo PHP_EOL.PHP_EOL; + public function renderVariable($val, $caller) { + $output = PHP_EOL; + $output .= SS_Cli::text(str_repeat('=', self::config()->columns), 'green'); + $output .= PHP_EOL; + $output .= SS_Cli::text($this->formatCaller($caller), 'blue', null, true); + $output .= PHP_EOL.PHP_EOL; if (is_string($val)) { - print_r(wordwrap($val, self::config()->columns)); + $output .= wordwrap($val, self::config()->columns); } else { - print_r($val); + $output .= var_export($val, true); } - echo PHP_EOL; - echo SS_Cli::text(str_repeat('=', self::config()->columns), 'green'); - echo PHP_EOL; + $output .= PHP_EOL; + $output .= SS_Cli::text(str_repeat('=', self::config()->columns), 'green'); + $output .= PHP_EOL; + + return $output; } } diff --git a/dev/DebugView.php b/dev/DebugView.php index 3ee580917..5e73ed807 100644 --- a/dev/DebugView.php +++ b/dev/DebugView.php @@ -11,7 +11,8 @@ * @package framework * @subpackage dev */ -class DebugView extends Object { +class DebugView extends Object +{ /** * Column size to wrap long strings to @@ -100,9 +101,66 @@ class DebugView extends Object { } /** - * Render HTML header for development views + * @deprecated 4.0.0:5.0.0 Use renderHeader() instead */ public function writeHeader() { + 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() { $url = htmlentities( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'], ENT_COMPAT, @@ -115,41 +173,55 @@ class DebugView extends Object { 'css/debug.css' ); - echo '' . $url . ''; - echo ''; - echo ''; - echo ''; + $output = '' . $url . ''; + $output .= ''; + $output .= ''; + $output .= ''; + + return $output; } /** * Render the information header for the view * - * @param string $title - * @param string $title + * @param string $title The main title + * @param string $subtitle The subtitle + * @param string|false $description The description to show + * @return string */ - public function writeInfo($title, $subtitle, $description=false) { - echo '
'; - echo "

" . Convert::raw2xml($title) . "

"; - if($subtitle) echo "

" . Convert::raw2xml($subtitle) . "

"; + public function renderInfo($title, $subtitle, $description=false) { + $output = '
'; + $output .= "

" . Convert::raw2xml($title) . "

"; + if($subtitle) $output .= "

" . Convert::raw2xml($subtitle) . "

"; if ($description) { - echo "

$description

"; + $output .= "

$description

"; } else { - echo $this->Breadcrumbs(); + $output .= $this->Breadcrumbs(); } - echo '
'; + $output .= '
'; + + return $output; } /** * Render HTML footer for development views + * @return string */ - public function writeFooter() { - echo ""; + public function renderFooter() { + return ""; } /** - * Write information about the error to the screen + * 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 */ - public function writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext) { + public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) { $errorType = isset(self::$error_types[$errno]) ? self::$error_types[$errno] : self::$unknown_error; $httpRequestEnt = htmlentities($httpRequest, ENT_COMPAT, 'UTF-8'); if (ini_get('html_errors')) { @@ -157,51 +229,66 @@ class DebugView extends Object { } else { $errstr = Convert::raw2xml($errstr); } - echo '
'; - echo "

[" . $errorType['title'] . '] ' . $errstr . "

"; - echo "

$httpRequestEnt

"; - echo "

Line $errline in $errfile

"; - echo '
'; + $output = '
'; + $output .= "

[" . $errorType['title'] . '] ' . $errstr . "

"; + $output .= "

$httpRequestEnt

"; + $output .= "

Line $errline in $errfile

"; + $output .= '
'; + + return $output; } /** - * Write a fragment of the a source file - * @param $lines An array of file lines; the keys should be the original line numbers + * 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 */ - public function writeSourceFragment($lines, $errline) { - echo '

Source

'; - echo '
';
+	public function renderSourceFragment($lines, $errline) {
+		$output = '

Source

'; + $output .= '
';
 		foreach($lines as $offset => $line) {
 			$line = htmlentities($line, ENT_COMPAT, 'UTF-8');
 			if ($offset == $errline) {
-				echo "$offset $line";
+				$output .= "$offset $line";
 			} else {
-				echo "$offset $line";
+				$output .= "$offset $line";
 			}
 		}
-		echo '
'; + $output .= '
'; + + return $output; } /** - * Write a backtrace + * Render a call track + * + * @param array $trace The debug_backtrace() array + * @return string */ - public function writeTrace($trace) { - echo '

Trace

'; - echo SS_Backtrace::get_rendered_backtrace($trace); - echo '
'; + public function renderTrace($trace) { + $output = '

Trace

'; + $output .= SS_Backtrace::get_rendered_backtrace($trace); + $output .= ''; + + return $output; } /** - * @param string $text + * Render an arbitrary paragraph. + * + * @param string $text The HTML-escaped text to render + * @return string */ - public function writeParagraph($text) { - echo '

' . $text . '

'; + public function renderParagraph($text) { + return '

' . $text . '

'; } /** * Formats the caller of a method * - * @param array $caller + * @param array $caller * @return string */ protected function formatCaller($caller) { @@ -215,14 +302,17 @@ class DebugView extends Object { /** * Outputs a variable in a user presentable way * - * @param object $val - * @param array $caller Caller information + * @param object $val + * @param array $caller Caller information + * @return string */ - public function writeVariable($val, $caller) { - echo '
';
-		echo "" . $this->formatCaller($caller). " - \n";
-		if (is_string($val)) print_r(wordwrap($val, self::config()->columns));
-		else print_r($val);
-		echo '
'; + public function renderVariable($val, $caller) { + $output = '
';
+		$output .= "" . $this->formatCaller($caller). " - \n";
+		if (is_string($val)) $output .= wordwrap($val, self::config()->columns);
+		else $output .= var_export($val, true);
+		$output .= '
'; + + return $output; } }