From baddf1d9de6189180bbce95267b95e7c3bbb0277 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 3 Sep 2019 10:27:31 -0400 Subject: [PATCH] lint all templates from configured Twig paths if no argument was provided --- src/Symfony/Bridge/Twig/CHANGELOG.md | 1 + .../Bridge/Twig/Command/LintCommand.php | 28 +++++++++++++------ .../Twig/Tests/Command/LintCommandTest.php | 11 +++++++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index 6e4ab6448e..1734c8f86f 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 536ca7bdbb..30619eef57 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -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); diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index 22f048e6bc..fe3a7231b4 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -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);