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 ;
3435use TheCodingMachine \GraphQL \Controllers \TypeGenerator ;
36+ use TheCodingMachine \GraphQL \Controllers \Types \MutableObjectType ;
3537use TheCodingMachine \GraphQL \Controllers \Types \ResolvableInputObjectType ;
3638
3739/**
@@ -66,21 +68,39 @@ public function process(ContainerBuilder $container)
6668
6769 $ namingStrategy = new NamingStrategy ();
6870 $ reader = $ this ->getAnnotationReader ();
69- $ inputTypeUtils = new InputTypeUtils ($ reader , $ namingStrategy );
71+ // $inputTypeUtils = new InputTypeUtils($reader, $namingStrategy);
7072
7173 // 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 ' );
74+ $ controllersNamespaces = $ container ->getParameter ('graphql_controllers.namespace.controllers ' );
75+ $ typesNamespaces = $ container ->getParameter ('graphql_controllers.namespace.types ' );
7476
7577 foreach ($ container ->getDefinitions () as $ id => $ definition ) {
7678 if ($ definition ->isAbstract () || $ definition ->getClass () === null ) {
7779 continue ;
7880 }
79- if (strpos ($ definition ->getClass (), $ controllersNamespace ) === 0 ) {
80- $ definition ->addTag ('graphql.annotated.controller ' );
81+ $ class = $ definition ->getClass ();
82+ foreach ($ controllersNamespaces as $ controllersNamespace ) {
83+ if (strpos ($ class , $ controllersNamespace ) === 0 ) {
84+ $ definition ->addTag ('graphql.annotated.controller ' );
85+ }
8186 }
82- if (strpos ($ definition ->getClass (), $ typesNamespace ) === 0 ) {
83- $ definition ->addTag ('graphql.annotated.type ' );
87+
88+ foreach ($ typesNamespaces as $ typesNamespace ) {
89+ if (strpos ($ class , $ typesNamespace ) === 0 ) {
90+ //$definition->addTag('graphql.annotated.type');
91+ // Set the types public
92+ $ reflectionClass = new ReflectionClass ($ class );
93+ if ($ this ->getAnnotationReader ()->getTypeAnnotation ($ reflectionClass ) !== null || $ this ->getAnnotationReader ()->getExtendTypeAnnotation ($ reflectionClass ) !== null ) {
94+ $ definition ->setPublic (true );
95+ } else {
96+ foreach ($ reflectionClass ->getMethods () as $ method ) {
97+ $ factory = $ reader ->getFactoryAnnotation ($ method );
98+ if ($ factory !== null ) {
99+ $ definition ->setPublic (true );
100+ }
101+ }
102+ }
103+ }
84104 }
85105 }
86106
@@ -120,8 +140,9 @@ public function process(ContainerBuilder $container)
120140 $ container ->setDefinition ($ controllerIdentifier , $ queryProvider );
121141 }
122142 }
123-
143+ /*
124144 foreach ($container->findTaggedServiceIds('graphql.annotated.type') as $id => $tag) {
145+ $used = false;
125146 $definition = $container->findDefinition($id);
126147 $class = $definition->getClass();
127148 if ($class === null) {
@@ -148,6 +169,7 @@ public function process(ContainerBuilder $container)
148169 $inputTypes[$inputClassName] = $objectTypeIdentifier;
149170 $typesByName[$inputName] = $objectTypeIdentifier;
150171
172+ $used = true;
151173 }
152174 }
153175
@@ -158,27 +180,38 @@ public function process(ContainerBuilder $container)
158180 $objectType = new Definition(ObjectType::class);
159181 $objectType->setPrivate(false);
160182 $objectType->setFactory([self::class, 'createObjectType']);
161- $ objectType ->addArgument (new Reference ( $ id) );
183+ $objectType->addArgument($id);
162184 $objectType->addArgument(new Reference(TypeGenerator::class));
163185 $objectType->addArgument(new Reference(RecursiveTypeMapperInterface::class));
164186 $container->setDefinition($objectTypeIdentifier, $objectType);
165187
166188 $types[$typeAnnotation->getClass()] = $objectTypeIdentifier;
167189 $typesByName[$namingStrategy->getOutputTypeName($class, $typeAnnotation)] = $objectTypeIdentifier;
168190 //$definition->addTag('graphql.annotated_type');
191+
192+ $used = true;
193+ }
194+
195+ // If the service is used for GraphQL, since it is referenced by service name in the factories, let's make it public
196+ if ($used) {
197+ $container->findDefinition($id)->setPublic(true);
169198 }
170199 }
171200
172201 $containerFetcherTypeMapper = $container->getDefinition(ContainerFetcherTypeMapper::class);
173202 $containerFetcherTypeMapper->replaceArgument(1, $types);
174203 $containerFetcherTypeMapper->replaceArgument(2, $inputTypes);
175204 $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);*/
205+ */
206+
207+ foreach ($ typesNamespaces as $ typesNamespace ) {
208+ $ definition = new Definition (GlobTypeMapper::class);
209+ $ definition ->addArgument ($ typesNamespace );
210+ $ definition ->setAutowired (true );
211+ $ definition ->addTag ('graphql.type_mapper ' );
212+ $ container ->setDefinition ('globTypeMapper_ ' .str_replace ('\\' , '__ ' , $ typesNamespace ), $ definition );
213+ }
214+
182215
183216 // Register custom output types
184217 $ taggedServices = $ container ->findTaggedServiceIds ('graphql.output_type ' );
@@ -218,20 +251,20 @@ public static function createQueryProvider($controller, FieldsBuilderFactory $fi
218251 }
219252
220253 /**
221- * @param object $typeClass
254+ * @param string $typeClass
222255 */
223- public static function createObjectType ($ typeClass , TypeGenerator $ typeGenerator , RecursiveTypeMapperInterface $ recursiveTypeMapper ): ObjectType
256+ /* public static function createObjectType(string $typeClass, TypeGenerator $typeGenerator, RecursiveTypeMapperInterface $recursiveTypeMapper): MutableObjectType
224257 {
225258 return $typeGenerator->mapAnnotatedObject($typeClass, $recursiveTypeMapper);
226- }
259+ }*/
227260
228261 /**
229262 * @param object $factory
230263 */
231- public static function createInputObjectType ($ factory , string $ methodName , InputTypeGenerator $ inputTypeGenerator , RecursiveTypeMapperInterface $ recursiveTypeMapper ): InputObjectType
264+ /* public static function createInputObjectType($factory, string $methodName, InputTypeGenerator $inputTypeGenerator, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
232265 {
233266 return $inputTypeGenerator->mapFactoryMethod($factory, $methodName, $recursiveTypeMapper);
234- }
267+ }*/
235268
236269 /**
237270 * Returns a cached Doctrine annotation reader.
0 commit comments