From 6e197a03dc8c61c807355f8bcc488543e2ac8922 Mon Sep 17 00:00:00 2001 From: Miroslav Sustek Date: Thu, 29 Jan 2015 01:15:47 +0100 Subject: [PATCH] [TwigBridge] Added support for passing more files to twig:lint command --- .../Bridge/Twig/Command/LintCommand.php | 24 +++++++++++++------ .../Twig/Tests/Command/LintCommandTest.php | 8 +++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 243804ccb5..449422b6d0 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -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(<<%command.name% 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)) { diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index 8d5bd99136..fea0b45bcc 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -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());