8000 Adds a linter command for templates by marcw · Pull Request #3804 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Adds a linter command for templates #3804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adds more features to twig:lint command
  • Loading branch information
marc.weistroff committed Apr 8, 2012
commit 8726ade31036904b5c6f8778b816ec622882b73b
56 changes: 38 additions & 18 deletions src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;

/**
* Command that will validate your template syntax and output encountered errors.
Expand All @@ -36,6 +37,16 @@ protected function configure()

The command will get the contents of "filename" and will validates its syntax.

<info>php %command.full_name% dirname</info>

The command will find all twig templates in dirname and will validate the syntax
of each Twig template.

<info>php %command.full_name% @AcmeMyBundle</info>

The command will find all twig templates in bundle AcmeMyBundle and will validate
the syntax of each one.

<info>cat filename | php %command.full_name%</info>

The command will get the template contents from stdin and will validates its syntax.
Expand All @@ -54,34 +65,43 @@ protected function execute(InputInterface $input, OutputInterface $output)
$template = null;
$filename = $input->getArgument('filename');

if ($filename && !is_readable($filename)) {
$output->writeln(sprintf('<error>File %s is not readable</error>', $filename));

return 2;
}

if ($filename) {
$template = file_get_contents($filename);
} else {
if (!$filename) {
if (0 !== ftell(STDIN)) {
$output->writeln(sprintf('<error>Please provide a filename or pipe template content to stdin.</error>'));

return 2;
throw new \RuntimeException("Please provide a filename or pipe template content to stdin.");
}

while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
}

return $twig->parse($twig->tokenize($template));
}

if (0 !== strpos($filename, '@') && !is_readable($filename)) {
throw new \RuntimeException("File or directory '%s' is not readable");
}

try {
$twig->parse($twig->tokenize($template));
} catch(\Twig_Error_Syntax $e) {
$output->writeln($e->getMessage());
$files = array();
if (is_file($filename)) {
$files = array($filename);
} elseif (is_dir($filename)) {
$files = Finder::create()->files()->in($filename)->name('*.twig');
} else {
$dir = $this->getApplication()->getKernel()->locateResource($filename);
$files = Finder::create()->files()->in($dir)->name('*.twig');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
    if (!is_dir($filename)) {
        $filename = $this->getApplication()->getKernel()->locateResource($filename);
    }

    $files = Finder::create()->files()->in($filename)->name('*.twig');
}


return 1;
foreach ($files as $file) {
try {
$twig->parse($twig->tokenize(file_get_contents($file)));
} catch (\Exception $e) {
$output->writeln(sprintf('<error>Syntax error in %s</error>', $file));

throw $e;
}
}

$output->writeln("<info>Template's syntax is valid.</info>");
$output->writeln('<info>No syntax error detected.</info>');
}
}

0