44namespace TheCodingMachine \Graphql \Controllers \Bundle \DependencyInjection ;
55
66use function class_exists ;
7- use function dirname ;
87use Doctrine \Common \Annotations \AnnotationException ;
98use Doctrine \Common \Annotations \AnnotationReader as DoctrineAnnotationReader ;
109use Doctrine \Common \Annotations \AnnotationRegistry ;
1514use GraphQL \Type \Definition \ObjectType ;
1615use Psr \Container \ContainerInterface ;
1716use ReflectionClass ;
17+ use function str_replace ;
1818use function strpos ;
1919use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
2020use Symfony \Component \DependencyInjection \ContainerBuilder ;
2828use TheCodingMachine \GraphQL \Controllers \FieldsBuilderFactory ;
2929use TheCodingMachine \GraphQL \Controllers \InputTypeGenerator ;
3030use TheCodingMachine \GraphQL \Controllers \InputTypeUtils ;
31+ use TheCodingMachine \GraphQL \Controllers \Mappers \GlobTypeMapper ;
3132use TheCodingMachine \GraphQL \Controllers \Mappers \RecursiveTypeMapperInterface ;
3233use TheCodingMachine \GraphQL \Controllers \Mappers \StaticTypeMapper ;
3334use TheCodingMachine \GraphQL \Controllers \NamingStrategy ;
@@ -66,21 +67,39 @@ public function process(ContainerBuilder $container)
6667
6768 $ namingStrategy = new NamingStrategy ();
6869 $ reader = $ this ->getAnnotationReader ();
69- $ inputTypeUtils = new InputTypeUtils ($ reader , $ namingStrategy );
70+ // $inputTypeUtils = new InputTypeUtils($reader, $namingStrategy);
7071
7172 // Let's scan the whole container and tag the services that belong to the namespace we want to inspect.
72- $ controllersNamespace = $ container ->getParameter ('graphql_controllers.namespace.controllers ' );
73- $ typesNamespace = $ container ->getParameter ('graphql_controllers.namespace.types ' );
73+ $ controllersNamespaces = $ container ->getParameter ('graphql_controllers.namespace.controllers ' );
74+ $ typesNamespaces = $ container ->getParameter ('graphql_controllers.namespace.types ' );
7475
7576 foreach ($ container ->getDefinitions () as $ id => $ definition ) {
7677 if ($ definition ->isAbstract () || $ definition ->getClass () === null ) {
7778 continue ;
7879 }
79- if (strpos ($ definition ->getClass (), $ controllersNamespace ) === 0 ) {
80- $ definition ->addTag ('graphql.annotated.controller ' );
80+ $ class = $ definition ->getClass ();
81+ foreach ($ controllersNamespaces as $ controllersNamespace ) {
82+ if (strpos ($ class , $ controllersNamespace ) === 0 ) {
83+ $ definition ->addTag ('graphql.annotated.controller ' );
84+ }
8185 }
82- if (strpos ($ definition ->getClass (), $ typesNamespace ) === 0 ) {
83- $ definition ->addTag ('graphql.annotated.type ' );
86+
87+ foreach ($ typesNamespaces as $ typesNamespace ) {
88+ if (strpos ($ class , $ typesNamespace ) === 0 ) {
89+ //$definition->addTag('graphql.annotated.type');
90+ // Set the types public
91+ $ reflectionClass = new ReflectionClass ($ class );
92+ if ($ this ->getAnnotationReader ()->getTypeAnnotation ($ reflectionClass ) !== null || $ this ->getAnnotationReader ()->getExtendTypeAnnotation ($ reflectionClass ) !== null ) {
93+ $ definition ->setPublic (true );
94+ } else {
95+ foreach ($ reflectionClass ->getMethods () as $ method ) {
96+ $ factory = $ reader ->getFactoryAnnotation ($ method );
97+ if ($ factory !== null ) {
98+ $ definition ->setPublic (true );
99+ }
100+ }
101+ }
102+ }
84103 }
85104 }
86105
@@ -120,8 +139,9 @@ public function process(ContainerBuilder $container)
120139 $ container ->setDefinition ($ controllerIdentifier , $ queryProvider );
121140 }
122141 }
123-
142+ /*
124143 foreach ($container->findTaggedServiceIds('graphql.annotated.type') as $id => $tag) {
144+ $used = false;
125145 $definition = $container->findDefinition($id);
126146 $class = $definition->getClass();
127147 if ($class === null) {
@@ -148,6 +168,7 @@ public function process(ContainerBuilder $container)
148168 $inputTypes[$inputClassName] = $objectTypeIdentifier;
149169 $typesByName[$inputName] = $objectTypeIdentifier;
150170
171+ $used = true;
151172 }
152173 }
153174
@@ -158,27 +179,38 @@ public function process(ContainerBuilder $container)
158179 $objectType = new Definition(ObjectType::class);
159180 $objectType->setPrivate(false);
160181 $objectType->setFactory([self::class, 'createObjectType']);
161- $ objectType ->addArgument (new Reference ( $ id) );
182+ $objectType->addArgument($id);
162183 $objectType->addArgument(new Reference(TypeGenerator::class));
163184 $objectType->addArgument(new Reference(RecursiveTypeMapperInterface::class));
164185 $container->setDefinition($objectTypeIdentifier, $objectType);
165186
166187 $types[$typeAnnotation->getClass()] = $objectTypeIdentifier;
167188 $typesByName[$namingStrategy->getOutputTypeName($class, $typeAnnotation)] = $objectTypeIdentifier;
168189 //$definition->addTag('graphql.annotated_type');
190+
191+ $used = true;
192+ }
193+
194+ // If the service is used for GraphQL, since it is referenced by service name in the factories, let's make it public
195+ if ($used) {
196+ $container->findDefinition($id)->setPublic(true);
169197 }
170198 }
171199
172200 $containerFetcherTypeMapper = $container->getDefinition(ContainerFetcherTypeMapper::class);
173201 $containerFetcherTypeMapper->replaceArgument(1, $types);
174202 $containerFetcherTypeMapper->replaceArgument(2, $inputTypes);
175203 $containerFetcherTypeMapper->replaceArgument(3, $typesByName);
176- /*$containerFetcherTypeMapper = new Definition(ContainerFetcherTypeMapper::class);
177- $containerFetcherTypeMapper->addArgument($container->getDefinition('service_container'));
178- $containerFetcherTypeMapper->addArgument($types);
179- $containerFetcherTypeMapper->addArgument([]);
180- $containerFetcherTypeMapper->addTag('graphql.type_mapper');
181- $container->setDefinition(ContainerFetcherTypeMapper::class, $containerFetcherTypeMapper);*/
204+ */
205+
206+ foreach ($ typesNamespaces as $ typesNamespace ) {
207+ $ definition = new Definition (GlobTypeMapper::class);
208+ $ definition ->addArgument ($ typesNamespace );
209+ $ definition ->setAutowired (true );
210+ $ definition ->addTag ('graphql.type_mapper ' );
211+ $ container ->setDefinition ('globTypeMapper_ ' .str_replace ('\\' , '__ ' , $ typesNamespace ), $ definition );
212+ }
213+
182214
183215 // Register custom output types
184216 $ taggedServices = $ container ->findTaggedServiceIds ('graphql.output_type ' );
0 commit comments