Skip to content
Permalink
Browse files

adds setOutputMask/outputMask

  • Loading branch information...
thinkingmedia
thinkingmedia committed Nov 3, 2016
1 parent 24c74f9 commit 1601aa1ebd0351ef0176f88766b8b0b1d031b702
Showing with 54 additions and 11 deletions.
  1. +39 −9 src/Error/Debugger.php
  2. +15 −2 tests/TestCase/Error/DebuggerTest.php
@@ -41,7 +41,7 @@ class Debugger
* @var array
*/
protected $_defaultConfig = [
'mask' => []
'outputMask' => []
];
/**
@@ -198,6 +198,32 @@ public static function configInstance($key = null, $value = null, $merge = true)
return static::getInstance()->config($key);
}
/**
* Reads the current output masking.
*
* @return array
*/
public static function outputMask()
{
return static::configInstance('outputMask');
}
/**
* Sets configurable masking of debugger output by property name and array key names.
*
* ### Example
*
* Debugger::setOutputMask(['password' => '[*************]');
*
* @param array $value An array where keys are replaced by their values in output.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
* @return void
*/
public static function setOutputMask(array $value, $merge = true)
{
static::configInstance('outputMask', $value, $merge);
}
/**
* Recursively formats and outputs the contents of the supplied variable.
*
@@ -534,13 +560,13 @@ protected static function _array(array $var, $depth, $indent)
$vars = [];
if ($depth >= 0) {
$mask = (array)static::configInstance('mask');
$outputMask = (array)static::outputMask();
foreach ($var as $key => $val) {
// Sniff for globals as !== explodes in < 5.4
if ($key === 'GLOBALS' && is_array($val) && isset($val['GLOBALS'])) {
$val = '[recursion]';
} elseif (array_key_exists($key, $mask)) {
$val = (string)$mask[$key];
} elseif (array_key_exists($key, $outputMask)) {
$val = (string)$outputMask[$key];
} elseif ($val !== $var) {
$val = static::_export($val, $depth, $indent);
}
@@ -558,7 +584,7 @@ protected static function _array(array $var, $depth, $indent)
/**
* Handles object to string conversion.
*
* @param string $var Object to convert.
* @param object $var Object to convert.
* @param int $depth The current depth, used for tracking recursion.
* @param int $indent The current indentation level.
* @return string
@@ -587,10 +613,10 @@ protected static function _object($var, $depth, $indent)
}
if ($depth > 0) {
$mask = (array)static::configInstance('mask');
$outputMask = (array)static::outputMask();
$objectVars = get_object_vars($var);
foreach ($objectVars as $key => $value) {
$value = array_key_exists($key, $mask) ? $mask[$key] : static::_export($value, $depth - 1, $indent);
$value = array_key_exists($key, $outputMask) ? $outputMask[$key] : static::_export($value, $depth - 1, $indent);
$props[] = "$key => " . $value;
}
@@ -612,7 +638,7 @@ protected static function _object($var, $depth, $indent)
'[%s] %s => %s',
$visibility,
$key,
array_key_exists($key, $mask) ? $mask[$key] : $value
array_key_exists($key, $outputMask) ? $outputMask[$key] : $value
);
}
}
@@ -643,6 +669,8 @@ public static function outputAs($format = null)
throw new InvalidArgumentException('Invalid Debugger output format.');
}
$self->_outputFormat = $format;
return null;
}
/**
@@ -786,7 +814,9 @@ public function outputError($data)
$links = implode(' ', $links);
if (isset($tpl['callback']) && is_callable($tpl['callback'])) {
return call_user_func($tpl['callback'], $data, compact('links', 'info'));
call_user_func($tpl['callback'], $data, compact('links', 'info'));
return;
}
echo Text::insert($tpl['error'], compact('links', 'info') + $data, $insertOpts);
}
@@ -567,14 +567,27 @@ public function testDebugInfo()
$this->assertEquals($expected, $result);
}
/**
* Tests reading the output mask settings.
*/
public function testSetOutputMask()
{
Debugger::setOutputMask(['password' => '[**********]']);
$this->assertEquals(['password' => '[**********]'], Debugger::outputMask());
Debugger::setOutputMask(['serial' => 'XXXXXX']);
$this->assertEquals(['password' => '[**********]', 'serial' => 'XXXXXX'], Debugger::outputMask());
Debugger::setOutputMask([], false);
$this->assertEquals([], Debugger::outputMask());
}
/**
* Tests the masking of an array key.
*
* @return void
*/
public function testMaskArray()
{
Debugger::configInstance('mask', ['password' => '[**********]']);
Debugger::setOutputMask(['password' => '[**********]']);
$result = Debugger::exportVar(['password' => 'pass1234']);
$expected = "['password'=>[**********]]";
$this->assertEquals($expected, preg_replace('/\s+/', '', $result));
@@ -587,7 +600,7 @@ public function testMaskArray()
*/
public function testMaskObject()
{
Debugger::configInstance('mask', ['password' => '[**********]']);
Debugger::setOutputMask(['password' => '[**********]']);
$object = new SecurityThing();
$result = Debugger::exportVar($object);
$expected = "object(Cake\\Test\\TestCase\\Error\\SecurityThing){password=>[**********]}";

0 comments on commit 1601aa1

Please sign in to comment.
You can’t perform that action at this time.