55import com .intellij .psi .xml .XmlFile ;
66import com .intellij .psi .xml .XmlTag ;
77import com .intellij .util .Consumer ;
8- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .AttributeValueInterface ;
9- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .DummyAttributeValue ;
10- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .XmlTagAttributeValue ;
11- import fr .adrienbrault .idea .symfony2plugin .dic .attribute .value .YamlKeyValueAttributeValue ;
128import fr .adrienbrault .idea .symfony2plugin .stubs .dict .FileResource ;
9+ import fr .adrienbrault .idea .symfony2plugin .stubs .dict .FileResourceContextTypeEnum ;
1310import fr .adrienbrault .idea .symfony2plugin .util .yaml .YamlHelper ;
1411import org .apache .commons .lang .StringUtils ;
1512import org .jetbrains .annotations .NotNull ;
16- import org .jetbrains .annotations .Nullable ;
1713import org .jetbrains .yaml .psi .YAMLFile ;
1814import org .jetbrains .yaml .psi .YAMLKeyValue ;
1915
16+ import java .util .HashMap ;
17+ import java .util .Map ;
18+ import java .util .TreeMap ;
19+
2020/**
2121 * @author Daniel Espendiller <daniel@espendiller.net>
2222 */
2323public class FileResourceVisitorUtil {
24-
2524 public static void visitFile (@ NotNull PsiFile psiFile , @ NotNull Consumer <FileResourceConsumer > consumer ) {
2625 if (psiFile instanceof XmlFile ) {
2726 visitXmlFile ((XmlFile ) psiFile , consumer );
@@ -46,7 +45,26 @@ private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<
4645 continue ;
4746 }
4847
49- consumer .consume (new FileResourceConsumer (resourceKey , yamlKeyValue , normalize (resource )));
48+ FileResourceContextTypeEnum fileResourceContextType = FileResourceContextTypeEnum .UNKNOWN ;
49+
50+ Map <String , String > map = new HashMap <>();
51+ for (String option : new String [] {"type" , "prefix" , "name_prefix" }) {
52+ String attributeValue = YamlHelper .getYamlKeyValueAsString (yamlKeyValue , option , true );
53+ if (StringUtils .isNotBlank (attributeValue ) && attributeValue .length () < 128 ) {
54+ map .put (option , attributeValue );
55+ }
56+ }
57+
58+ boolean isRouteContext = map .containsKey ("type" )
59+ || map .containsKey ("prefix" )
60+ || map .containsKey ("name_prefix" )
61+ || YamlHelper .getYamlKeyValue (yamlKeyValue , "requirements" , true ) != null ;
62+
63+ if (isRouteContext ) {
64+ fileResourceContextType = FileResourceContextTypeEnum .ROUTE ;
65+ }
66+
67+ consumer .consume (new FileResourceConsumer (resourceKey , normalize (resource ), fileResourceContextType , map ));
5068 }
5169 }
5270
@@ -65,7 +83,15 @@ private static void visitXmlFile(@NotNull XmlFile psiFile, @NotNull Consumer<Fil
6583 continue ;
6684 }
6785
68- consumer .consume (new FileResourceConsumer (xmlTag , xmlTag , normalize (resource )));
86+ Map <String , String > map = new HashMap <>();
87+ for (String option : new String [] {"type" , "prefix" , "name-prefix" }) {
88+ String attributeValue = xmlTag .getAttributeValue (option );
89+ if (StringUtils .isNotBlank (attributeValue ) && attributeValue .length () < 128 ) {
90+ map .put (option .replace ("-" , "_" ), attributeValue );
91+ }
92+ }
93+
94+ consumer .consume (new FileResourceConsumer (xmlTag , normalize (resource ), FileResourceContextTypeEnum .ROUTE , map ));
6995 }
7096 }
7197
@@ -75,39 +101,23 @@ public static String normalize(@NotNull String resource) {
75101 }
76102
77103 public static class FileResourceConsumer {
78-
79104 @ NotNull
80105 private final PsiElement psiElement ;
81106
82- @ Nullable
83- private AttributeValueInterface attributeValue = null ;
107+ @ NotNull
108+ private final String resource ;
84109
85110 @ NotNull
86- private final PsiElement scope ;
111+ private final FileResourceContextTypeEnum contextType ;
112+
87113 @ NotNull
88- private final String resource ;
114+ private final Map < String , String > contextValues ;
89115
90- public FileResourceConsumer (@ NotNull PsiElement target , @ NotNull PsiElement scope , @ NotNull String resource ) {
116+ public FileResourceConsumer (@ NotNull PsiElement target , @ NotNull String resource , @ NotNull FileResourceContextTypeEnum fileResourceContextTypeEnum , @ NotNull Map < String , String > contextValues ) {
91117 this .psiElement = target ;
92- this .scope = scope ;
93118 this .resource = resource ;
94- }
95-
96- @ NotNull
97- public AttributeValueInterface getAttributeValue () {
98- if (this .attributeValue != null ) {
99- return this .attributeValue ;
100- }
101-
102- // We use lazy instances
103- // @TODO: replace with factory pattern
104- if (this .psiElement instanceof YAMLKeyValue ) {
105- return this .attributeValue = new YamlKeyValueAttributeValue ((YAMLKeyValue ) this .scope );
106- } else if (this .psiElement instanceof XmlTag ) {
107- return this .attributeValue = new XmlTagAttributeValue ((XmlTag ) this .scope );
108- }
109-
110- return this .attributeValue = new DummyAttributeValue (this .psiElement );
119+ this .contextType = fileResourceContextTypeEnum ;
120+ this .contextValues = contextValues ;
111121 }
112122
113123 @ NotNull
@@ -121,14 +131,13 @@ public PsiElement getPsiElement() {
121131 }
122132
123133 @ NotNull
124- public FileResource createFileResource () {
125- FileResource fileResource = new FileResource (this .getResource ());
126- String prefix = this .getAttributeValue ().getString ("prefix" );
127- if (prefix != null ) {
128- fileResource .setPrefix (prefix );
129- }
134+ public FileResourceContextTypeEnum getContextType () {
135+ return contextType ;
136+ }
130137
131- return fileResource ;
138+ @ NotNull
139+ public FileResource createFileResource () {
140+ return new FileResource (this .getResource (), this .getContextType (), new TreeMap <>(this .contextValues ));
132141 }
133142 }
134143}
0 commit comments