36
36
37
37
class ConfigParserPass implements CompilerPassInterface
38
38
{
39
+ public const TYPE_YAML = 'yaml ' ;
40
+ public const TYPE_GRAPHQL = 'graphql ' ;
41
+ public const TYPE_ANNOTATION = 'annotation ' ;
42
+ public const TYPE_ATTRIBUTE = 'attribute ' ;
43
+
44
+ public const SUPPORTED_TYPES = [
45
+ self ::TYPE_YAML ,
46
+ self ::TYPE_GRAPHQL ,
47
+ self ::TYPE_ANNOTATION ,
48
+ self ::TYPE_ATTRIBUTE ,
49
+ ];
50
+
39
51
public const SUPPORTED_TYPES_EXTENSIONS = [
40
- ' yaml ' => '{yaml,yml} ' ,
41
- ' graphql ' => '{graphql,graphqls} ' ,
42
- ' annotation ' => 'php ' ,
43
- ' attribute ' => 'php ' ,
52
+ self :: TYPE_YAML => '{yaml,yml} ' ,
53
+ self :: TYPE_GRAPHQL => '{graphql,graphqls} ' ,
54
+ self :: TYPE_ANNOTATION => 'php ' ,
55
+ self :: TYPE_ATTRIBUTE => 'php ' ,
44
56
];
45
57
46
58
/**
59
+ * @deprecated They are going to be configurable.
47
60
* @var array<string, class-string<ParserInterface|PreParserInterface>>
48
61
*/
49
62
public const PARSERS = [
50
- ' yaml ' => YamlParser::class,
51
- ' graphql ' => GraphQLParser::class,
52
- ' annotation ' => AnnotationParser::class,
53
- ' attribute ' => AttributeParser::class,
63
+ self :: TYPE_YAML => YamlParser::class,
64
+ self :: TYPE_GRAPHQL => GraphQLParser::class,
65
+ self :: TYPE_ANNOTATION => AnnotationParser::class,
66
+ self :: TYPE_ATTRIBUTE => AttributeParser::class,
54
67
];
55
68
56
69
private const DEFAULT_CONFIG = [
@@ -64,6 +77,7 @@ class ConfigParserPass implements CompilerPassInterface
64
77
'types ' => [],
65
78
],
66
79
],
80
+ 'parsers ' => self ::PARSERS ,
67
81
];
68
82
69
83
/**
@@ -93,6 +107,10 @@ private function getConfigs(ContainerBuilder $container): array
93
107
$ config = $ container ->getParameterBag ()->resolveValue ($ container ->getParameter ('overblog_graphql.config ' ));
94
108
$ container ->getParameterBag ()->remove ('overblog_graphql.config ' );
95
109
$ container ->setParameter ($ this ->getAlias ().'.classes_map ' , []);
110
+
111
+ // use default value if needed
112
+ $ config = array_replace_recursive (self ::DEFAULT_CONFIG , $ config );
113
+
96
114
$ typesMappings = $ this ->mappingConfig ($ config , $ container );
97
115
// reset treated files
98
116
$ this ->treatedFiles = [];
@@ -103,7 +121,7 @@ private function getConfigs(ContainerBuilder $container): array
103
121
// Pre-parse all files
104
122
AnnotationParser::reset ($ config );
105
123
AttributeParser::reset ($ config );
106
- $ typesNeedPreParsing = $ this ->typesNeedPreParsing ();
124
+ $ typesNeedPreParsing = $ this ->typesNeedPreParsing ($ config [ ' parsers ' ] );
107
125
foreach ($ typesMappings as $ params ) {
108
126
if ($ typesNeedPreParsing [$ params ['type ' ]]) {
109
127
$ this ->parseTypeConfigFiles ($ params ['type ' ], $ params ['files ' ], $ container , $ config , true );
@@ -122,10 +140,15 @@ private function getConfigs(ContainerBuilder $container): array
122
140
return $ flattenTypeConfig ;
123
141
}
124
142
125
- private function typesNeedPreParsing (): array
143
+ /**
144
+ * @param array<string,string> $parsers
145
+ *
146
+ * @return array<string,bool>
147
+ */
148
+ private function typesNeedPreParsing (array $ parsers ): array
126
149
{
127
150
$ needPreParsing = [];
128
- foreach (self :: PARSERS as $ type => $ className ) {
151
+ foreach ($ parsers as $ type => $ className ) {
129
152
$ needPreParsing [$ type ] = is_a ($ className , PreParserInterface::class, true );
130
153
}
131
154
@@ -152,7 +175,7 @@ private function parseTypeConfigFiles(string $type, iterable $files, ContainerBu
152
175
continue ;
153
176
}
154
177
155
- $ parser = [self :: PARSERS [$ type ], $ method ];
178
+ $ parser = [$ configs [ ' parsers ' ] [$ type ], $ method ];
156
179
if (is_callable ($ parser )) {
157
180
$ config [] = ($ parser )($ file , $ container , $ configs );
158
181
}
@@ -176,9 +199,6 @@ private function checkTypesDuplication(array $typeConfigs): void
176
199
177
200
private function mappingConfig (array $ config , ContainerBuilder $ container ): array
178
201
{
179
- // use default value if needed
180
- $ config = array_replace_recursive (self ::DEFAULT_CONFIG , $ config );
181
-
182
202
$ mappingConfig = $ config ['definitions ' ]['mappings ' ];
183
203
$ typesMappings = $ mappingConfig ['types ' ];
184
204
0 commit comments