You are here

protected function Debug::guessTwigFunctionParameters in Devel 4.x

Same name and namespace in other branches
  1. 8.3 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
Debug::message in src/Twig/Extension/Debug.php
Provides debug function to Twig templates.

File

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

Class

Debug
Provides the Devel debugging function within Twig templates.

Namespace

Drupal\devel\Twig\Extension

Code

protected function guessTwigFunctionParameters() {
  $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 \Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
      $template = $trace['object'];
      $callee = $backtrace[$index - 1];
      break;
    }
  }
  $parameters = [];

  /** @var \Twig_Template $template */
  if (NULL !== $template && NULL !== $callee) {
    $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;
}