66use function array_keys ;
77use function filemtime ;
88use GraphQL \Type \Definition \InputObjectType ;
9- use GraphQL \Type \Definition \InputType ;
10- use GraphQL \Type \Definition \ObjectType ;
119use GraphQL \Type \Definition \OutputType ;
1210use Mouf \Composer \ClassNameMapper ;
1311use Psr \Container \ContainerInterface ;
1715use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
1816use TheCodingMachine \GraphQLite \AnnotationReader ;
1917use TheCodingMachine \GraphQLite \Annotations \ExtendType ;
20- use TheCodingMachine \GraphQLite \Annotations \Factory ;
2118use TheCodingMachine \GraphQLite \Annotations \Type ;
2219use TheCodingMachine \GraphQLite \InputTypeGenerator ;
2320use TheCodingMachine \GraphQLite \InputTypeUtils ;
24- use TheCodingMachine \ GraphQLite \ NamingStrategy ;
21+ use GraphQL \ Type \ Definition \ InputType ;
2522use TheCodingMachine \GraphQLite \NamingStrategyInterface ;
2623use TheCodingMachine \GraphQLite \TypeGenerator ;
2724use TheCodingMachine \GraphQLite \Types \MutableObjectType ;
@@ -93,7 +90,11 @@ final class GlobTypeMapper implements TypeMapperInterface
9390 /**
9491 * @var bool
9592 */
96- private $ fullExtendMapComputed = false ;
93+ private $ fullMapClassToExtendTypeArrayComputed = false ;
94+ /**
95+ * @var bool
96+ */
97+ private $ fullMapNameToExtendTypeArrayComputed = false ;
9798 /**
9899 * @var NamingStrategyInterface
99100 */
@@ -197,44 +198,38 @@ private function getMapInputNameToFactory(): array
197198 return $ this ->getMaps ()['mapInputNameToFactory ' ];
198199 }
199200
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
206202 {
207- if ($ this ->fullExtendMapComputed === false ) {
203+ if ($ this ->fullMapClassToExtendTypeArrayComputed === false ) {
208204 $ namespace = str_replace ('\\' , '_ ' , $ this ->namespace );
209205 $ keyExtendClassCache = 'globTypeMapperExtend_ ' .$ namespace ;
210- $ keyExtendNameCache = 'globTypeMapperExtend_names_ ' .$ namespace ;
211206 $ 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 ();
217209 // This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
218210 // Defaults to 2 seconds.
219211 $ this ->cache ->set ($ keyExtendClassCache , $ this ->mapClassToExtendTypeArray , $ this ->globTtl );
220- $ this ->cache ->set ($ keyExtendNameCache , $ this ->mapNameToExtendType , $ this ->globTtl );
221212 }
222- $ this ->fullExtendMapComputed = true ;
213+ $ this ->fullMapClassToExtendTypeArrayComputed = true ;
223214 }
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 ;
233216 }
234217
235218 private function getMapNameToExtendType (RecursiveTypeMapperInterface $ recursiveTypeMapper ): array
236219 {
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 ;
238233 }
239234
240235 /**
@@ -270,6 +265,7 @@ private function buildMap(): void
270265 $ this ->mapClassToFactory = [];
271266 $ this ->mapInputNameToFactory = [];
272267
268+ /** @var ReflectionClass[] $classes */
273269 $ classes = $ this ->getClassList ();
274270 foreach ($ classes as $ className => $ refClass ) {
275271 $ type = $ this ->annotationReader ->getTypeAnnotation ($ refClass );
@@ -285,7 +281,12 @@ private function buildMap(): void
285281 $ this ->storeTypeInCache ($ className , $ type , $ refClass ->getFileName ());
286282 }
287283
284+ $ isAbstract = $ refClass ->isAbstract ();
285+
288286 foreach ($ refClass ->getMethods () as $ method ) {
287+ if (!$ method ->isPublic () || ($ isAbstract && !$ method ->isStatic ())) {
288+ continue ;
289+ }
289290 $ factory = $ this ->annotationReader ->getFactoryAnnotation ($ method );
290291 if ($ factory !== null ) {
291292 [$ inputName , $ className ] = $ this ->inputTypeUtils ->getInputTypeNameAndClassName ($ method );
@@ -300,16 +301,28 @@ private function buildMap(): void
300301 }
301302 }
302303
303- private function buildExtendMap ( RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
304+ private function buildMapClassToExtendTypeArray ( ): void
304305 {
305306 $ 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+ {
306319 $ this ->mapNameToExtendType = [];
307320 $ classes = $ this ->getClassList ();
308321 foreach ($ classes as $ className => $ refClass ) {
309322 $ extendType = $ this ->annotationReader ->getExtendTypeAnnotation ($ refClass );
310323
311324 if ($ extendType !== null ) {
312- $ this ->storeExtendTypeInCache ($ className , $ extendType , $ refClass ->getFileName (), $ recursiveTypeMapper );
325+ $ this ->storeExtendTypeMapperByNameInCache ($ className , $ extendType , $ refClass ->getFileName (), $ recursiveTypeMapper );
313326 }
314327 }
315328 }
@@ -355,10 +368,11 @@ private function storeInputTypeInCache(ReflectionMethod $refMethod, string $inpu
355368 ], $ this ->mapTtl );
356369 }
357370
371+
358372 /**
359- * Stores in cache the mapping ExtendTypeClass <=> Object class <=> GraphQL type name .
373+ * Stores in cache the mapping ExtendTypeClass <=> Object class.
360374 */
361- private function storeExtendTypeInCache (string $ extendTypeClassName , ExtendType $ extendType , string $ typeFileName, RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
375+ private function storeExtendTypeMapperByClassInCache (string $ extendTypeClassName , ExtendType $ extendType , string $ typeFileName ): void
362376 {
363377 $ objectClassName = $ extendType ->getClass ();
364378 $ this ->mapClassToExtendTypeArray [$ objectClassName ][$ extendTypeClassName ] = $ extendTypeClassName ;
@@ -367,15 +381,21 @@ private function storeExtendTypeInCache(string $extendTypeClassName, ExtendType
367381 'fileName ' => $ typeFileName ,
368382 'extendTypeClasses ' => $ this ->mapClassToExtendTypeArray [$ objectClassName ]
369383 ], $ this ->mapTtl );
384+ }
370385
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+ {
371391 $ targetType = $ recursiveTypeMapper ->mapClassToType ($ extendType ->getClass (), null );
372392 $ typeName = $ targetType ->name ;
373393
374394 $ this ->mapNameToExtendType [$ typeName ][$ extendTypeClassName ] = $ extendTypeClassName ;
375395 $ this ->cache ->set ('globExtendTypeMapperByName_ ' .$ typeName , [
376396 'filemtime ' => filemtime ($ typeFileName ),
377397 'fileName ' => $ typeFileName ,
378- 'extendTypeClasses ' => $ this ->mapClassToExtendTypeArray [ $ objectClassName ]
398+ 'extendTypeClasses ' => $ this ->mapNameToExtendType [ $ typeName ]
379399 ], $ this ->mapTtl );
380400 }
381401
@@ -709,7 +729,7 @@ public function canExtendTypeForClass(string $className, MutableObjectType $type
709729 $ extendTypeClassName = $ this ->getExtendTypesFromCacheByObjectClass ($ className );
710730
711731 if ($ extendTypeClassName === null ) {
712- $ map = $ this ->getMapClassToExtendTypeArray ($ recursiveTypeMapper );
732+ $ map = $ this ->getMapClassToExtendTypeArray ();
713733 }
714734
715735 return isset ($ this ->mapClassToExtendTypeArray [$ className ]);
@@ -728,7 +748,7 @@ public function extendTypeForClass(string $className, MutableObjectType $type, R
728748 $ extendTypeClassNames = $ this ->getExtendTypesFromCacheByObjectClass ($ className );
729749
730750 if ($ extendTypeClassNames === null ) {
731- $ this ->getExtendMaps ( $ recursiveTypeMapper );
751+ $ this ->getMapClassToExtendTypeArray ( );
732752 }
733753
734754 if (!isset ($ this ->mapClassToExtendTypeArray [$ className ])) {
@@ -760,9 +780,9 @@ public function canExtendTypeForName(string $typeName, MutableObjectType $type,
760780 return true;
761781 }*/
762782
763- $ this ->getExtendMaps ($ recursiveTypeMapper );
783+ $ map = $ this ->getMapNameToExtendType ($ recursiveTypeMapper );
764784
765- return isset ($ this -> mapNameToExtendType [$ typeName ])/* || isset($this->mapInputNameToFactory[$typeName])*/ ;
785+ return isset ($ map [$ typeName ])/* || isset($this->mapInputNameToFactory[$typeName])*/ ;
766786 }
767787
768788 /**
0 commit comments