Skip to content

Commit aa95d00

Browse files
committed
provide template guesser for @template with default property is empty
1 parent 28dc9e9 commit aa95d00

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/util/PhpMethodVariableResolveUtil.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
package fr.adrienbrault.idea.symfony2plugin.templating.util;
22

33
import com.intellij.psi.PsiElement;
4-
import com.intellij.psi.PsiFile;
54
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
65
import com.intellij.psi.util.PsiTreeUtil;
7-
import com.intellij.util.containers.ArrayListSet;
86
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
97
import com.jetbrains.php.lang.parser.PhpElementTypes;
108
import com.jetbrains.php.lang.psi.elements.*;
119
import de.espend.idea.php.annotation.util.AnnotationUtil;
1210
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
13-
import fr.adrienbrault.idea.symfony2plugin.config.SymfonyPhpReferenceContributor;
1411
import fr.adrienbrault.idea.symfony2plugin.extension.PluginConfigurationExtension;
1512
import fr.adrienbrault.idea.symfony2plugin.extension.PluginConfigurationExtensionParameter;
1613
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable;
1714
import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil;
1815
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1916
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
20-
import kotlin.Pair;
2117
import kotlin.Triple;
2218
import org.apache.commons.lang.StringUtils;
2319
import org.jetbrains.annotations.NotNull;
2420
import org.jetbrains.annotations.Nullable;
2521

2622
import java.util.*;
2723
import java.util.function.Consumer;
24+
import java.util.regex.Matcher;
25+
import java.util.regex.Pattern;
26+
27+
import static fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore;
2828

2929
/**
3030
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -380,12 +380,35 @@ private void visitPhpDocTag(@NotNull PhpDocTag phpDocTag) {
380380
}
381381

382382
String annotationFqnName = AnnotationBackportUtil.getClassNameReference(phpDocTag, fileImports);
383-
if(!"Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template".equals(StringUtils.stripStart(annotationFqnName, "\\"))) {
383+
if(!StringUtils.stripStart(TwigUtil.TEMPLATE_ANNOTATION_CLASS, "\\").equals(StringUtils.stripStart(annotationFqnName, "\\"))) {
384384
return;
385385
}
386386

387387
String template = AnnotationUtil.getPropertyValueOrDefault(phpDocTag, "template");
388-
if(template != null && template.endsWith(".twig")) {
388+
if (template == null) {
389+
// see \Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser
390+
// App\Controller\MyNiceController::myAction => my_nice/my.html.twig
391+
Method methodScope = AnnotationBackportUtil.getMethodScope(phpDocTag);
392+
if(methodScope != null) {
393+
PhpClass phpClass = methodScope.getContainingClass();
394+
if (phpClass != null) {
395+
// App\Controller\ "MyNice" Controller
396+
Matcher matcher = Pattern.compile("Controller\\\\(.+)Controller$", Pattern.MULTILINE).matcher(StringUtils.stripStart(phpClass.getFQN(), "\\"));
397+
if(matcher.find()){
398+
String group = underscore(matcher.group(1).replace("\\", "/"));
399+
String name = methodScope.getName();
400+
401+
// __invoke is using controller as template name
402+
if (name.equals("__invoke")) {
403+
addTemplateWithScope(group + ".html.twig", methodScope, null);
404+
} else {
405+
String action = name.endsWith("Action") ? name.substring(0, name.length() - "Action".length()) : name;
406+
addTemplateWithScope(group + "/" + underscore(action) + ".html.twig", methodScope, null);
407+
}
408+
}
409+
}
410+
}
411+
} else if(template.endsWith(".twig")) {
389412
Method methodScope = AnnotationBackportUtil.getMethodScope(phpDocTag);
390413
if(methodScope != null) {
391414
addTemplateWithScope(template, methodScope, null);

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/PhpTwigTemplateUsageStubIndexTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,67 @@ public void testThatTemplatePropertyAnnotationIsIndexed() {
104104
"foo-annotation-property.html.twig".equals(value.getTemplate()) && value.getScopes().contains("Foobar.foobar")
105105
);
106106
}
107+
108+
public void testEmptyTemplateAnnotationIndexUsingTemplateGuesser() {
109+
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
110+
"namespace App\\Controller;" +
111+
"" +
112+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;" +
113+
"class MyNiceFoobarController\n" +
114+
"{" +
115+
"/**\n" +
116+
" *\n" +
117+
" * @Template()\n" +
118+
" */" +
119+
"public function foobarWhatAction() {}" +
120+
"}\n"
121+
);
122+
123+
assertIndexContains(
124+
PhpTwigTemplateUsageStubIndex.KEY,
125+
"my_nice_foobar/foobar_what.html.twig"
126+
);
127+
}
128+
129+
public void testEmptyTemplateAnnotationIndexUsingInvokeTemplateGuesser() {
130+
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
131+
"namespace App\\Controller;" +
132+
"" +
133+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;" +
134+
"class MyNiceFoobarController\n" +
135+
"{" +
136+
"/**\n" +
137+
" *\n" +
138+
" * @Template()\n" +
139+
" */" +
140+
"public function __invoke() {}" +
141+
"}\n"
142+
);
143+
144+
assertIndexContains(
145+
PhpTwigTemplateUsageStubIndex.KEY,
146+
"my_nice_foobar.html.twig"
147+
);
148+
}
149+
150+
public void testEmptyTemplateAnnotationIndexWithDirectoryUseTemplateGuesser() {
151+
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
152+
"namespace App\\Controller\\CarItem\\WithApple;" +
153+
"" +
154+
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;" +
155+
"class MyNiceFoobarController\n" +
156+
"{" +
157+
"/**\n" +
158+
" *\n" +
159+
" * @Template()\n" +
160+
" */" +
161+
"public function foobarWhatAction() {}" +
162+
"}\n"
163+
);
164+
165+
assertIndexContains(
166+
PhpTwigTemplateUsageStubIndex.KEY,
167+
"car_item/with_apple/my_nice_foobar/foobar_what.html.twig"
168+
);
169+
}
107170
}

0 commit comments

Comments
 (0)