function Debug::guessTwigFunctionParameters

Same name in other branches
  1. 4.x src/Twig/Extension/Debug.php \Drupal\devel\Twig\Extension\Debug::guessTwigFunctionParameters()

Gets the twig function parameters for the current invocation.

Return value

array The detected twig function parameters.

2 calls to Debug::guessTwigFunctionParameters()
Debug::doDump in src/Twig/Extension/Debug.php
Writes the debug information for Twig templates.
Debug::message in src/Twig/Extension/Debug.php
Provides debug function to Twig templates.

File

src/Twig/Extension/Debug.php, line 247

Class

Debug
Provides the Devel debugging function within Twig templates.

Namespace

Drupal\devel\Twig\Extension

Code

protected function guessTwigFunctionParameters() : array {
    $callee = NULL;
    $template = NULL;
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
    foreach ($backtrace as $index => $trace) {
        if (isset($trace['object']) && $trace['object'] instanceof Template) {
            $template = $trace['object'];
            $callee = $backtrace[$index - 1];
            break;
        }
    }
    $parameters = [];
    if ($template !== NULL && $callee !== NULL) {
        $line_number = $callee['line'];
        $debug_infos = $template->getDebugInfo();
        if (isset($debug_infos[$line_number])) {
            $source_line = $debug_infos[$line_number];
            $source_file_name = $template->getTemplateName();
            if (is_readable($source_file_name)) {
                $source = file($source_file_name, FILE_IGNORE_NEW_LINES);
                $line = $source[$source_line - 1];
                preg_match('/\\((.+)\\)/', $line, $matches);
                if (isset($matches[1])) {
                    $parameters = array_map('trim', explode(',', $matches[1]));
                }
            }
        }
    }
    return $parameters;
}