1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\Extension { 13use Twig\TwigFunction; 14 15/** 16 * @final 17 */ 18class DebugExtension extends AbstractExtension 19{ 20 public function getFunctions() 21 { 22 // dump is safe if var_dump is overridden by xdebug 23 $isDumpOutputHtmlSafe = \extension_loaded('xdebug') 24 // false means that it was not set (and the default is on) or it explicitly enabled 25 && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump')) 26 // false means that it was not set (and the default is on) or it explicitly enabled 27 // xdebug.overload_var_dump produces HTML only when html_errors is also enabled 28 && (false === ini_get('html_errors') || ini_get('html_errors')) 29 || 'cli' === \PHP_SAPI 30 ; 31 32 return [ 33 new TwigFunction('dump', 'twig_var_dump', ['is_safe' => $isDumpOutputHtmlSafe ? ['html'] : [], 'needs_context' => true, 'needs_environment' => true, 'is_variadic' => true]), 34 ]; 35 } 36 37 public function getName() 38 { 39 return 'debug'; 40 } 41} 42 43class_alias('Twig\Extension\DebugExtension', 'Twig_Extension_Debug'); 44} 45 46namespace { 47use Twig\Environment; 48use Twig\Template; 49use Twig\TemplateWrapper; 50 51function twig_var_dump(Environment $env, $context, array $vars = []) 52{ 53 if (!$env->isDebug()) { 54 return; 55 } 56 57 ob_start(); 58 59 if (!$vars) { 60 $vars = []; 61 foreach ($context as $key => $value) { 62 if (!$value instanceof Template && !$value instanceof TemplateWrapper) { 63 $vars[$key] = $value; 64 } 65 } 66 67 var_dump($vars); 68 } else { 69 foreach ($vars as $var) { 70 var_dump($var); 71 } 72 } 73 74 return ob_get_clean(); 75} 76} 77