Skip to content

[TwigBridge] lint all templates from configured Twig paths if no argument was provided #33446

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 1 commit into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
24 changes: 18 additions & 6 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Twig\Environment;
use Twig\Error\Error;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Source;

/**
Expand Down Expand Up @@ -78,16 +79,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filenames = $input->getArgument('filename');

if (0 === \count($filenames)) {
if (0 !== ftell(STDIN)) {
throw new RuntimeException('Please provide a filename or pipe template content to 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))]);
}

$template = '';
while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
$loader = $this->twig->getLoader();
if ($loader instanceof FilesystemLoader) {
$paths = [];
foreach ($loader->getNamespaces() as $namespace) {
$paths[] = $loader->getPaths($namespace);
}
$filenames = array_merge(...$paths);
}

return $this->display($input, $output, $io, [$this->validate($template, uniqid('sf_', true))]);
if (0 === \count($filenames)) {
throw new RuntimeException('Please provide a filename or pipe template content to STDIN.');
}
}

$filesInfo = $this->getFilesInfo($filenames);
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ public function testLintFileCompileTimeException()
$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);
Expand Down