This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of git://github.com/zendframework/zf2 into string
- Loading branch information
47 parents
344bf4d
+
c2dcfb8
+
051a743
+
a18bb70
+
164830b
+
663ec7d
+
b65fb05
+
f2c66a4
+
e559f5a
+
5552bc5
+
f64b93e
+
f709e36
+
c458155
+
74b69b6
+
86b2d28
+
43f4776
+
d85e69b
+
0647e8a
+
8a71d1d
+
8572bf9
+
55fc535
+
23fa1d1
+
c504a46
+
715ca59
+
ca3e878
+
093972d
+
ecf6289
+
a6bf7c0
+
c096289
+
751264e
+
19613a0
+
3e91ac8
+
7279926
+
82fd41c
+
6c56755
+
a75bae0
+
ba49c46
+
05dad41
+
acfbafd
+
c7739fb
+
d817a3d
+
2788fd5
+
45930cd
+
01e2838
+
b622b9b
+
eddafc6
+
6777a89
commit 5b48ea5
Showing
3 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) "string"\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); | ||
} | ||
|
||
} |