6
6
use function array_keys ;
7
7
use function filemtime ;
8
8
use GraphQL \Type \Definition \InputObjectType ;
9
- use GraphQL \Type \Definition \InputType ;
10
- use GraphQL \Type \Definition \ObjectType ;
11
9
use GraphQL \Type \Definition \OutputType ;
12
10
use Mouf \Composer \ClassNameMapper ;
13
11
use Psr \Container \ContainerInterface ;
17
15
use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
18
16
use TheCodingMachine \GraphQLite \AnnotationReader ;
19
17
use TheCodingMachine \GraphQLite \Annotations \ExtendType ;
20
- use TheCodingMachine \GraphQLite \Annotations \Factory ;
21
18
use TheCodingMachine \GraphQLite \Annotations \Type ;
22
19
use TheCodingMachine \GraphQLite \InputTypeGenerator ;
23
20
use TheCodingMachine \GraphQLite \InputTypeUtils ;
24
- use TheCodingMachine \ GraphQLite \ NamingStrategy ;
21
+ use GraphQL \ Type \ Definition \ InputType ;
25
22
use TheCodingMachine \GraphQLite \NamingStrategyInterface ;
26
23
use TheCodingMachine \GraphQLite \TypeGenerator ;
27
24
use TheCodingMachine \GraphQLite \Types \MutableObjectType ;
@@ -93,7 +90,11 @@ final class GlobTypeMapper implements TypeMapperInterface
93
90
/**
94
91
* @var bool
95
92
*/
96
- private $ fullExtendMapComputed = false ;
93
+ private $ fullMapClassToExtendTypeArrayComputed = false ;
94
+ /**
95
+ * @var bool
96
+ */
97
+ private $ fullMapNameToExtendTypeArrayComputed = false ;
97
98
/**
98
99
* @var NamingStrategyInterface
99
100
*/
@@ -197,44 +198,38 @@ private function getMapInputNameToFactory(): array
197
198
return $ this ->getMaps ()['mapInputNameToFactory ' ];
198
199
}
199
200
200
- /**
201
- * Returns an array of fully qualified class names.
202
- *
203
- * @return array<string,array<string,string>>
204
- */
205
- private function getExtendMaps (RecursiveTypeMapperInterface $ recursiveTypeMapper ): array
201
+ private function getMapClassToExtendTypeArray (): array
206
202
{
207
- if ($ this ->fullExtendMapComputed === false ) {
203
+ if ($ this ->fullMapClassToExtendTypeArrayComputed === false ) {
208
204
$ namespace = str_replace ('\\' , '_ ' , $ this ->namespace );
209
205
$ keyExtendClassCache = 'globTypeMapperExtend_ ' .$ namespace ;
210
- $ keyExtendNameCache = 'globTypeMapperExtend_names_ ' .$ namespace ;
211
206
$ this ->mapClassToExtendTypeArray = $ this ->cache ->get ($ keyExtendClassCache );
212
- $ this ->mapNameToExtendType = $ this ->cache ->get ($ keyExtendNameCache );
213
- if ($ this ->mapClassToExtendTypeArray === null ||
214
- $ this ->mapNameToExtendType === null
215
- ) {
216
- $ this ->buildExtendMap ($ recursiveTypeMapper );
207
+ if ($ this ->mapClassToExtendTypeArray === null ) {
208
+ $ this ->buildMapClassToExtendTypeArray ();
217
209
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
218
210
// Defaults to 2 seconds.
219
211
$ this ->cache ->set ($ keyExtendClassCache , $ this ->mapClassToExtendTypeArray , $ this ->globTtl );
220
- $ this ->cache ->set ($ keyExtendNameCache , $ this ->mapNameToExtendType , $ this ->globTtl );
221
212
}
222
- $ this ->fullExtendMapComputed = true ;
213
+ $ this ->fullMapClassToExtendTypeArrayComputed = true ;
223
214
}
224
- return [
225
- 'mapClassToExtendTypeArray ' => $ this ->mapClassToExtendTypeArray ,
226
- 'mapNameToExtendType ' => $ this ->mapNameToExtendType ,
227
- ];
228
- }
229
-
230
- private function getMapClassToExtendTypeArray (RecursiveTypeMapperInterface $ recursiveTypeMapper ): array
231
- {
232
- return $ this ->getExtendMaps ($ recursiveTypeMapper )['mapClassToExtendTypeArray ' ];
215
+ return $ this ->mapClassToExtendTypeArray ;
233
216
}
234
217
235
218
private function getMapNameToExtendType (RecursiveTypeMapperInterface $ recursiveTypeMapper ): array
236
219
{
237
- return $ this ->getExtendMaps ($ recursiveTypeMapper )['mapNameToExtendType ' ];
220
+ if ($ this ->fullMapNameToExtendTypeArrayComputed === false ) {
221
+ $ namespace = str_replace ('\\' , '_ ' , $ this ->namespace );
222
+ $ keyExtendNameCache = 'globTypeMapperExtend_names_ ' .$ namespace ;
223
+ $ this ->mapNameToExtendType = $ this ->cache ->get ($ keyExtendNameCache );
224
+ if ($ this ->mapNameToExtendType === null ) {
225
+ $ this ->buildMapNameToExtendTypeArray ($ recursiveTypeMapper );
226
+ // This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
227
+ // Defaults to 2 seconds.
228
+ $ this ->cache ->set ($ keyExtendNameCache , $ this ->mapNameToExtendType , $ this ->globTtl );
229
+ }
230
+ $ this ->fullMapNameToExtendTypeArrayComputed = true ;
231
+ }
232
+ return $ this ->mapNameToExtendType ;
238
233
}
239
234
240
235
/**
@@ -270,6 +265,7 @@ private function buildMap(): void
270
265
$ this ->mapClassToFactory = [];
271
266
$ this ->mapInputNameToFactory = [];
272
267
268
+ /** @var ReflectionClass[] $classes */
273
269
$ classes = $ this ->getClassList ();
274
270
foreach ($ classes as $ className => $ refClass ) {
275
271
$ type = $ this ->annotationReader ->getTypeAnnotation ($ refClass );
@@ -285,7 +281,12 @@ private function buildMap(): void
285
281
$ this ->storeTypeInCache ($ className , $ type , $ refClass ->getFileName ());
286
282
}
287
283
284
+ $ isAbstract = $ refClass ->isAbstract ();
285
+
288
286
foreach ($ refClass ->getMethods () as $ method ) {
287
+ if (!$ method ->isPublic () || ($ isAbstract && !$ method ->isStatic ())) {
288
+ continue ;
289
+ }
289
290
$ factory = $ this ->annotationReader ->getFactoryAnnotation ($ method );
290
291
if ($ factory !== null ) {
291
292
[$ inputName , $ className ] = $ this ->inputTypeUtils ->getInputTypeNameAndClassName ($ method );
@@ -300,16 +301,28 @@ private function buildMap(): void
300
301
}
301
302
}
302
303
303
- private function buildExtendMap ( RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
304
+ private function buildMapClassToExtendTypeArray ( ): void
304
305
{
305
306
$ this ->mapClassToExtendTypeArray = [];
307
+ $ classes = $ this ->getClassList ();
308
+ foreach ($ classes as $ className => $ refClass ) {
309
+ $ extendType = $ this ->annotationReader ->getExtendTypeAnnotation ($ refClass );
310
+
311
+ if ($ extendType !== null ) {
312
+ $ this ->storeExtendTypeMapperByClassInCache ($ className , $ extendType , $ refClass ->getFileName ());
313
+ }
314
+ }
315
+ }
316
+
317
+ private function buildMapNameToExtendTypeArray (RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
318
+ {
306
319
$ this ->mapNameToExtendType = [];
307
320
$ classes = $ this ->getClassList ();
308
321
foreach ($ classes as $ className => $ refClass ) {
309
322
$ extendType = $ this ->annotationReader ->getExtendTypeAnnotation ($ refClass );
310
323
311
324
if ($ extendType !== null ) {
312
- $ this ->storeExtendTypeInCache ($ className , $ extendType , $ refClass ->getFileName (), $ recursiveTypeMapper );
325
+ $ this ->storeExtendTypeMapperByNameInCache ($ className , $ extendType , $ refClass ->getFileName (), $ recursiveTypeMapper );
313
326
}
314
327
}
315
328
}
@@ -355,10 +368,11 @@ private function storeInputTypeInCache(ReflectionMethod $refMethod, string $inpu
355
368
], $ this ->mapTtl );
356
369
}
357
370
371
+
358
372
/**
359
- * Stores in cache the mapping ExtendTypeClass <=> Object class <=> GraphQL type name .
373
+ * Stores in cache the mapping ExtendTypeClass <=> Object class.
360
374
*/
361
- private function storeExtendTypeInCache (string $ extendTypeClassName , ExtendType $ extendType , string $ typeFileName, RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
375
+ private function storeExtendTypeMapperByClassInCache (string $ extendTypeClassName , ExtendType $ extendType , string $ typeFileName ): void
362
376
{
363
377
$ objectClassName = $ extendType ->getClass ();
364
378
$ this ->mapClassToExtendTypeArray [$ objectClassName ][$ extendTypeClassName ] = $ extendTypeClassName ;
@@ -367,15 +381,21 @@ private function storeExtendTypeInCache(string $extendTypeClassName, ExtendType
367
381
'fileName ' => $ typeFileName ,
368
382
'extendTypeClasses ' => $ this ->mapClassToExtendTypeArray [$ objectClassName ]
369
383
], $ this ->mapTtl );
384
+ }
370
385
386
+ /**
387
+ * Stores in cache the mapping ExtendTypeClass <=> name class.
388
+ */
389
+ private function storeExtendTypeMapperByNameInCache (string $ extendTypeClassName , ExtendType $ extendType , string $ typeFileName , RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
390
+ {
371
391
$ targetType = $ recursiveTypeMapper ->mapClassToType ($ extendType ->getClass (), null );
372
392
$ typeName = $ targetType ->name ;
373
393
374
394
$ this ->mapNameToExtendType [$ typeName ][$ extendTypeClassName ] = $ extendTypeClassName ;
375
395
$ this ->cache ->set ('globExtendTypeMapperByName_ ' .$ typeName , [
376
396
'filemtime ' => filemtime ($ typeFileName ),
377
397
'fileName ' => $ typeFileName ,
378
- 'extendTypeClasses ' => $ this ->mapClassToExtendTypeArray [ $ objectClassName ]
398
+ 'extendTypeClasses ' => $ this ->mapNameToExtendType [ $ typeName ]
379
399
], $ this ->mapTtl );
380
400
}
381
401
@@ -709,7 +729,7 @@ public function canExtendTypeForClass(string $className, MutableObjectType $type
709
729
$ extendTypeClassName = $ this ->getExtendTypesFromCacheByObjectClass ($ className );
710
730
711
731
if ($ extendTypeClassName === null ) {
712
- $ map = $ this ->getMapClassToExtendTypeArray ($ recursiveTypeMapper );
732
+ $ map = $ this ->getMapClassToExtendTypeArray ();
713
733
}
714
734
715
735
return isset ($ this ->mapClassToExtendTypeArray [$ className ]);
@@ -728,7 +748,7 @@ public function extendTypeForClass(string $className, MutableObjectType $type, R
728
748
$ extendTypeClassNames = $ this ->getExtendTypesFromCacheByObjectClass ($ className );
729
749
730
750
if ($ extendTypeClassNames === null ) {
731
- $ this ->getExtendMaps ( $ recursiveTypeMapper );
751
+ $ this ->getMapClassToExtendTypeArray ( );
732
752
}
733
753
734
754
if (!isset ($ this ->mapClassToExtendTypeArray [$ className ])) {
@@ -760,9 +780,9 @@ public function canExtendTypeForName(string $typeName, MutableObjectType $type,
760
780
return true;
761
781
}*/
762
782
763
- $ this ->getExtendMaps ($ recursiveTypeMapper );
783
+ $ map = $ this ->getMapNameToExtendType ($ recursiveTypeMapper );
764
784
765
- return isset ($ this -> mapNameToExtendType [$ typeName ])/* || isset($this->mapInputNameToFactory[$typeName])*/ ;
785
+ return isset ($ map [$ typeName ])/* || isset($this->mapInputNameToFactory[$typeName])*/ ;
766
786
}
767
787
768
788
/**
0 commit comments