diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index d9c0b873a..6edf16b05 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -15,6 +15,7 @@ use BackendAuth; use Twig_Environment; use Controller as BaseController; use Cms\Twig\Loader as TwigLoader; +use Cms\Twig\DebugExtension; use Cms\Twig\Extension as CmsTwigExtension; use Cms\Classes\FileHelper as CmsFileHelper; use System\Models\RequestLog; @@ -262,11 +263,13 @@ class Controller extends BaseController */ protected function initTwigEnvironment() { - $this->loader = new TwigLoader(); + $this->loader = new TwigLoader; + + $isDebugMode = Config::get('app.debug', false); $options = [ 'auto_reload' => true, - 'debug' => Config::get('app.debug', false), + 'debug' => $isDebugMode, ]; if (!Config::get('cms.twigNoCache')) $options['cache'] = storage_path().'/twig'; @@ -274,6 +277,9 @@ class Controller extends BaseController $this->twig = new Twig_Environment($this->loader, $options); $this->twig->addExtension(new CmsTwigExtension($this)); $this->twig->addExtension(new SystemTwigExtension); + + if ($isDebugMode) + $this->twig->addExtension(new DebugExtension($this)); } /** diff --git a/modules/cms/twig/DebugExtension.php b/modules/cms/twig/DebugExtension.php new file mode 100644 index 000000000..7bcf4fb38 --- /dev/null +++ b/modules/cms/twig/DebugExtension.php @@ -0,0 +1,235 @@ +controller = $controller; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new Twig_SimpleFunction('dump', [$this, 'runDump'], array('is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true)), + ); + } + + public function runDump(Twig_Environment $env, $context) + { + if (!$env->isDebug()) { + return; + } + + ob_start(); + + $count = func_num_args(); + if (2 === $count) { + $this->controllerMode = true; + $vars = []; + foreach ($context as $key => $value) { + if (!$value instanceof Twig_Template) { + $vars[$key] = $value; + } + } + + $this->dump($vars); + } else { + for ($i = 2; $i < $count; $i++) { + $this->dump(func_get_arg($i)); + } + } + + return ob_get_clean(); + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'debug'; + } + + /** + * Dump information about a variable + * + * @param mixed $variable Variable to dump + * @param string $caption Caption of the dump + * @return void + */ + public function dump($variables = null, $caption = 'Variable Inspector') + { + $info = []; + + if (!is_array($variables)) + $variables = [$variables]; + + $output = []; + $output[] = '
' . $html . ''; + } + + protected function makeTableHeader($caption) + { + $output = []; + $output[] = '