Improves the DebugExtension

It now uses the HtmlDumper from Illuminate to elaborate on each variable when clicked
getName() no longer needed on Twig extensions
This commit is contained in:
Samuel Georges 2017-05-13 10:20:18 +10:00
parent 2b5709adb9
commit 6b74efa4b0
3 changed files with 58 additions and 40 deletions

View File

@ -7,6 +7,8 @@ use Cms\Classes\Controller;
use Cms\Classes\ComponentBase; use Cms\Classes\ComponentBase;
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Debug\HtmlDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use October\Rain\Database\Model; use October\Rain\Database\Model;
class DebugExtension extends Twig_Extension class DebugExtension extends Twig_Extension
@ -36,6 +38,9 @@ class DebugExtension extends Twig_Extension
*/ */
protected $commentMap = []; protected $commentMap = [];
/**
* @var array Blocked object methods that should not be included in the dump.
*/
protected $blockMethods = [ protected $blockMethods = [
'componentDetails', 'componentDetails',
'defineProperties', 'defineProperties',
@ -57,7 +62,6 @@ class DebugExtension extends Twig_Extension
/** /**
* Returns a list of global functions to add to the existing list. * Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions * @return array An array of global functions
*/ */
public function getFunctions() public function getFunctions()
@ -128,19 +132,8 @@ class DebugExtension extends Twig_Extension
return $result; return $result;
} }
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'debug';
}
/** /**
* Dump information about a variable * Dump information about a variable
*
* @param mixed $variables Variable to dump * @param mixed $variables Variable to dump
* @param mixed $caption Caption [and subcaption] of the dump * @param mixed $caption Caption [and subcaption] of the dump
* @return void * @return void
@ -217,13 +210,49 @@ class DebugExtension extends Twig_Extension
$css = $this->getDataCss($variable); $css = $this->getDataCss($variable);
$output = []; $output = [];
$output[] = '<tr>'; $output[] = '<tr>';
$output[] = '<td style="'.$css.'">'.$this->evalKeyLabel($key).'</td>'; $output[] = '<td style="'.$css.';cursor:pointer" onclick="'.$this->evalToggleDumpOnClick().'">'.$this->evalKeyLabel($key).'</td>';
$output[] = '<td style="'.$css.'">'.$this->evalVarLabel($variable).'</td>'; $output[] = '<td style="'.$css.'">'.$this->evalVarLabel($variable).'</td>';
$output[] = '<td style="'.$css.'">'.$this->evalVarDesc($variable, $key).'</td>'; $output[] = '<td style="'.$css.'">'.$this->evalVarDesc($variable, $key).'</td>';
$output[] = '</tr>'; $output[] = '</tr>';
$output[] = '<tr>';
$output[] = '<td colspan="3">'.$this->evalVarDump($variable).'</td>';
$output[] = '</tr>';
return implode(PHP_EOL, $output); return implode(PHP_EOL, $output);
} }
/**
* Builds JavaScript for toggling the dump container
* @return string
*/
protected function evalToggleDumpOnClick()
{
$output = "var d=this.parentElement.nextElementSibling.getElementsByTagName('div')[0];";
$output .= "d.style.display=='none'?d.style.display='block':d.style.display='none'";
return $output;
}
/**
* Dumps a variable using HTML Dumper, wrapped in a hidden DIV element.
* @param mixed $variable
* @return string
*/
protected function evalVarDump($variable)
{
$dumper = new HtmlDumper;
$cloner = new VarCloner;
$output = '<div style="display:none">';
$output .= $dumper->dump($cloner->cloneVar($variable), true);
$output .= '</div>';
return $output;
}
/**
* Returns a variable name as HTML friendly.
* @param string $key
* @return string
*/
protected function evalKeyLabel($key) protected function evalKeyLabel($key)
{ {
if ($this->variablePrefix === true) { if ($this->variablePrefix === true) {
@ -385,6 +414,11 @@ class DebugExtension extends Twig_Extension
// Object helpers // Object helpers
// //
/**
* Returns default comment information for a paginator object.
* @param Illuminate\Pagination\Paginator $paginator
* @return array
*/
protected function paginatorToArray(Paginator $paginator) protected function paginatorToArray(Paginator $paginator)
{ {
$this->commentMap = [ $this->commentMap = [
@ -410,7 +444,11 @@ class DebugExtension extends Twig_Extension
]; ];
} }
/**
* Returns a map of an object as an array, containing methods and properties.
* @param mixed $object
* @return array
*/
protected function objectToArray($object) protected function objectToArray($object)
{ {
$class = get_class($object); $class = get_class($object);
@ -463,6 +501,11 @@ class DebugExtension extends Twig_Extension
return $methods + $vars; return $methods + $vars;
} }
/**
* Extracts the comment from a DocBlock
* @param ReflectionClass $reflectionObj
* @return string
*/
protected function evalDocBlock($reflectionObj) protected function evalDocBlock($reflectionObj)
{ {
$comment = $reflectionObj->getDocComment(); $comment = $reflectionObj->getDocComment();
@ -476,14 +519,13 @@ class DebugExtension extends Twig_Extension
return $comment; return $comment;
} }
// //
// Style helpers // Style helpers
// //
/** /**
* Get the CSS string for the output data * Get the CSS string for the output data
* * @param mixed $variable
* @return string * @return string
*/ */
protected function getDataCss($variable) protected function getDataCss($variable)
@ -504,7 +546,6 @@ class DebugExtension extends Twig_Extension
/** /**
* Get the CSS string for the output container * Get the CSS string for the output container
*
* @return string * @return string
*/ */
protected function getContainerCss() protected function getContainerCss()
@ -523,7 +564,6 @@ class DebugExtension extends Twig_Extension
/** /**
* Get the CSS string for the output header * Get the CSS string for the output header
*
* @return string * @return string
*/ */
protected function getHeaderCss() protected function getHeaderCss()
@ -540,7 +580,6 @@ class DebugExtension extends Twig_Extension
/** /**
* Get the CSS string for the output subheader * Get the CSS string for the output subheader
*
* @return string * @return string
*/ */
protected function getSubheaderCss() protected function getSubheaderCss()
@ -558,7 +597,6 @@ class DebugExtension extends Twig_Extension
/** /**
* Convert a key/value pair array into a CSS string * Convert a key/value pair array into a CSS string
*
* @param array $rules List of rules to process * @param array $rules List of rules to process
* @return string * @return string
*/ */

View File

@ -34,16 +34,6 @@ class Extension extends Twig_Extension
$this->controller = $controller; $this->controller = $controller;
} }
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'CMS';
}
/** /**
* Returns a list of functions to add to the existing list. * Returns a list of functions to add to the existing list.
* *

View File

@ -30,16 +30,6 @@ class Extension extends Twig_Extension
$this->markupManager = MarkupManager::instance(); $this->markupManager = MarkupManager::instance();
} }
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'System';
}
/** /**
* Returns a list of functions to add to the existing list. * Returns a list of functions to add to the existing list.
* *