Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DebugExtension.php

class DebugExtension

Same name in this branch
  1. 11.1.x vendor/twig/twig/src/Extension/DebugExtension.php \Twig\Extension\DebugExtension

A class providing Drupal Twig Debug extension.

Hierarchy

  • class \Twig\Extension\AbstractExtension implements \Twig\Extension\ExtensionInterface
    • class \Drupal\Core\Template\DebugExtension extends \Twig\Extension\AbstractExtension

Expanded class hierarchy of DebugExtension

File

core/lib/Drupal/Core/Template/DebugExtension.php, line 12

Namespace

Drupal\Core\Template
View source
final class DebugExtension extends AbstractExtension {
    
    /**
     * The Symfony VarDumper class.
     *
     * Defined as a string because the Symfony VarDumper does not always exist.
     *
     * @var string
     */
    private const SYMFONY_VAR_DUMPER_CLASS = '\\Symfony\\Component\\VarDumper\\VarDumper';
    
    /**
     * {@inheritdoc}
     */
    public function getFunctions() : array {
        // Override Twig built in debugger when Symfony VarDumper is available to
        // improve developer experience.
        // @see \Twig\Extension\DebugExtension
        // @see \Symfony\Component\VarDumper\VarDumper
        if (class_exists(self::SYMFONY_VAR_DUMPER_CLASS)) {
            return [
                new TwigFunction('dump', [
                    self::class,
                    'dump',
                ], [
                    'needs_context' => TRUE,
                    'needs_environment' => TRUE,
                    'is_variadic' => TRUE,
                ]),
            ];
        }
        return [];
    }
    
    /**
     * Dumps information about variables using Symfony VarDumper.
     *
     * @param \Twig\Environment $env
     *   The Twig environment.
     * @param array $context
     *   Variables from the Twig template.
     * @param array $variables
     *   (optional) Variable(s) to dump.
     */
    public static function dump(Environment $env, array $context, ...$variables) : void {
        if (!$env->isDebug()) {
            return;
        }
        if (class_exists(self::SYMFONY_VAR_DUMPER_CLASS)) {
            if (func_num_args() === 2) {
                call_user_func(self::SYMFONY_VAR_DUMPER_CLASS . '::dump', $context);
            }
            else {
                array_walk($variables, self::SYMFONY_VAR_DUMPER_CLASS . '::dump');
            }
        }
        else {
            throw new \LogicException('Could not dump the variable because symfony/var-dumper component is not installed.');
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractExtension::getFilters public function Returns a list of filters to add to the existing list. Overrides ExtensionInterface::getFilters 4
AbstractExtension::getNodeVisitors public function Returns the node visitor instances to add to the existing list. Overrides ExtensionInterface::getNodeVisitors 9
AbstractExtension::getOperators public function Returns a list of operators to add to the existing list. Overrides ExtensionInterface::getOperators 1
AbstractExtension::getTests public function Returns a list of tests to add to the existing list. Overrides ExtensionInterface::getTests 2
AbstractExtension::getTokenParsers public function Returns the token parser instances to add to the existing list. Overrides ExtensionInterface::getTokenParsers 5
DebugExtension::dump public static function Dumps information about variables using Symfony VarDumper.
DebugExtension::getFunctions public function Returns a list of functions to add to the existing list. Overrides AbstractExtension::getFunctions
DebugExtension::SYMFONY_VAR_DUMPER_CLASS private constant The Symfony VarDumper class.
RSS feed
Powered by Drupal