Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop' of git://github.com/zendframework/zf2 into string
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 2 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zendframework/zend-debug",
"description": "Zend\\Debug component",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
Expand All @@ -13,7 +13,8 @@
}
},
"require": {
"php": ">=5.3.23"
"php": ">=5.3.3",
"zendframework/zend-escaper": "self.version"
},
"require-dev": {
"zendframework/zend-escaper": "2.*",
Expand Down
127 changes: 127 additions & 0 deletions src/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Debug.php
*/

namespace Zend\Debug;

use Zend\Escaper\Escaper;

/**
* Concrete class for generating debug dumps related to the output source.
*
* @category Zend
* @package Zend_Debug
*/
class Debug
{
/**
* @var Escaper
*/
protected static $escaper = null;

/**
* @var string
*/
protected static $sapi = null;

/**
* Get the current value of the debug output environment.
* This defaults to the value of PHP_SAPI.
*
* @return string;
*/
public static function getSapi()
{
if (static::$sapi === null) {
static::$sapi = PHP_SAPI;
}
return static::$sapi;
}

/**
* Set the debug output environment.
* Setting a value of null causes Zend_Debug to use PHP_SAPI.
*
* @param string $sapi
* @return void;
*/
public static function setSapi($sapi)
{
static::$sapi = $sapi;
}

/**
* Set Escaper instance
*
* @param Escaper $escaper
*/
public static function setEscaper(Escaper $escaper)
{
static::$escaper = $escaper;
}

/**
* Get Escaper instance
*
* Lazy loads an instance if none provided.
*
* @return Escaper
*/
public static function getEscaper()
{
if (null === static::$escaper) {
static::setEscaper(new Escaper());
}
return static::$escaper;
}

/**
* Debug helper function. This is a wrapper for var_dump() that adds
* the <pre /> 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 = '<pre>'
. $label
. $output
. '</pre>';
}

if ($echo) {
echo $output;
}
return $output;
}

}
101 changes: 101 additions & 0 deletions test/DebugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_DebugTest.php
*/

namespace ZendTest;

use Zend\Debug\Debug;

/**
* @category Zend
* @package Zend_Debug
* @subpackage UnitTests
* @group Zend_Debug
*/
class DebugTest extends \PHPUnit_Framework_TestCase
{

public function testDebugDefaultSapi()
{
$sapi = php_sapi_name();
Debug::setSapi(null);
$data = 'string';
$result = Debug::Dump($data, null, false);
$this->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(
"<pre>string(6) \"string\"\n</pre>",
"<pre>string(6) &quot;string&quot;\n</pre>",
)
);
}

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("<pre>", $result);
$this->assertContains("</pre>", $result);
}

}

0 comments on commit 5b48ea5

Please sign in to comment.