Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore;

/**
* @author Adrien Brault <adrien.brault@gmail.com>
* @author Daniel Espendiller <daniel@espendiller.net>
Expand Down Expand Up @@ -150,21 +152,39 @@ public static String[] getControllerMethodShortcut(@NotNull Method method) {
templateFolderName = templateFolderName.replace("\\", "/");

String shortcutName;
String shortcutNameForOldNotation;

// Foobar without (.html.twig)
String templateName = className.substring(0, className.lastIndexOf("Controller"));

if(methodName.equals("__invoke")) {
// AppBundle::Foobar.html.twig
// AppBundle::foo_bar.html.twig
shortcutName = String.format(
"%s::%s%s",
symfonyBundle.getName(),
underscore(templateFolderName),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, we basically stop supporting the old < 4.0 syntax

Yes its just what i was think of, but i still see lot of projects having older versions. i think they can be easily supported as the method already supports multiple template names. I guess just adding the old naming to the end of the array will avoid this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever! Gonna try that out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it, tested it and it works. Tests are also green. Not sure if I need to add more test cases to also guard the old ones? How to do that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice, but not needed for merging ;)

underscore(templateName)
);

// AppBundle::FooBar.html.twig
shortcutNameForOldNotation = String.format(
"%s::%s%s",
symfonyBundle.getName(),
templateFolderName,
templateName
);
} else {
// FooBundle:Foobar:foobar.html.twig
// FooBundle:foo_bar:foo_bar.html.twig
shortcutName = String.format(
"%s:%s%s:%s",
symfonyBundle.getName(),
underscore(templateFolderName),
underscore(templateName),
underscore(StringUtils.removeEnd(methodName, "Action"))
);

// FooBundle:FooBar:fooBar.html.twig
shortcutNameForOldNotation = String.format(
"%s:%s%s:%s",
symfonyBundle.getName(),
templateFolderName,
Expand All @@ -179,6 +199,9 @@ public static String[] getControllerMethodShortcut(@NotNull Method method) {
shortcutName + ".html.twig",
shortcutName + ".json.twig",
shortcutName + ".xml.twig",
shortcutNameForOldNotation + ".html.twig",
shortcutNameForOldNotation + ".json.twig",
shortcutNameForOldNotation + ".xml.twig",
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ public void testGetControllerMethodShortcut() {

List<String> strings = Arrays.asList(TwigUtil.getControllerMethodShortcut((Method) psiElement.getParent()));

assertContainsElements(strings, "FooBundle:Foobar:" + string[1] + ".html.twig");
assertContainsElements(strings, "FooBundle:Foobar:" + string[1] + ".json.twig");
assertContainsElements(strings, "FooBundle:Foobar:" + string[1] + ".xml.twig");
assertContainsElements(strings, "FooBundle:foobar:" + string[1] + ".html.twig");
assertContainsElements(strings, "FooBundle:foobar:" + string[1] + ".json.twig");
assertContainsElements(strings, "FooBundle:foobar:" + string[1] + ".xml.twig");
}
}

Expand All @@ -444,9 +444,30 @@ public void testGetControllerMethodShortcutForInvoke() {

List<String> strings = Arrays.asList(TwigUtil.getControllerMethodShortcut((Method) psiElement.getParent()));

assertContainsElements(strings, "FooBundle::Foobar.html.twig");
assertContainsElements(strings, "FooBundle::Foobar.json.twig");
assertContainsElements(strings, "FooBundle::Foobar.xml.twig");
assertContainsElements(strings, "FooBundle::foobar.html.twig");
assertContainsElements(strings, "FooBundle::foobar.json.twig");
assertContainsElements(strings, "FooBundle::foobar.xml.twig");
}

public void testGetControllerMethodShortcutForInvokeWithSnakeCase() {
myFixture.copyFileToProject("controller_method.php");

myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
"namespace FooBundle\\Controller;\n" +
"class FooBarController\n" +
"{\n" +
" public function __in<caret>voke() {}\n" +
"" +
"}\n"
);

PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

List<String> strings = Arrays.asList(TwigUtil.getControllerMethodShortcut((Method) psiElement.getParent()));

assertContainsElements(strings, "FooBundle::foo_bar.html.twig");
assertContainsElements(strings, "FooBundle::foo_bar.json.twig");
assertContainsElements(strings, "FooBundle::foo_bar.xml.twig");
}

public void testFindTwigFileController() {
Expand Down