35
35
use TheCodingMachine \GraphQLite \TypeGenerator ;
36
36
use TheCodingMachine \GraphQLite \Types \MutableObjectType ;
37
37
use TheCodingMachine \GraphQLite \Types \ResolvableInputObjectType ;
38
+ use function var_dump ;
38
39
39
40
/**
40
41
* Detects controllers and types automatically and tag them.
@@ -51,29 +52,20 @@ class GraphqliteCompilerPass implements CompilerPassInterface
51
52
*/
52
53
public function process (ContainerBuilder $ container )
53
54
{
54
- /**
55
- * @var array<string, string> An array matching class name to the the container identifier of a factory creating a type.
56
- */
57
- $ types = [];
58
-
59
- /**
60
- * @var array<string, string> An array matching class name to the the container identifier of a factory creating an input type.
61
- */
62
- $ inputTypes = [];
63
-
64
- /**
65
- * @var array<string, string> An array matching a Graphql type name to the the container identifier of a factory creating a type.
66
- */
67
- $ typesByName = [];
68
-
69
- $ namingStrategy = new NamingStrategy ();
70
55
$ reader = $ this ->getAnnotationReader ();
71
56
//$inputTypeUtils = new InputTypeUtils($reader, $namingStrategy);
72
57
73
58
// Let's scan the whole container and tag the services that belong to the namespace we want to inspect.
74
59
$ controllersNamespaces = $ container ->getParameter ('graphqlite.namespace.controllers ' );
75
60
$ typesNamespaces = $ container ->getParameter ('graphqlite.namespace.types ' );
76
61
62
+ // 2 seconds of TTL in environment mode. Otherwise, let's cache forever!
63
+ $ env = $ container ->getParameter ('kernel.environment ' );
64
+ $ globTtl = null ;
65
+ if ($ env === 'dev ' ) {
66
+ $ globTtl = 2 ;
67
+ }
68
+
77
69
foreach ($ container ->getDefinitions () as $ id => $ definition ) {
78
70
if ($ definition ->isAbstract () || $ definition ->getClass () === null ) {
79
71
continue ;
@@ -144,73 +136,11 @@ public function process(ContainerBuilder $container)
144
136
$ container ->setDefinition ($ controllerIdentifier , $ queryProvider );
145
137
}
146
138
}
147
- /*
148
- foreach ($container->findTaggedServiceIds('graphql.annotated.type') as $id => $tag) {
149
- $used = false;
150
- $definition = $container->findDefinition($id);
151
- $class = $definition->getClass();
152
- if ($class === null) {
153
- throw new \RuntimeException(sprintf('Service %s has no class defined.', $id));
154
- }
155
-
156
- $reflectionClass = new ReflectionClass($class);
157
- foreach ($reflectionClass->getMethods() as $method) {
158
- $factory = $reader->getFactoryAnnotation($method);
159
- if ($factory !== null) {
160
- $objectTypeIdentifier = $class.'__'.$method->getName().'__InputType';
161
-
162
- $objectType = new Definition(ResolvableInputObjectType::class);
163
- $objectType->setPrivate(false);
164
- $objectType->setFactory([self::class, 'createInputObjectType']);
165
- $objectType->addArgument(new Reference($id));
166
- $objectType->addArgument($method->getName());
167
- $objectType->addArgument(new Reference(InputTypeGenerator::class));
168
- $objectType->addArgument(new Reference(RecursiveTypeMapperInterface::class));
169
- $container->setDefinition($objectTypeIdentifier, $objectType);
170
-
171
- [$inputName, $inputClassName] = $inputTypeUtils->getInputTypeNameAndClassName($method);
172
-
173
- $inputTypes[$inputClassName] = $objectTypeIdentifier;
174
- $typesByName[$inputName] = $objectTypeIdentifier;
175
-
176
- $used = true;
177
- }
178
- }
179
-
180
- $typeAnnotation = $this->annotationReader->getTypeAnnotation($reflectionClass);
181
- if ($typeAnnotation !== null) {
182
- $objectTypeIdentifier = $class.'__Type';
183
-
184
- $objectType = new Definition(ObjectType::class);
185
- $objectType->setPrivate(false);
186
- $objectType->setFactory([self::class, 'createObjectType']);
187
- $objectType->addArgument($id);
188
- $objectType->addArgument(new Reference(TypeGenerator::class));
189
- $objectType->addArgument(new Reference(RecursiveTypeMapperInterface::class));
190
- $container->setDefinition($objectTypeIdentifier, $objectType);
191
-
192
- $types[$typeAnnotation->getClass()] = $objectTypeIdentifier;
193
- $typesByName[$namingStrategy->getOutputTypeName($class, $typeAnnotation)] = $objectTypeIdentifier;
194
- //$definition->addTag('graphql.annotated_type');
195
-
196
- $used = true;
197
- }
198
-
199
- // If the service is used for GraphQL, since it is referenced by service name in the factories, let's make it public
200
- if ($used) {
201
- $container->findDefinition($id)->setPublic(true);
202
- }
203
- }
204
-
205
- $containerFetcherTypeMapper = $container->getDefinition(ContainerFetcherTypeMapper::class);
206
- $containerFetcherTypeMapper->replaceArgument(1, $types);
207
- $containerFetcherTypeMapper->replaceArgument(2, $inputTypes);
208
- $containerFetcherTypeMapper->replaceArgument(3, $typesByName);
209
- */
210
139
211
140
foreach ($ typesNamespaces as $ typesNamespace ) {
212
141
$ definition = new Definition (GlobTypeMapper::class);
213
142
$ definition ->addArgument ($ typesNamespace );
143
+ $ definition ->setArgument ('$globTtl ' , $ globTtl );
214
144
$ definition ->setAutowired (true );
215
145
$ definition ->addTag ('graphql.type_mapper ' );
216
146
$ container ->setDefinition ('globTypeMapper_ ' .str_replace ('\\' , '__ ' , $ typesNamespace ), $ definition );
@@ -254,22 +184,6 @@ public static function createQueryProvider($controller, FieldsBuilderFactory $fi
254
184
return new ControllerQueryProvider ($ controller , $ fieldsBuilderFactory ->buildFieldsBuilder ($ recursiveTypeMapper ));
255
185
}
256
186
257
- /**
258
- * @param string $typeClass
259
- */
260
- /*public static function createObjectType(string $typeClass, TypeGenerator $typeGenerator, RecursiveTypeMapperInterface $recursiveTypeMapper): MutableObjectType
261
- {
262
- return $typeGenerator->mapAnnotatedObject($typeClass, $recursiveTypeMapper);
263
- }*/
264
-
265
- /**
266
- * @param object $factory
267
- */
268
- /*public static function createInputObjectType($factory, string $methodName, InputTypeGenerator $inputTypeGenerator, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
269
- {
270
- return $inputTypeGenerator->mapFactoryMethod($factory, $methodName, $recursiveTypeMapper);
271
- }*/
272
-
273
187
/**
274
188
* Returns a cached Doctrine annotation reader.
275
189
* Note: we cannot get the annotation reader service in the container as we are in a compiler pass.
0 commit comments