protected function execute(InputInterface $input, CliOutputInterface $output)
{
$twig = new StubbedEnvironment(new \Twig_Loader_String());
$template = null;
$filename = $input->getArgument('filename');
$exclude = $input->getOption('exclude');
$summary = $input->getOption('summary');
$output = $this->getOutput($output, $input->getOption('format'));
if (!$filename) {
if (0 !== ftell(STDIN)) {
throw new \RuntimeException("Please provide a filename or pipe template content to stdin.");
}
while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
}
return $this->validateTemplate($twig, $output, $template);
}
if (!is_readable($filename)) {
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
}
$files = array();
if (is_file($filename)) {
$files = array($filename);
} elseif (is_dir($filename)) {
$files = Finder::create()->files()->in($filename)->name('*.twig')->filter(function (\SplFileInfo $file) use($exclude) {
foreach ($exclude as $excludeItem) {
if (1 === preg_match('#' . $excludeItem . '#', $file->getRealPath())) {
return false;
}
}
return true;
});
}
$onlyPrintErrors = $input->getOption('only-print-errors');
$errors = 0;
$linted = 0;
foreach ($files as $file) {
$linted++;
$errors += $this->validateTemplate($twig, $output, file_get_contents($file), $file, $onlyPrintErrors);
}
$stats = array('total' => count($files), 'linted' => $linted, 'errors' => $errors);
if ($summary) {
$output->summary($stats);
}
return $errors > 0 ? 1 : 0;
}