From 3c5e51a9f1fa8aca4d806b8573194ff7547abeba Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Sat, 5 Jul 2014 14:37:06 +1200 Subject: [PATCH] API Debug::dump in CLI no longer generates HTML. Uses colours. API Column size is configurable in DebugView --- dev/CliDebugView.php | 26 ++++++++++++++++++++------ dev/Debug.php | 22 +++++++++++++--------- dev/DebugView.php | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/dev/CliDebugView.php b/dev/CliDebugView.php index 9c39c486d..86ba02b19 100644 --- a/dev/CliDebugView.php +++ b/dev/CliDebugView.php @@ -41,7 +41,7 @@ class CliDebugView extends DebugView { foreach($lines as $offset => $line) { echo ($offset == $errline) ? "* " : " "; echo str_pad("$offset:",5); - echo wordwrap($line, 100, "\n "); + echo wordwrap($line, self::config()->columns, "\n "); } echo "\n"; } @@ -61,11 +61,25 @@ class CliDebugView extends DebugView { * @param string $title */ public function writeInfo($title, $subtitle, $description=false) { - echo wordwrap(strtoupper($title),100) . "\n"; - echo wordwrap($subtitle,100) . "\n"; - echo str_repeat('-',min(100,max(strlen($title),strlen($subtitle)))) . "\n"; - echo wordwrap($description,100) . "\n\n"; + 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 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; + if (is_string($val)) { + print_r(wordwrap($val, self::config()->columns)); + } else { + print_r($val); + } + echo PHP_EOL; + echo SS_Cli::text(str_repeat('=', self::config()->columns), 'green'); + echo PHP_EOL; + } } - diff --git a/dev/Debug.php b/dev/Debug.php index 65d1ed8e6..7123a31d2 100644 --- a/dev/Debug.php +++ b/dev/Debug.php @@ -71,6 +71,11 @@ class Debug { } + /** + * Returns the caller for a specific method + * + * @return array + */ public static function caller() { $bt = debug_backtrace(); $caller = $bt[2]; @@ -102,12 +107,7 @@ class Debug { * @param mixed $val */ public static function dump($val) { - echo '
';
-		$caller = Debug::caller();
-		echo "" . basename($caller['file']) . ":$caller[line] - \n";
-		if (is_string($val)) print_r(wordwrap($val, 100));
-		else print_r($val);
-		echo '
'; + self::create_debug_view()->writeVariable($val, self::caller()); } /** @@ -366,13 +366,17 @@ class Debug { } return false; } - + /** * Create an instance of an appropriate DebugView object. + * + * @return DebugView */ public static function create_debug_view() { - if(Director::is_cli() || Director::is_ajax()) return new CliDebugView(); - else return new DebugView(); + $service = Director::is_cli() || Director::is_ajax() + ? 'CliDebugView' + : 'DebugView'; + return Injector::inst()->get($service); } /** diff --git a/dev/DebugView.php b/dev/DebugView.php index f65e1ead7..e2824c35d 100644 --- a/dev/DebugView.php +++ b/dev/DebugView.php @@ -12,6 +12,14 @@ * @subpackage dev */ class DebugView extends Object { + + /** + * Column size to wrap long strings to + * + * @var int + * @config + */ + private static $columns = 100; protected static $error_types = array( E_USER_ERROR => array( @@ -180,5 +188,32 @@ class DebugView extends Object { public function writeParagraph($text) { echo '

' . $text . '

'; } + + /** + * Formats the caller of a method + * + * @param array $caller + * @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; + } + + /** + * Outputs a variable in a user presentable way + * + * @param object $val + * @param array $caller Caller information + */ + 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 '
'; + } } -