|
1 | 1 | package fr.adrienbrault.idea.symfony2plugin.templating.util; |
2 | 2 |
|
3 | 3 | import com.intellij.psi.PsiElement; |
4 | | -import com.intellij.psi.PsiFile; |
5 | 4 | import com.intellij.psi.PsiRecursiveElementWalkingVisitor; |
6 | 5 | import com.intellij.psi.util.PsiTreeUtil; |
7 | | -import com.intellij.util.containers.ArrayListSet; |
8 | 6 | import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag; |
9 | 7 | import com.jetbrains.php.lang.parser.PhpElementTypes; |
10 | 8 | import com.jetbrains.php.lang.psi.elements.*; |
11 | 9 | import de.espend.idea.php.annotation.util.AnnotationUtil; |
12 | 10 | import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
13 | | -import fr.adrienbrault.idea.symfony2plugin.config.SymfonyPhpReferenceContributor; |
14 | 11 | import fr.adrienbrault.idea.symfony2plugin.extension.PluginConfigurationExtension; |
15 | 12 | import fr.adrienbrault.idea.symfony2plugin.extension.PluginConfigurationExtensionParameter; |
16 | 13 | import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable; |
17 | 14 | import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil; |
18 | 15 | import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
19 | 16 | import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
20 | | -import kotlin.Pair; |
21 | 17 | import kotlin.Triple; |
22 | 18 | import org.apache.commons.lang.StringUtils; |
23 | 19 | import org.jetbrains.annotations.NotNull; |
24 | 20 | import org.jetbrains.annotations.Nullable; |
25 | 21 |
|
26 | 22 | import java.util.*; |
27 | 23 | 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; |
28 | 28 |
|
29 | 29 | /** |
30 | 30 | * @author Daniel Espendiller <daniel@espendiller.net> |
@@ -380,12 +380,35 @@ private void visitPhpDocTag(@NotNull PhpDocTag phpDocTag) { |
380 | 380 | } |
381 | 381 |
|
382 | 382 | 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, "\\"))) { |
384 | 384 | return; |
385 | 385 | } |
386 | 386 |
|
387 | 387 | 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")) { |
389 | 412 | Method methodScope = AnnotationBackportUtil.getMethodScope(phpDocTag); |
390 | 413 | if(methodScope != null) { |
391 | 414 | addTemplateWithScope(template, methodScope, null); |
|
0 commit comments