Skip to content

Commit 251d642

Browse files
committed
lint all templates from configured Twig paths if no argument was provided
1 parent 9690562 commit 251d642

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* marked all classes extending twig as `@final`
88
* deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
99
`DebugCommand::__construct()` method, swap the variables position.
10+
* the `LintCommand` lint all the templates stored in all configured Twig paths if none argument is provided
1011

1112
4.3.0
1213
-----

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Twig\Environment;
2424
use Twig\Error\Error;
2525
use Twig\Loader\ArrayLoader;
26+
use Twig\Loader\FilesystemLoader;
2627
use Twig\Source;
2728

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

8081
if (0 === \count($filenames)) {
81-
if (0 !== ftell(STDIN)) {
82-
throw new RuntimeException('Please provide a filename or pipe template content to STDIN.');
82+
if (0 === ftell(STDIN)) {
83+
$template = '';
84+
while (!feof(STDIN)) {
85+
$template .= fread(STDIN, 1024);
86+
}
87+
88+
return $this->display($input, $output, $io, [$this->validate($template, uniqid('sf_', true))]);
8389
}
8490

85-
$template = '';
86-
while (!feof(STDIN)) {
87-
$template .= fread(STDIN, 1024);
91+
$loader = $this->twig->getLoader();
92+
if ($loader instanceof FilesystemLoader) {
93+
$paths = [];
94+
foreach ($loader->getNamespaces() as $namespace) {
95+
$paths[] = $loader->getPaths($namespace);
96+
}
97+
$filenames = array_merge(...$paths);
8898
}
8999

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

93105
$filesInfo = $this->getFilesInfo($filenames);

src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,18 @@ public function testLintFileCompileTimeException()
6666
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
6767
}
6868

69+
public function testLintDefaultPaths()
70+
{
71+
$tester = $this->createCommandTester();
72+
$ret = $tester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);
73+
74+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
75+
self::assertStringContainsString('OK in', trim($tester->getDisplay()));
76+
}
77+
6978
private function createCommandTester(): CommandTester
7079
{
71-
$command = new LintCommand(new Environment(new FilesystemLoader()));
80+
$command = new LintCommand(new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/')));
7281

7382
$application = new Application();
7483
$application->add($command);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy