You are here

protected function KintExtension::getTwigFunctionParameters in Devel 8

Same name and namespace in other branches
  1. 8.2 kint/src/Twig/KintExtension.php \Drupal\kint\Twig\KintExtension::getTwigFunctionParameters()

Gets the twig function parameters for the current invocation.

Return value

array The detected twig function parameters.

1 call to KintExtension::getTwigFunctionParameters()
KintExtension::kint in kint/src/Twig/KintExtension.php
Provides Kint function to Twig templates.

File

kint/src/Twig/KintExtension.php, line 114

Class

KintExtension
Provides the Kint debugging function within Twig templates.

Namespace

Drupal\kint\Twig

Code

protected function getTwigFunctionParameters() {
  $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('/kint\\((.+)\\)/', $line, $matches);
        if (isset($matches[1])) {
          $parameters = array_map('trim', explode(',', $matches[1]));
        }
      }
    }
  }
  return $parameters;
}