diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index 9ad7aaf5..a9f11be3 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -24,8 +24,47 @@ class Helpers public static function dump($sql = NULL, $return = FALSE) { ob_start(); - if ($sql instanceof Result) { - $sql->dump(); + if ($sql instanceof Result && PHP_SAPI === 'cli') { + $hasColors = (substr(getenv('TERM'), 0, 5) === 'xterm'); + $maxLen = 0; + foreach ($sql as $i => $row) { + if ($i === 0) { + foreach ($row as $col => $foo) { + $len = mb_strlen($col); + $maxLen = max($len, $maxLen); + } + } + + echo $hasColors ? "\033[1;37m#row: $i\033[0m\n" : "#row: $i\n"; + foreach ($row as $col => $val) { + $spaces = $maxLen - mb_strlen($col) + 2; + echo "$col" . str_repeat(' ', $spaces) . "$val\n"; + } + echo "\n"; + } + + echo empty($row) ? "empty result set\n\n" : "\n"; + + } elseif ($sql instanceof Result) { + foreach ($sql as $i => $row) { + if ($i === 0) { + echo "\n\n\n\t\n\t\t\n"; + foreach ($row as $col => $foo) { + echo "\t\t\n"; + } + echo "\t\n\n\n"; + } + + echo "\t\n\t\t\n"; + foreach ($row as $col) { + echo "\t\t\n"; + } + echo "\t\n"; + } + + echo empty($row) + ? '

empty result set

' + : "\n
#row" . htmlSpecialChars($col) . "
", $i, "", htmlSpecialChars($col), "
\n"; } else { if ($sql === NULL) { diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index 69ea0110..3ceb9fe0 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -614,66 +614,7 @@ class Result implements IDataSource */ final public function dump() { - $i = 0; - $this->seek(0); - if (PHP_SAPI === 'cli') { - $hasColors = (substr(getenv('TERM'), 0, 5) === 'xterm'); - $maxLen = 0; - while ($row = $this->fetch()) { - if ($i === 0) { - foreach ($row as $col => $foo) { - $len = mb_strlen($col); - $maxLen = max($len, $maxLen); - } - } - - if ($hasColors) { - echo "\033[1;37m#row: $i\033[0m\n"; - } else { - echo "#row: $i\n"; - } - - foreach ($row as $col => $val) { - $spaces = $maxLen - mb_strlen($col) + 2; - echo "$col" . str_repeat(' ', $spaces) . "$val\n"; - } - - echo "\n"; - $i++; - } - - if ($i === 0) { - echo "empty result set\n"; - } - echo "\n"; - - } else { - while ($row = $this->fetch()) { - if ($i === 0) { - echo "\n\n\n\t\n\t\t\n"; - - foreach ($row as $col => $foo) { - echo "\t\t\n"; - } - - echo "\t\n\n\n"; - } - - echo "\t\n\t\t\n"; - foreach ($row as $col) { - //if (is_object($col)) $col = $col->__toString(); - echo "\t\t\n"; - } - echo "\t\n"; - $i++; - } - - if ($i === 0) { - echo '

empty result set

'; - } else { - echo "\n
#row" . htmlSpecialChars($col) . "
", $i, "", htmlSpecialChars($col), "
\n"; - } - } + echo Helpers::dump($this); } }