TwigExtension.php in Twig VarDumper 8
File
src/TwigExtension.php
View source
<?php
namespace Drupal\twig_vardumper;
class TwigExtension extends \Twig_Extension {
public function getFunctions() {
return [
new \Twig_SimpleFunction('dump', [
$this,
'drupalDump',
], [
'needs_context' => TRUE,
'needs_environment' => TRUE,
]),
new \Twig_SimpleFunction('vardumper', [
$this,
'drupalDump',
], [
'needs_context' => TRUE,
'needs_environment' => TRUE,
]),
];
}
public function getName() {
return 'twig_vardumper';
}
public function drupalDump(\Twig_Environment $env, array $context) {
if (!$env
->isDebug()) {
return;
}
$var_dumper = '\\Symfony\\Component\\VarDumper\\VarDumper';
if (class_exists($var_dumper)) {
$count = func_num_args();
if (2 === $count) {
$vars = [];
foreach ($context as $key => $value) {
if (!$value instanceof \Twig_Template) {
$vars[$key] = $value;
}
}
call_user_func($var_dumper . '::dump', $vars);
}
else {
for ($i = 2; $i < $count; ++$i) {
call_user_func($var_dumper . '::dump', func_get_arg($i));
}
}
}
else {
trigger_error('Could not dump the variable because symfony/var-dumper component is not installed.', E_USER_WARNING);
}
}
}
Classes
Name |
Description |
TwigExtension |
Twig extension with some useful functions and filters. |