diff --git a/composer.json b/composer.json index 1412147..cda0fd5 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "zendframework/zend-debug", - "description": "Zend\\Debug component", + "description": " ", "license": "BSD-3-Clause", "keywords": [ "zf2", @@ -13,7 +13,8 @@ } }, "require": { - "php": ">=5.3.23" + "php": ">=5.3.3", + "zendframework/zend-escaper": "self.version" }, "require-dev": { "zendframework/zend-escaper": "2.*", diff --git a/src/Debug.php b/src/Debug.php new file mode 100644 index 0000000..43c907b --- /dev/null +++ b/src/Debug.php @@ -0,0 +1,127 @@ + tags, cleans up newlines and indents, and runs + * htmlentities() before output. + * + * @param mixed $var The variable to dump. + * @param string $label OPTIONAL Label to prepend to output. + * @param bool $echo OPTIONAL Echo output if true. + * @return string + */ + public static function dump($var, $label=null, $echo=true) + { + // format the label + $label = ($label===null) ? '' : rtrim($label) . ' '; + + // var_dump the variable into a buffer and keep the output + ob_start(); + var_dump($var); + $output = ob_get_clean(); + + // neaten the newlines and indents + $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output); + if (static::getSapi() == 'cli') { + $output = PHP_EOL . $label + . PHP_EOL . $output + . PHP_EOL; + } else { + if (!extension_loaded('xdebug')) { + $output = static::getEscaper()->escapeHtml($output); + } + + $output = '
' + . $label + . $output + . ''; + } + + if ($echo) { + echo $output; + } + return $output; + } + +} diff --git a/test/DebugTest.php b/test/DebugTest.php new file mode 100644 index 0000000..2bb5aa7 --- /dev/null +++ b/test/DebugTest.php @@ -0,0 +1,101 @@ +assertEquals($sapi, Debug::getSapi()); + } + + public function testDebugDump() + { + Debug::setSapi('cli'); + $data = 'string'; + $result = Debug::Dump($data, null, false); + $result = str_replace(array(PHP_EOL, "\n"), '_', $result); + $expected = "__string(6) \"string\"__"; + $this->assertEquals($expected, $result); + } + + public function testDebugCgi() + { + Debug::setSapi('cgi'); + $data = 'string'; + $result = Debug::Dump($data, null, false); + + // Has to check for two strings, because xdebug internally handles CLI vs Web + $this->assertContains($result, + array( + "
string(6) \"string\"\n", + "
string(6) "string"\n", + ) + ); + } + + public function testDebugDumpEcho() + { + Debug::setSapi('cli'); + $data = 'string'; + + ob_start(); + $result1 = Debug::Dump($data, null, true); + $result2 = ob_get_contents(); + ob_end_clean(); + + $this->assertContains('string(6) "string"', $result1); + $this->assertEquals($result1, $result2); + } + + public function testDebugDumpLabel() + { + Debug::setSapi('cli'); + $data = 'string'; + $label = 'LABEL'; + $result = Debug::Dump($data, $label, false); + $result = str_replace(array(PHP_EOL, "\n"), '_', $result); + $expected = "_{$label} _string(6) \"string\"__"; + $this->assertEquals($expected, $result); + } + + /** + * @group ZF-4136 + * @group ZF-1663 + */ + public function testXdebugEnabledAndNonCliSapiDoesNotEscapeSpecialChars() + { + if (!extension_loaded('xdebug')) { + $this->markTestSkipped("This test only works in combination with xdebug."); + } + + Debug::setSapi('apache'); + $a = array("a" => "b"); + + $result = Debug::dump($a, "LABEL", false); + $this->assertContains("
", $result); + $this->assertContains("", $result); + } + +}