You are here

private function LintCommand::validate in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/Command/LintCommand.php \Drupal\twig_tweak\Command\LintCommand::validate()
2 calls to LintCommand::validate()
LintCommand::execute in src/Command/LintCommand.php
LintCommand::getFilesInfo in src/Command/LintCommand.php

File

src/Command/LintCommand.php, line 154

Class

LintCommand
Command that will validate your template syntax and output encountered errors.

Namespace

Drupal\twig_tweak\Command

Code

private function validate(string $template, string $file) : array {
  $realLoader = $this->twig
    ->getLoader();
  try {
    $temporaryLoader = new ArrayLoader([
      $file => $template,
    ]);
    $this->twig
      ->setLoader($temporaryLoader);
    $nodeTree = $this->twig
      ->parse($this->twig
      ->tokenize(new Source($template, $file)));
    $this->twig
      ->compile($nodeTree);
    $this->twig
      ->setLoader($realLoader);
  } catch (Error $e) {
    $this->twig
      ->setLoader($realLoader);
    return [
      'template' => $template,
      'file' => $file,
      'line' => $e
        ->getTemplateLine(),
      'valid' => false,
      'exception' => $e,
    ];
  }
  return [
    'template' => $template,
    'file' => $file,
    'valid' => true,
  ];
}