You are here

protected function LintCommand::execute 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::execute()

File

src/Command/LintCommand.php, line 80

Class

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

Namespace

Drupal\twig_tweak\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) {
  $io = new SymfonyStyle($input, $output);
  $filenames = $input
    ->getArgument('filename');
  $showDeprecations = $input
    ->getOption('show-deprecations');
  if ([
    '-',
  ] === $filenames) {
    return $this
      ->display($input, $output, $io, [
      $this
        ->validate(file_get_contents('php://stdin'), uniqid('sf_', true)),
    ]);
  }
  if (!$filenames) {
    $loader = $this->twig
      ->getLoader();
    if ($loader instanceof FilesystemLoader) {
      $paths = [];
      foreach ($loader
        ->getNamespaces() as $namespace) {
        $paths[] = $loader
          ->getPaths($namespace);
      }
      $filenames = array_merge(...$paths);
    }
    if (!$filenames) {
      throw new RuntimeException('Please provide a filename or pipe template content to STDIN.');
    }
  }
  if ($showDeprecations) {
    $prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
      if (\E_USER_DEPRECATED === $level) {
        $templateLine = 0;
        if (preg_match('/ at line (\\d+)[ .]/', $message, $matches)) {
          $templateLine = $matches[1];
        }
        throw new Error($message, $templateLine);
      }
      return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
    });
  }
  try {
    $filesInfo = $this
      ->getFilesInfo($filenames);
  } finally {
    if ($showDeprecations) {
      restore_error_handler();
    }
  }
  return $this
    ->display($input, $output, $io, $filesInfo);
}