|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.ux.variable.collector; |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.jetbrains.php.PhpIndex; |
| 5 | +import com.jetbrains.php.lang.psi.elements.Field; |
| 6 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 7 | +import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigFileVariableCollector; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigFileVariableCollectorParameter; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable; |
| 10 | +import fr.adrienbrault.idea.symfony2plugin.util.UxUtil; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | + |
| 13 | +import java.util.Collection; |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * Support variables piped from "AsTwigComponent, AsLiveComponent" to Twig templates |
| 19 | + * |
| 20 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 21 | + */ |
| 22 | +public class UxComponentVariableCollector implements TwigFileVariableCollector { |
| 23 | + @Override |
| 24 | + public void collectPsiVariables(@NotNull TwigFileVariableCollectorParameter parameter, @NotNull Map<String, PsiVariable> variables) { |
| 25 | + Project project = parameter.getProject(); |
| 26 | + Collection<PhpClass> componentClassesForTemplateFile = UxUtil.getComponentClassesForTemplateFile(project, parameter.getElement().getContainingFile()); |
| 27 | + |
| 28 | + if (!componentClassesForTemplateFile.isEmpty()) { |
| 29 | + variables.put("attributes", new PsiVariable("\\Symfony\\UX\\TwigComponent\\ComponentAttributes")); |
| 30 | + } |
| 31 | + |
| 32 | + for (PhpClass phpClass : componentClassesForTemplateFile) { |
| 33 | + variables.put("this", new PsiVariable(phpClass.getFQN())); |
| 34 | + |
| 35 | + for (Field field : phpClass.getFields()) { |
| 36 | + if (field.getModifier().isPublic()) { |
| 37 | + variables.put(field.getName(), new PsiVariable( |
| 38 | + PhpIndex.getInstance(project).completeType(project, field.getType(), new HashSet<>()).getTypes() |
| 39 | + )); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments