Skip to content

Commit

Permalink
provide cache for form extension resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Aug 5, 2023
1 parent e4dbeb9 commit 80c6c8d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ public String getName() {
}

@Nullable
public PhpClass getPhpClass() {
return phpClass;
}

@Nullable
public PhpClass getPhpClass(Project project) {

public PhpClass getPhpClass(@NotNull Project project) {
if(phpClass != null) {
return phpClass;
}
Expand All @@ -49,7 +43,7 @@ public PhpClass getPhpClass(Project project) {
return null;
}

return PhpElementsUtil.getClass(project, this.phpClassName);
return this.phpClass = PhpElementsUtil.getClass(project, this.phpClassName);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.xml.XmlDocumentImpl;
import com.intellij.psi.util.PsiElementFilter;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.*;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
Expand Down Expand Up @@ -48,6 +48,7 @@
* @author Daniel Espendiller <[email protected]>
*/
public class FormUtil {
private static final Key<CachedValue<Collection<String[]>>> FORM_EXTENSION_TYPES = new Key<>("SYMFONY_FORM_EXTENSION_TYPES");

final public static String ABSTRACT_FORM_INTERFACE = "\\Symfony\\Component\\Form\\FormTypeInterface";
final public static String FORM_EXTENSION_INTERFACE = "\\Symfony\\Component\\Form\\FormTypeExtensionInterface";
Expand Down Expand Up @@ -79,7 +80,7 @@ public static PhpClass getFormTypeToClass(Project project, @Nullable String form
return new FormTypeCollector(project).collect().getFormTypeToClass(formType);
}

public static Collection<LookupElement> getFormTypeLookupElements(Project project) {
public static Collection<LookupElement> getFormTypeLookupElements(@NotNull Project project) {

Collection<LookupElement> lookupElements = new ArrayList<>();

Expand All @@ -89,7 +90,7 @@ public static Collection<LookupElement> getFormTypeLookupElements(Project projec
String name = entry.getValue().getName();
String typeText = entry.getValue().getPhpClassName();

PhpClass phpClass = entry.getValue().getPhpClass();
PhpClass phpClass = entry.getValue().getPhpClass(project);
if(phpClass != null) {
typeText = phpClass.getName();
}
Expand Down Expand Up @@ -359,7 +360,7 @@ public static Set<String> getTags(XmlTag serviceTag) {
XmlAttribute attribute = serviceSubTag.getAttribute("name");
if(attribute != null) {
String tagName = attribute.getValue();
if(tagName != null && StringUtils.isNotBlank(tagName)) {
if(StringUtils.isNotBlank(tagName)) {
tags.add(tagName);
}
}
Expand All @@ -372,20 +373,42 @@ public static Set<String> getTags(XmlTag serviceTag) {

@NotNull
public static Map<String, FormTypeClass> getFormTypeClasses(@NotNull Project project) {
Collection<String[]> nameAndClass = CachedValuesManager.getManager(project).getCachedValue(
project,
FORM_EXTENSION_TYPES,
new CachedValueProvider<>() {
@Override
public @NotNull Result<Collection<String[]>> compute() {
Collection<String[]> items = new ArrayList<>();

Map<String, FormTypeClass> map = new HashMap<>();
for (PhpClass phpClass : PhpIndex.getInstance(project).getAllSubclasses(ABSTRACT_FORM_INTERFACE)) {
if (!isValidFormPhpClass(phpClass)) {
continue;
}

for(PhpClass phpClass: PhpIndex.getInstance(project).getAllSubclasses(ABSTRACT_FORM_INTERFACE)) {
if(!isValidFormPhpClass(phpClass)) {
continue;
}
String name = FormUtil.getFormNameOfPhpClass(phpClass);
if (name == null) {
continue;
}

String name = FormUtil.getFormNameOfPhpClass(phpClass);
if (name == null) {
continue;
}
items.add(new String[]{name, phpClass.getFQN()});

map.put(name, new FormTypeClass(name, phpClass, EnumFormTypeSource.INDEX));
}

return Result.create(items, PsiModificationTracker.MODIFICATION_COUNT);
}
},
false
);

Map<String, FormTypeClass> map = new HashMap<>();

for (String[] item : nameAndClass) {
PhpClass phpClass = PhpElementsUtil.getClassInterface(project, item[1]);
if (phpClass != null) {
String name = item[0];
map.put(name, new FormTypeClass(name, phpClass, EnumFormTypeSource.INDEX));
}
}

return map;
Expand Down Expand Up @@ -460,7 +483,7 @@ public PhpClass getFormTypeClass(String formTypeName) {
return null;
}

return forms.get(formTypeName).getPhpClass();
return forms.get(formTypeName).getPhpClass(project);
}

public Map<String, FormTypeClass> getFormTypesMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void testGetFormTypeClassOnParameter() {
public void testGetFormTypeClasses() {
Map<String, FormTypeClass> formTypeClasses = FormUtil.getFormTypeClasses(getProject());
assertNotNull(formTypeClasses.get("foo_type"));
assertEquals(formTypeClasses.get("foo_type").getPhpClass().getFQN(), "\\Form\\FormType\\Foo");
assertEquals(formTypeClasses.get("foo_type").getPhpClass(getProject()).getFQN(), "\\Form\\FormType\\Foo");

assertNotNull(formTypeClasses.get("foo_bar"));
assertEquals(formTypeClasses.get("foo_bar").getPhpClass().getFQN(), "\\Form\\FormType\\FooBar");
assertEquals(formTypeClasses.get("foo_bar").getPhpClass(getProject()).getFQN(), "\\Form\\FormType\\FooBar");
}

public void testGetFormAliases() {
Expand Down

0 comments on commit 80c6c8d

Please sign in to comment.