Skip to content

Commit

Permalink
Merge pull request #2351 from Haehnchen/feature/2261-template-attribute
Browse files Browse the repository at this point in the history
#2261 whitelist "Symfony\Bridge\Twig\Attribute\Template" for template usages to fix render detection
  • Loading branch information
Haehnchen authored Apr 17, 2024
2 parents 7a38bc3 + d295f25 commit a6099a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void visitElement(@NotNull PsiElement element) {

if (element instanceof PhpAttribute) {
String fqn = ((PhpAttribute) element).getFQN();
if (fqn != null && PhpElementsUtil.isInstanceOf(element.getProject(), fqn, TwigUtil.TEMPLATE_ANNOTATION_CLASS)) {
if (fqn != null && Arrays.stream(TwigUtil.TEMPLATE_ANNOTATION_CLASS).anyMatch(s -> PhpElementsUtil.isInstanceOf(element.getProject(), fqn, s))) {
annotate((PhpAttribute) element, holder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,19 @@ public boolean processPhpCallInstruction(PhpCallInstruction instruction) {

public static class TemplateRenderVisitor {
public static void processMethodAttributes(@NotNull Method method, Consumer<Triple<String, PhpNamedElement, FunctionReference>> consumer) {
Collection<@NotNull PhpAttribute> attributes = method.getAttributes(TwigUtil.TEMPLATE_ANNOTATION_CLASS);
for (PhpAttribute attribute : attributes) {
if (attribute.getArguments().isEmpty()) {
// #[@Template()]
visitMethodForGuessing(method, consumer);
} else {
// [@Template("foobar.html.twig")]
// #[@Template(template: "foobar.html.twig")]
String template = PhpPsiAttributesUtil.getAttributeValueByNameAsStringWithDefaultParameterFallback(attribute, "template");
if (StringUtils.isNotBlank(template)) {
addTemplateWithScope(template, method, null, consumer);
for (String templateAnnotationClass : TwigUtil.TEMPLATE_ANNOTATION_CLASS) {
Collection<@NotNull PhpAttribute> attributes = method.getAttributes(templateAnnotationClass);
for (PhpAttribute attribute : attributes) {
if (attribute.getArguments().isEmpty()) {
// #[@Template()]
visitMethodForGuessing(method, consumer);
} else {
// [@Template("foobar.html.twig")]
// #[@Template(template: "foobar.html.twig")]
String template = PhpPsiAttributesUtil.getAttributeValueByNameAsStringWithDefaultParameterFallback(attribute, "template");
if (StringUtils.isNotBlank(template)) {
addTemplateWithScope(template, method, null, consumer);
}
}
}
}
Expand Down Expand Up @@ -414,7 +416,7 @@ public static void processDocTag(@NotNull PhpDocTag phpDocTag, Consumer<Triple<S
}

String annotationFqnName = AnnotationBackportUtil.getClassNameReference(phpDocTag, fileImports);
if(!StringUtils.stripStart(TwigUtil.TEMPLATE_ANNOTATION_CLASS, "\\").equals(StringUtils.stripStart(annotationFqnName, "\\"))) {
if (Arrays.stream(TwigUtil.TEMPLATE_ANNOTATION_CLASS).noneMatch(s -> s.equals(annotationFqnName))) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public enum NamespaceType {

public static final String[] IMG_FILES_EXTENSIONS = new String[] { "png", "jpg", "jpeg", "gif", "svg"};

public static final String TEMPLATE_ANNOTATION_CLASS = "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template";
public static final String[] TEMPLATE_ANNOTATION_CLASS = new String[] {
"\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template",
"\\Symfony\\Bridge\\Twig\\Attribute\\Template",
};

public static class TwigPathNamespaceComparator implements Comparator<TwigPath> {
@Override
Expand Down

0 comments on commit a6099a3

Please sign in to comment.