Commit d3c7dbd7 by Seldaek

* Plugins: improved the dump plugin, it now displays object's properties and…

* Plugins: improved the dump plugin, it now displays object's properties and optionally public methods (if the new show_methods arg is set to true) (thanks to Stephan Wentz for the patch) git-svn-id: svn://dwoo.org/dwoo/trunk@183 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 6ca4be14
[2008--] 1.0.0
* Plugins: improved the dump plugin, it now displays object's properties
and optionally public methods (if the new show_methods arg is set to true)
(thanks to Stephan Wentz for the patch)
* Lines containing only comments and whitespace are now entirely removed
* Adapters: Zend: Added parameters to provide a custom engine (extends Dwoo)
or a custom data class (extends Dwoo_Data) - thanks to Maxime Mérian for
or a custom data class (extends Dwoo_Data) - thanks to Maxime Mérian for
the patch
* Fixed a 1.0.0beta regression that messed with custom plugin directories
on Windows
......
......@@ -4,6 +4,7 @@
* Dumps values of the given variable, or the entire data if nothing provided
* <pre>
* * var : the variable to display
* * show_methods : if set to true, the public methods of any object encountered are also displayed
* </pre>
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
......@@ -23,8 +24,12 @@
*/
class Dwoo_Plugin_dump extends Dwoo_Plugin
{
public function process($var = '$')
protected $outputObjects;
protected $outputMethods;
public function process($var = '$', $show_methods = false)
{
$this->outputMethods = $show_methods;
if ($var === '$') {
$var = $this->dwoo->getData();
$out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data';
......@@ -32,8 +37,14 @@ class Dwoo_Plugin_dump extends Dwoo_Plugin
$out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump';
}
$this->outputObjects = array();
if (!is_array($var)) {
return $this->exportVar('', $var);
if (is_object($var)) {
return $this->exportObj('', $var);
} else {
return $this->exportVar('', $var);
}
}
$scope = $this->dwoo->getScope();
......@@ -54,12 +65,14 @@ class Dwoo_Plugin_dump extends Dwoo_Plugin
$out = '';
foreach ($var as $i=>$v) {
if (is_array($v) || (is_object($v) && $v instanceof Iterator)) {
$out .= $i.' ('.(is_array($v) ? 'array':'object:'.get_class($v)).')';
$out .= $i.' ('.(is_array($v) ? 'array':'object: '.get_class($v)).')';
if ($v===$scope) {
$out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">'.$this->export($v, $scope).'</div>';
} else {
$out .= ':<div style="padding-left:20px;">'.$this->export($v, $scope).'</div>';
}
} elseif (is_object($v)) {
$out .= $this->exportObj($i.' (object: '.get_class($v).'):', $v);
} else {
$out .= $this->exportVar($i.' = ', $v);
}
......@@ -73,12 +86,93 @@ class Dwoo_Plugin_dump extends Dwoo_Plugin
return $i.htmlentities(var_export($v, true)).'<br />';
} elseif (is_null($v)) {
return $i.'null<br />';
} elseif (is_object($v)) {
return $i.'object('.get_class($v).')<br />';
} elseif (is_resource($v)) {
return $i.'resource('.get_resource_type($v).')<br />';
} else {
return $i.htmlentities(var_export($v, true)).'<br />';
}
}
}
protected function exportObj($i, $obj)
{
if (array_search($obj, $this->outputObjects, true) !== false) {
return $i . ' [recursion, skipped]<br />';
}
$this->outputObjects[] = $obj;
$list = (array) $obj;
$protectedLength = strlen(get_class($obj)) + 2;
$out = array();
if ($this->outputMethods) {
$ref = new ReflectionObject($obj);
foreach ($ref->getMethods() as $method) {
if (!$method->isPublic()) {
continue;
}
if (empty($out['method'])) {
$out['method'] = '';
}
$params = array();
foreach ($method->getParameters() as $param) {
$params[] = ($param->isPassedByReference() ? '&':'') . '$'.$param->getName() . ($param->isOptional() ? ' = '.var_export($param->getDefaultValue(), true) : '');
}
$out['method'] .= '(method) ' . $method->getName() .'('.implode(', ', $params).')<br />';
}
}
foreach ($list as $attributeName => $attributeValue) {
if(property_exists($obj, $attributeName)) {
$key = 'public';
} elseif(substr($attributeName, 0, 3) === "\0*\0") {
$key = 'protected';
$attributeName = substr($attributeName, 3);
} else {
$key = 'private';
$attributeName = substr($attributeName, $protectedLength);
}
if (empty($out[$key])) {
$out[$key] = '';
}
$out[$key] .= '('.$key.') ';
if (is_array($attributeValue)) {
$out[$key] .= $attributeName.' (array):<br />
<div style="padding-left:20px;">'.$this->export($attributeValue, false).'</div>';
} elseif (is_object($attributeValue)) {
$out[$key] .= $this->exportObj($attributeName.' (object: '.get_class($attributeValue).'):', $attributeValue);
} else {
$out[$key] .= $this->exportVar($attributeName.' = ', $attributeValue);
}
}
$return = $i . '<br /><div style="padding-left:20px;">';
if (!empty($out['method'])) {
$return .= $out['method'];
}
if (!empty($out['public'])) {
$return .= $out['public'];
}
if (!empty($out['protected'])) {
$return .= $out['protected'];
}
if (!empty($out['private'])) {
$return .= $out['private'];
}
return $return . '</div>';
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment