lint all templates from configured Twig paths if no argument was provided

This commit is contained in:
Yonel Ceruto 2019-09-03 10:27:31 -04:00
parent 9690562410
commit baddf1d9de
3 changed files with 31 additions and 9 deletions

View File

@ -7,6 +7,7 @@ CHANGELOG
* marked all classes extending twig as `@final`
* deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
`DebugCommand::__construct()` method, swap the variables position.
* the `LintCommand` lints all the templates stored in all configured Twig paths if none argument is provided
4.3.0
-----

View File

@ -23,6 +23,7 @@ use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Source;
/**
@ -78,16 +79,27 @@ EOF
$filenames = $input->getArgument('filename');
if (0 === \count($filenames)) {
if (0 !== ftell(STDIN)) {
if (0 === ftell(STDIN)) {
$template = '';
while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
}
return $this->display($input, $output, $io, [$this->validate($template, uniqid('sf_', true))]);
}
$loader = $this->twig->getLoader();
if ($loader instanceof FilesystemLoader) {
$paths = [];
foreach ($loader->getNamespaces() as $namespace) {
$paths[] = $loader->getPaths($namespace);
}
$filenames = array_merge(...$paths);
}
if (0 === \count($filenames)) {
throw new RuntimeException('Please provide a filename or pipe template content to STDIN.');
}
$template = '';
while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
}
return $this->display($input, $output, $io, [$this->validate($template, uniqid('sf_', true))]);
}
$filesInfo = $this->getFilesInfo($filenames);

View File

@ -66,9 +66,18 @@ class LintCommandTest extends TestCase
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
}
public function testLintDefaultPaths()
{
$tester = $this->createCommandTester();
$ret = $tester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
self::assertStringContainsString('OK in', trim($tester->getDisplay()));
}
private function createCommandTester(): CommandTester
{
$command = new LintCommand(new Environment(new FilesystemLoader()));
$command = new LintCommand(new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/')));
$application = new Application();
$application->add($command);