44
55namespace TheCodingMachine \GraphQLite ;
66
7+ use function class_exists ;
8+ use function interface_exists ;
9+ use function is_array ;
10+ use function str_replace ;
711use GraphQL \Type \Definition \FieldDefinition ;
812use InvalidArgumentException ;
913use Mouf \Composer \ClassNameMapper ;
1620use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
1721use TheCodingMachine \GraphQLite \Annotations \Mutation ;
1822use TheCodingMachine \GraphQLite \Annotations \Query ;
19-
20- use function class_exists ;
21- use function interface_exists ;
22- use function is_array ;
23- use function str_replace ;
23+ use TheCodingMachine \GraphQLite \Annotations \Subscription ;
2424
2525/**
2626 * Scans all the classes in a given namespace of the main project (not the vendor directory).
27- * Analyzes all classes and detects "Query" and "Mutation " annotations.
27+ * Analyzes all classes and detects "Query", "Mutation", and "Subscription " annotations.
2828 *
2929 * Assumes that the container contains a class whose identifier is the same as the class name.
3030 */
@@ -53,14 +53,20 @@ public function __construct(
5353 )
5454 {
5555 $ this ->classNameMapper = $ classNameMapper ?? ClassNameMapper::createFromComposerFile (null , null , true );
56- $ this ->cacheContract = new Psr16Adapter ($ this ->cache , str_replace (['\\' , '{ ' , '} ' , '( ' , ') ' , '/ ' , '@ ' , ': ' ], '_ ' , $ namespace ), $ cacheTtl ?? 0 );
56+ $ this ->cacheContract = new Psr16Adapter (
57+ $ this ->cache ,
58+ str_replace (['\\' , '{ ' , '} ' , '( ' , ') ' , '/ ' , '@ ' , ': ' ], '_ ' , $ namespace ),
59+ $ cacheTtl ?? 0 ,
60+ );
5761 }
5862
5963 private function getAggregateControllerQueryProvider (): AggregateControllerQueryProvider
6064 {
61- if ($ this ->aggregateControllerQueryProvider === null ) {
62- $ this ->aggregateControllerQueryProvider = new AggregateControllerQueryProvider ($ this ->getInstancesList (), $ this ->fieldsBuilder , $ this ->container );
63- }
65+ $ this ->aggregateControllerQueryProvider ??= new AggregateControllerQueryProvider (
66+ $ this ->getInstancesList (),
67+ $ this ->fieldsBuilder ,
68+ $ this ->container ,
69+ );
6470
6571 return $ this ->aggregateControllerQueryProvider ;
6672 }
@@ -100,7 +106,7 @@ private function buildInstancesList(): array
100106 if (! $ refClass ->isInstantiable ()) {
101107 continue ;
102108 }
103- if (! $ this ->hasQueriesOrMutations ($ refClass )) {
109+ if (! $ this ->hasOperations ($ refClass )) {
104110 continue ;
105111 }
106112 if (! $ this ->container ->has ($ className )) {
@@ -114,7 +120,7 @@ private function buildInstancesList(): array
114120 }
115121
116122 /** @param ReflectionClass<object> $reflectionClass */
117- private function hasQueriesOrMutations (ReflectionClass $ reflectionClass ): bool
123+ private function hasOperations (ReflectionClass $ reflectionClass ): bool
118124 {
119125 foreach ($ reflectionClass ->getMethods (ReflectionMethod::IS_PUBLIC ) as $ refMethod ) {
120126 $ queryAnnotation = $ this ->annotationReader ->getRequestAnnotation ($ refMethod , Query::class);
@@ -125,6 +131,10 @@ private function hasQueriesOrMutations(ReflectionClass $reflectionClass): bool
125131 if ($ mutationAnnotation !== null ) {
126132 return true ;
127133 }
134+ $ subscriptionAnnotation = $ this ->annotationReader ->getRequestAnnotation ($ refMethod , Subscription::class);
135+ if ($ subscriptionAnnotation !== null ) {
136+ return true ;
137+ }
128138 }
129139 return false ;
130140 }
@@ -140,4 +150,10 @@ public function getMutations(): array
140150 {
141151 return $ this ->getAggregateControllerQueryProvider ()->getMutations ();
142152 }
153+
154+ /** @return array<string,FieldDefinition> */
155+ public function getSubscriptions (): array
156+ {
157+ return $ this ->getAggregateControllerQueryProvider ()->getSubscriptions ();
158+ }
143159}
0 commit comments