From 6b74efa4b0a5d8ae9f63232e01124dfe82fd0091 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 13 May 2017 10:20:18 +1000 Subject: [PATCH] Improves the DebugExtension It now uses the HtmlDumper from Illuminate to elaborate on each variable when clicked getName() no longer needed on Twig extensions --- modules/cms/twig/DebugExtension.php | 78 +++++++++++++++++++++-------- modules/cms/twig/Extension.php | 10 ---- modules/system/twig/Extension.php | 10 ---- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/modules/cms/twig/DebugExtension.php b/modules/cms/twig/DebugExtension.php index 80a4759dc..cf58389b5 100644 --- a/modules/cms/twig/DebugExtension.php +++ b/modules/cms/twig/DebugExtension.php @@ -7,6 +7,8 @@ use Cms\Classes\Controller; use Cms\Classes\ComponentBase; use Illuminate\Pagination\Paginator; use Illuminate\Support\Collection; +use Illuminate\Support\Debug\HtmlDumper; +use Symfony\Component\VarDumper\Cloner\VarCloner; use October\Rain\Database\Model; class DebugExtension extends Twig_Extension @@ -36,6 +38,9 @@ class DebugExtension extends Twig_Extension */ protected $commentMap = []; + /** + * @var array Blocked object methods that should not be included in the dump. + */ protected $blockMethods = [ 'componentDetails', 'defineProperties', @@ -57,7 +62,6 @@ class DebugExtension extends Twig_Extension /** * Returns a list of global functions to add to the existing list. - * * @return array An array of global functions */ public function getFunctions() @@ -128,19 +132,8 @@ class DebugExtension extends Twig_Extension return $result; } - /** - * Returns the name of the extension. - * - * @return string The extension name - */ - public function getName() - { - return 'debug'; - } - /** * Dump information about a variable - * * @param mixed $variables Variable to dump * @param mixed $caption Caption [and subcaption] of the dump * @return void @@ -217,13 +210,49 @@ class DebugExtension extends Twig_Extension $css = $this->getDataCss($variable); $output = []; $output[] = ''; - $output[] = ''.$this->evalKeyLabel($key).''; + $output[] = ''.$this->evalKeyLabel($key).''; $output[] = ''.$this->evalVarLabel($variable).''; $output[] = ''.$this->evalVarDesc($variable, $key).''; $output[] = ''; + $output[] = ''; + $output[] = ''.$this->evalVarDump($variable).''; + $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 = '
'; + $output .= $dumper->dump($cloner->cloneVar($variable), true); + $output .= '
'; + + return $output; + } + + /** + * Returns a variable name as HTML friendly. + * @param string $key + * @return string + */ protected function evalKeyLabel($key) { if ($this->variablePrefix === true) { @@ -385,6 +414,11 @@ class DebugExtension extends Twig_Extension // Object helpers // + /** + * Returns default comment information for a paginator object. + * @param Illuminate\Pagination\Paginator $paginator + * @return array + */ protected function paginatorToArray(Paginator $paginator) { $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) { $class = get_class($object); @@ -463,6 +501,11 @@ class DebugExtension extends Twig_Extension return $methods + $vars; } + /** + * Extracts the comment from a DocBlock + * @param ReflectionClass $reflectionObj + * @return string + */ protected function evalDocBlock($reflectionObj) { $comment = $reflectionObj->getDocComment(); @@ -476,14 +519,13 @@ class DebugExtension extends Twig_Extension return $comment; } - // // Style helpers // /** * Get the CSS string for the output data - * + * @param mixed $variable * @return string */ protected function getDataCss($variable) @@ -504,7 +546,6 @@ class DebugExtension extends Twig_Extension /** * Get the CSS string for the output container - * * @return string */ protected function getContainerCss() @@ -523,7 +564,6 @@ class DebugExtension extends Twig_Extension /** * Get the CSS string for the output header - * * @return string */ protected function getHeaderCss() @@ -540,7 +580,6 @@ class DebugExtension extends Twig_Extension /** * Get the CSS string for the output subheader - * * @return string */ protected function getSubheaderCss() @@ -558,7 +597,6 @@ class DebugExtension extends Twig_Extension /** * Convert a key/value pair array into a CSS string - * * @param array $rules List of rules to process * @return string */ diff --git a/modules/cms/twig/Extension.php b/modules/cms/twig/Extension.php index 7ff415832..44069d6b0 100644 --- a/modules/cms/twig/Extension.php +++ b/modules/cms/twig/Extension.php @@ -34,16 +34,6 @@ class Extension extends Twig_Extension $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. * diff --git a/modules/system/twig/Extension.php b/modules/system/twig/Extension.php index 21db7d5f4..4ed80df37 100644 --- a/modules/system/twig/Extension.php +++ b/modules/system/twig/Extension.php @@ -30,16 +30,6 @@ class Extension extends Twig_Extension $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. *