Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d366e51

Browse filesBrowse files
committed
Handle Twig namespaces
1 parent 396dc78 commit d366e51
Copy full SHA for d366e51

File tree

Expand file treeCollapse file tree

8 files changed

+43
-7
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+43
-7
lines changed

‎extension.neon

Copy file name to clipboardExpand all lines: extension.neon
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ parametersSchema:
116116
constantHassers: bool()
117117
console_application_loader: schema(string(), nullable())
118118
consoleApplicationLoader: schema(string(), nullable())
119-
twigTemplateDirectories: listOf(string())
119+
twigTemplateDirectories: arrayOf(schema(string(), nullable()))
120120
])
121121

122122
services:

‎src/Rules/Symfony/TwigTemplateExistsRule.php

Copy file name to clipboardExpand all lines: src/Rules/Symfony/TwigTemplateExistsRule.php
+15-3Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function file_exists;
1717
use function in_array;
1818
use function is_string;
19+
use function preg_match;
1920
use function sprintf;
2021

2122
/**
@@ -24,10 +25,10 @@
2425
final class TwigTemplateExistsRule implements Rule
2526
{
2627

27-
/** @var list<string> */
28+
/** @var array<string, string|null> */
2829
private $twigTemplateDirectories;
2930

30-
/** @param list<string> $twigTemplateDirectories */
31+
/** @param array<string, string|null> $twigTemplateDirectories */
3132
public function __construct(array $twigTemplateDirectories)
3233
{
3334
$this->twigTemplateDirectories = $twigTemplateDirectories;
@@ -108,7 +109,18 @@ private function getTwigTemplateArg(MethodCall $node, Scope $scope): ?Arg
108109

109110
private function twigTemplateExists(string $templateName): bool
110111
{
111-
foreach ($this->twigTemplateDirectories as $twigTemplateDirectory) {
112+
if (preg_match('#^@(.+)\/(.+)$#', $templateName, $matches) === 1) {
113+
$templateNamespace = $matches[1];
114+
$templateName = $matches[2];
115+
} else {
116+
$templateNamespace = null;
117+
}
118+
119+
foreach ($this->twigTemplateDirectories as $twigTemplateDirectory => $namespace) {
120+
if ($namespace !== $templateNamespace) {
121+
continue;
122+
}
123+
112124
$templatePath = $twigTemplateDirectory . '/' . $templateName;
113125

114126
if (file_exists($templatePath)) {

‎tests/Rules/Symfony/ExampleTwigController.php

Copy file name to clipboardExpand all lines: tests/Rules/Symfony/ExampleTwigController.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function foo(): void
6161
$this->render($name);
6262

6363
$this->render($this->getName());
64+
65+
$this->render('@admin/backend.html.twig');
66+
$this->render('@admin/foo.html.twig');
67+
$this->render('backend.html.twig');
6468
}
6569

6670
private function getName(): string

‎tests/Rules/Symfony/TwigTemplateExistsRuleMoreTemplatesTest.php

Copy file name to clipboardExpand all lines: tests/Rules/Symfony/TwigTemplateExistsRuleMoreTemplatesTest.php
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ final class TwigTemplateExistsRuleMoreTemplatesTest extends RuleTestCase
1414
protected function getRule(): Rule
1515
{
1616
return new TwigTemplateExistsRule([
17-
__DIR__ . '/data',
18-
__DIR__ . '/templates',
17+
__DIR__ . '/twig/templates' => null,
18+
__DIR__ . '/twig/admin' => 'admin',
19+
__DIR__ . '/twig/user' => null,
1920
]);
2021
}
2122

@@ -30,6 +31,14 @@ public function testGetArgument(): void
3031
'Twig template "baz.html.twig" does not exist.',
3132
61,
3233
],
34+
[
35+
'Twig template "@admin/foo.html.twig" does not exist.',
36+
66,
37+
],
38+
[
39+
'Twig template "backend.html.twig" does not exist.',
40+
67,
41+
],
3342
]
3443
);
3544
}

‎tests/Rules/Symfony/TwigTemplateExistsRuleTest.php

Copy file name to clipboardExpand all lines: tests/Rules/Symfony/TwigTemplateExistsRuleTest.php
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ final class TwigTemplateExistsRuleTest extends RuleTestCase
1313

1414
protected function getRule(): Rule
1515
{
16-
return new TwigTemplateExistsRule([__DIR__ . '/templates']);
16+
return new TwigTemplateExistsRule([
17+
__DIR__ . '/twig/templates' => null,
18+
__DIR__ . '/twig/admin' => 'admin',
19+
]);
1720
}
1821

1922
public function testGetArgument(): void
@@ -83,6 +86,14 @@ public function testGetArgument(): void
8386
'Twig template "baz.html.twig" does not exist.',
8487
61,
8588
],
89+
[
90+
'Twig template "@admin/foo.html.twig" does not exist.',
91+
66,
92+
],
93+
[
94+
'Twig template "backend.html.twig" does not exist.',
95+
67,
96+
],
8697
]
8798
);
8899
}

‎tests/Rules/Symfony/twig/user/bar.html.twig

Copy file name to clipboardExpand all lines: tests/Rules/Symfony/twig/user/bar.html.twig
Whitespace-only changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.