feature #33446 [TwigBridge] lint all templates from configured Twig paths if no argument was provided (yceruto)
This PR was merged into the 4.4 branch.
Discussion
----------
[TwigBridge] lint all templates from configured Twig paths if no argument was provided
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | https://github.com/symfony/symfony/issues/33443
| License | MIT
| Doc PR | -
All details in PR title and related issue https://github.com/symfony/symfony/issues/33443.
Commits
-------
baddf1d9de
lint all templates from configured Twig paths if no argument was provided
This commit is contained in:
commit
1bfefefcf5
@ -7,6 +7,7 @@ CHANGELOG
|
|||||||
* marked all classes extending twig as `@final`
|
* marked all classes extending twig as `@final`
|
||||||
* deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
|
* deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
|
||||||
`DebugCommand::__construct()` method, swap the variables position.
|
`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
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
@ -23,6 +23,7 @@ use Symfony\Component\Finder\Finder;
|
|||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
use Twig\Error\Error;
|
use Twig\Error\Error;
|
||||||
use Twig\Loader\ArrayLoader;
|
use Twig\Loader\ArrayLoader;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
use Twig\Source;
|
use Twig\Source;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,16 +79,27 @@ EOF
|
|||||||
$filenames = $input->getArgument('filename');
|
$filenames = $input->getArgument('filename');
|
||||||
|
|
||||||
if (0 === \count($filenames)) {
|
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.');
|
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);
|
$filesInfo = $this->getFilesInfo($filenames);
|
||||||
|
@ -66,9 +66,18 @@ class LintCommandTest extends TestCase
|
|||||||
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
|
$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
|
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 = new Application();
|
||||||
$application->add($command);
|
$application->add($command);
|
||||||
|
Reference in New Issue
Block a user