feature #13548 [TwigBridge] Added support for passing more files to twig:lint command (sustmi)

This PR was merged into the 2.7 branch.

Discussion
----------

[TwigBridge] Added support for passing more files to twig:lint command

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | none

Commits
-------

6e197a0 [TwigBridge] Added support for passing more files to twig:lint command
This commit is contained in:
Fabien Potencier 2015-02-04 15:25:22 +01:00
commit db5dee66ee
2 changed files with 21 additions and 11 deletions

View File

@ -16,6 +16,7 @@ if (!defined('JSON_PRETTY_PRINT')) {
}
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@ -62,7 +63,7 @@ class LintCommand extends Command
$this
->setDescription('Lints a template and outputs encountered errors')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
->addArgument('filename')
->addArgument('filename', InputArgument::IS_ARRAY)
->setHelp(<<<EOF
The <info>%command.name%</info> command lints a template and outputs to STDOUT
the first encountered syntax error.
@ -94,9 +95,9 @@ EOF
return 1;
}
$filename = $input->getArgument('filename');
$filenames = $input->getArgument('filename');
if (!$filename) {
if (0 === count($filenames)) {
if (0 !== ftell(STDIN)) {
throw new \RuntimeException("Please provide a filename or pipe template content to STDIN.");
}
@ -109,14 +110,23 @@ EOF
return $this->display($input, $output, array($this->validate($twig, $template, uniqid('sf_'))));
}
$filesInfo = array();
foreach ($this->findFiles($filename) as $file) {
$filesInfo[] = $this->validate($twig, file_get_contents($file), $file);
}
$filesInfo = $this->getFilesInfo($twig, $filenames);
return $this->display($input, $output, $filesInfo);
}
private function getFilesInfo(\Twig_Environment $twig, array $filenames)
{
$filesInfo = array();
foreach ($filenames as $filename) {
foreach ($this->findFiles($filename) as $file) {
$filesInfo[] = $this->validate($twig, file_get_contents($file), $file);
}
}
return $filesInfo;
}
protected function findFiles($filename)
{
if (is_file($filename)) {

View File

@ -28,7 +28,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase
$tester = $this->createCommandTester();
$filename = $this->createFile('{{ foo }}');
$ret = $tester->execute(array('filename' => $filename), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE));
$ret = $tester->execute(array('filename' => array($filename)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE));
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertRegExp('/^OK in /', $tester->getDisplay());
@ -39,7 +39,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase
$tester = $this->createCommandTester();
$filename = $this->createFile('{{ foo');
$ret = $tester->execute(array('filename' => $filename));
$ret = $tester->execute(array('filename' => array($filename)));
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
$this->assertRegExp('/^KO in /', $tester->getDisplay());
@ -54,7 +54,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase
$filename = $this->createFile('');
unlink($filename);
$ret = $tester->execute(array('filename' => $filename));
$ret = $tester->execute(array('filename' => array($filename)));
}
public function testLintFileCompileTimeException()
@ -62,7 +62,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase
$tester = $this->createCommandTester();
$filename = $this->createFile("{{ 2|number_format(2, decimal_point='.', ',') }}");
$ret = $tester->execute(array('filename' => $filename));
$ret = $tester->execute(array('filename' => array($filename)));
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
$this->assertRegExp('/^KO in /', $tester->getDisplay());