mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-05 02:52:41 +00:00
New Debugger::exportVar() formatting.
Better resembles var_dump(). Includes types, and is less noisy to read than before.
This commit is contained in:
parent
183ffb29d7
commit
49f687b0c4
2 changed files with 123 additions and 99 deletions
|
@ -295,33 +295,43 @@ class DebuggerTest extends CakeTestCase {
|
||||||
$Controller = new Controller();
|
$Controller = new Controller();
|
||||||
$Controller->helpers = array('Html', 'Form');
|
$Controller->helpers = array('Html', 'Form');
|
||||||
$View = new View($Controller);
|
$View = new View($Controller);
|
||||||
$result = Debugger::exportVar($View);
|
$View->int = 2;
|
||||||
$expected = 'View
|
$View->float = 1.333;
|
||||||
View::$Helpers = HelperCollection object
|
|
||||||
View::$plugin = NULL
|
|
||||||
View::$name = ""
|
|
||||||
View::$passedArgs = array
|
|
||||||
View::$helpers = array
|
|
||||||
View::$viewPath = ""
|
|
||||||
View::$viewVars = array
|
|
||||||
View::$view = NULL
|
|
||||||
View::$layout = "default"
|
|
||||||
View::$layoutPath = NULL
|
|
||||||
View::$autoLayout = true
|
|
||||||
View::$ext = ".ctp"
|
|
||||||
View::$subDir = NULL
|
|
||||||
View::$theme = NULL
|
|
||||||
View::$cacheAction = false
|
|
||||||
View::$validationErrors = array
|
|
||||||
View::$hasRendered = false
|
|
||||||
View::$uuids = array
|
|
||||||
View::$output = false
|
|
||||||
View::$request = NULL
|
|
||||||
View::$elementCache = "default"';
|
|
||||||
|
|
||||||
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
|
$result = Debugger::exportVar($View);
|
||||||
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
|
$expected = <<<TEXT
|
||||||
$this->assertEqual($expected, $result);
|
object(View) {
|
||||||
|
Helpers => object(HelperCollection) {}
|
||||||
|
plugin => null
|
||||||
|
name => ''
|
||||||
|
passedArgs => array()
|
||||||
|
helpers => array(
|
||||||
|
(int) 0 => 'Html',
|
||||||
|
(int) 1 => 'Form'
|
||||||
|
)
|
||||||
|
viewPath => ''
|
||||||
|
viewVars => array()
|
||||||
|
view => null
|
||||||
|
layout => 'default'
|
||||||
|
layoutPath => null
|
||||||
|
autoLayout => true
|
||||||
|
ext => '.ctp'
|
||||||
|
subDir => null
|
||||||
|
theme => null
|
||||||
|
cacheAction => false
|
||||||
|
validationErrors => array()
|
||||||
|
hasRendered => false
|
||||||
|
uuids => array()
|
||||||
|
output => false
|
||||||
|
request => null
|
||||||
|
elementCache => 'default'
|
||||||
|
int => (int) 2
|
||||||
|
float => (float) 1.333
|
||||||
|
}
|
||||||
|
TEXT;
|
||||||
|
$result = str_replace(array("\r\n", "\n"), "", $result);
|
||||||
|
$expected = str_replace(array("\r\n", "\n"), "", $expected);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -357,23 +367,22 @@ class DebuggerTest extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
public function testDump() {
|
public function testDump() {
|
||||||
$var = array('People' => array(
|
$var = array('People' => array(
|
||||||
array(
|
array(
|
||||||
'name' => 'joeseph',
|
'name' => 'joeseph',
|
||||||
'coat' => 'technicolor',
|
'coat' => 'technicolor',
|
||||||
'hair_color' => 'brown'
|
'hair_color' => 'brown'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'name' => 'Shaft',
|
'name' => 'Shaft',
|
||||||
'coat' => 'black',
|
'coat' => 'black',
|
||||||
'hair' => 'black'
|
'hair' => 'black'
|
||||||
)
|
)
|
||||||
)
|
));
|
||||||
);
|
|
||||||
ob_start();
|
ob_start();
|
||||||
Debugger::dump($var);
|
Debugger::dump($var);
|
||||||
$result = ob_get_clean();
|
$result = ob_get_clean();
|
||||||
$expected = "<pre>array(\n\t\"People\" => array()\n)</pre>";
|
$expected = "<pre>array(\n\t'People' => array()\n)</pre>";
|
||||||
$this->assertEqual($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -383,16 +392,16 @@ class DebuggerTest extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
public function testGetInstance() {
|
public function testGetInstance() {
|
||||||
$result = Debugger::getInstance();
|
$result = Debugger::getInstance();
|
||||||
$this->assertIsA($result, 'Debugger');
|
$this->assertInstanceOf('Debugger', $result);
|
||||||
|
|
||||||
$result = Debugger::getInstance('DebuggerTestCaseDebugger');
|
$result = Debugger::getInstance('DebuggerTestCaseDebugger');
|
||||||
$this->assertIsA($result, 'DebuggerTestCaseDebugger');
|
$this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
|
||||||
|
|
||||||
$result = Debugger::getInstance();
|
$result = Debugger::getInstance();
|
||||||
$this->assertIsA($result, 'DebuggerTestCaseDebugger');
|
$this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
|
||||||
|
|
||||||
$result = Debugger::getInstance('Debugger');
|
$result = Debugger::getInstance('Debugger');
|
||||||
$this->assertIsA($result, 'Debugger');
|
$this->assertInstanceOf('Debugger', $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -419,8 +419,8 @@ class Debugger {
|
||||||
/**
|
/**
|
||||||
* Converts a variable to a string for debug output.
|
* Converts a variable to a string for debug output.
|
||||||
*
|
*
|
||||||
* *Note:* The following keys will have their contents replaced with
|
* *Note:* The following keys will have their contents
|
||||||
* `*****`:
|
* replaced with `*****`:
|
||||||
*
|
*
|
||||||
* - password
|
* - password
|
||||||
* - login
|
* - login
|
||||||
|
@ -434,93 +434,108 @@ class Debugger {
|
||||||
* shown in an error message if CakePHP is deployed in development mode.
|
* shown in an error message if CakePHP is deployed in development mode.
|
||||||
*
|
*
|
||||||
* @param string $var Variable to convert
|
* @param string $var Variable to convert
|
||||||
* @param integer $recursion
|
* @param integer $depth The depth to output to. Defaults to 2.
|
||||||
* @return string Variable as a formatted string
|
* @return string Variable as a formatted string
|
||||||
* @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::exportVar
|
* @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::exportVar
|
||||||
*/
|
*/
|
||||||
public static function exportVar($var, $recursion = 0) {
|
public static function exportVar($var, $depth = 3) {
|
||||||
switch (strtolower(gettype($var))) {
|
return self::_export($var, $depth, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function _export($var, $depth, $indent) {
|
||||||
|
switch (self::getType($var)) {
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
return ($var) ? 'true' : 'false';
|
return ($var) ? 'true' : 'false';
|
||||||
break;
|
break;
|
||||||
case 'integer':
|
case 'integer':
|
||||||
case 'double':
|
return '(int) ' . $var;
|
||||||
return $var;
|
case 'float':
|
||||||
|
return '(float) ' . $var;
|
||||||
break;
|
break;
|
||||||
case 'string':
|
case 'string':
|
||||||
if (trim($var) == "") {
|
if (trim($var) == '') {
|
||||||
return '""';
|
return "''";
|
||||||
}
|
}
|
||||||
return '"' . h($var) . '"';
|
return "'" . h($var) . "'";
|
||||||
break;
|
break;
|
||||||
case 'object':
|
|
||||||
return get_class($var) . "\n" . self::_object($var);
|
|
||||||
case 'array':
|
case 'array':
|
||||||
$var = array_merge($var, array_intersect_key(array(
|
return self::_array($var, $depth - 1, $indent + 1);
|
||||||
'password' => '*****',
|
|
||||||
'login' => '*****',
|
|
||||||
'host' => '*****',
|
|
||||||
'database' => '*****',
|
|
||||||
'port' => '*****',
|
|
||||||
'prefix' => '*****',
|
|
||||||
'schema' => '*****'
|
|
||||||
), $var));
|
|
||||||
|
|
||||||
$out = "array(";
|
|
||||||
$vars = array();
|
|
||||||
foreach ($var as $key => $val) {
|
|
||||||
if ($recursion >= 0) {
|
|
||||||
if (is_numeric($key)) {
|
|
||||||
$vars[] = "\n\t" . self::exportVar($val, $recursion - 1);
|
|
||||||
} else {
|
|
||||||
$vars[] = "\n\t" . self::exportVar($key, $recursion - 1)
|
|
||||||
. ' => ' . self::exportVar($val, $recursion - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$n = null;
|
|
||||||
if (!empty($vars)) {
|
|
||||||
$n = "\n";
|
|
||||||
}
|
|
||||||
return $out . implode(",", $vars) . "{$n})";
|
|
||||||
break;
|
break;
|
||||||
case 'resource':
|
case 'resource':
|
||||||
return strtolower(gettype($var));
|
return strtolower(gettype($var));
|
||||||
break;
|
break;
|
||||||
case 'null':
|
case 'null':
|
||||||
return 'null';
|
return 'null';
|
||||||
|
default:
|
||||||
|
return self::_object($var, $depth - 1, $indent + 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export an array type object. Filters out keys used in datasource configuration.
|
||||||
|
*
|
||||||
|
* @param array $var The array to export.
|
||||||
|
* @param integer $depth The current depth, used for recursion tracking.
|
||||||
|
* @return string Exported array.
|
||||||
|
*/
|
||||||
|
protected static function _array(array $var, $depth, $indent) {
|
||||||
|
$var = array_merge($var, array_intersect_key(array(
|
||||||
|
'password' => '*****',
|
||||||
|
'login' => '*****',
|
||||||
|
'host' => '*****',
|
||||||
|
'database' => '*****',
|
||||||
|
'port' => '*****',
|
||||||
|
'prefix' => '*****',
|
||||||
|
'schema' => '*****'
|
||||||
|
), $var));
|
||||||
|
|
||||||
|
$out = "array(";
|
||||||
|
$n = $break = $end = null;
|
||||||
|
if (!empty($var)) {
|
||||||
|
$n = "\n";
|
||||||
|
$break = "\n" . str_repeat("\t", $indent);
|
||||||
|
$end = "\n" . str_repeat("\t", $indent - 1);
|
||||||
|
}
|
||||||
|
$vars = array();
|
||||||
|
|
||||||
|
if ($depth >= 0) {
|
||||||
|
foreach ($var as $key => $val) {
|
||||||
|
$vars[] = $break . self::exportVar($key) .
|
||||||
|
' => ' .
|
||||||
|
self::_export($val, $depth - 1, $indent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $out . implode(',', $vars) . $end . ')';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles object to string conversion.
|
* Handles object to string conversion.
|
||||||
*
|
*
|
||||||
* @param string $var Object to convert
|
* @param string $var Object to convert
|
||||||
|
* @param integer $depth The current depth, used for tracking recursion.
|
||||||
* @return string
|
* @return string
|
||||||
* @see Debugger::exportVar()
|
* @see Debugger::exportVar()
|
||||||
*/
|
*/
|
||||||
protected static function _object($var) {
|
protected static function _object($var, $depth, $indent) {
|
||||||
$out = array();
|
$out = '';
|
||||||
|
$props = array();
|
||||||
|
|
||||||
if (is_object($var)) {
|
$className = get_class($var);
|
||||||
$className = get_class($var);
|
$out .= 'object(' . $className . ') {';
|
||||||
|
|
||||||
|
if ($depth > 0) {
|
||||||
|
$end = "\n" . str_repeat("\t", $indent - 1);
|
||||||
|
$break = "\n" . str_repeat("\t", $indent);
|
||||||
$objectVars = get_object_vars($var);
|
$objectVars = get_object_vars($var);
|
||||||
|
|
||||||
foreach ($objectVars as $key => $value) {
|
foreach ($objectVars as $key => $value) {
|
||||||
if (is_object($value)) {
|
$value = self::_export($value, $depth - 1, $indent);
|
||||||
$value = get_class($value) . ' object';
|
$props[] = "$key => " . $value;
|
||||||
} elseif (is_array($value)) {
|
|
||||||
$value = 'array';
|
|
||||||
} elseif ($value === null) {
|
|
||||||
$value = 'NULL';
|
|
||||||
} elseif (in_array(gettype($value), array('boolean', 'integer', 'double', 'string', 'array', 'resource'))) {
|
|
||||||
$value = Debugger::exportVar($value);
|
|
||||||
}
|
|
||||||
$out[] = "$className::$$key = " . $value;
|
|
||||||
}
|
}
|
||||||
|
$out .= $break . implode($break, $props) . $end;
|
||||||
}
|
}
|
||||||
return implode("\n", $out);
|
$out .= '}';
|
||||||
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue