Skip to content

Commit a9bfd0d

Browse files
authored
Merge pull request #8 from moufmouf/autodetection_refactoring
Refactoring of detection of controllers and types
2 parents ec2e19e + e5aa62f commit a9bfd0d

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

DependencyInjection/GraphqlControllersCompilerPass.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ public function process(ContainerBuilder $container)
6868
$reader = $this->getAnnotationReader();
6969
$inputTypeUtils = new InputTypeUtils($reader, $namingStrategy);
7070

71+
// 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+
75+
foreach ($container->getDefinitions() as $id => $definition) {
76+
if ($definition->isAbstract() || $definition->getClass() === null) {
77+
continue;
78+
}
79+
if (strpos($definition->getClass(), $controllersNamespace) === 0) {
80+
$definition->addTag('graphql.annotated.controller');
81+
}
82+
if (strpos($definition->getClass(), $typesNamespace) === 0) {
83+
$definition->addTag('graphql.annotated.type');
84+
}
85+
}
86+
7187
foreach ($container->findTaggedServiceIds('graphql.annotated.controller') as $id => $tag) {
7288
$definition = $container->findDefinition($id);
7389
$class = $definition->getClass();

DependencyInjection/GraphqlControllersExtension.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@
2121
class GraphqlControllersExtension extends Extension
2222
{
2323

24-
private $projectDir;
25-
26-
public function setProjectDir(string $projectDir): void
27-
{
28-
$this->projectDir = $projectDir;
29-
}
30-
3124
/**
3225
* Loads a specific configuration.
3326
*
@@ -44,27 +37,9 @@ public function load(array $configs, ContainerBuilder $container)
4437
$namespaceController = rtrim($configs[0]['namespace']['controllers'], '\\') . '\\';
4538
$namespaceType = rtrim($configs[0]['namespace']['types'], '\\') . '\\';
4639

47-
$definitionTemplateControllerClass = new Definition();
48-
$definitionTemplateControllerClass->addTag('graphql.annotated.controller');
49-
$definitionTemplateTypeClass = new Definition();
50-
$definitionTemplateTypeClass->addTag('graphql.annotated.type');
51-
52-
if ($this->projectDir === null) {
53-
$this->projectDir = dirname(__DIR__, 4).'/';
54-
}
55-
56-
$controllersDir = $this->projectDir.$this->getNamespaceDir($namespaceController);
57-
$typesDir = $this->projectDir.$this->getNamespaceDir($namespaceType);
58-
59-
if (!is_dir($controllersDir)) {
60-
throw new GraphQLException(sprintf('GraphQL-Controllers bundle expects controllers to be located in the "%s" directory. This directory does not exists.', $controllersDir));
61-
}
62-
if (!is_dir($typesDir)) {
63-
throw new GraphQLException(sprintf('GraphQL-Controllers bundle expects types to be located in the "%s" directory. This directory does not exists.', $typesDir));
64-
}
40+
$container->setParameter('graphql_controllers.namespace.controllers', $namespaceController);
41+
$container->setParameter('graphql_controllers.namespace.types', $namespaceType);
6542

66-
$loader->registerClasses($definitionTemplateControllerClass, $namespaceController, $controllersDir.'/*.php');
67-
$loader->registerClasses($definitionTemplateTypeClass, $namespaceType, $typesDir.'/*.php');
6843
$loader->load('graphql-controllers.xml');
6944

7045
$definition = $container->getDefinition(ServerConfig::class);

Tests/GraphqlControllersTestingKernel.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ class GraphqlControllersTestingKernel extends Kernel
1616

1717
public function registerBundles()
1818
{
19-
$graphQlControllersBundle = new GraphqlControllersBundle();
20-
$graphQlControllersBundle->getContainerExtension()->setProjectDir(__DIR__.'/..');
21-
2219
return [
2320
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
24-
$graphQlControllersBundle,
21+
new GraphqlControllersBundle(),
2522
];
2623
}
2724

@@ -42,8 +39,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
4239

4340
$loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
4441
$loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
45-
//$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
46-
//$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
47-
42+
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
43+
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
4844
}
4945
}

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"doctrine/cache": "^1.8",
2424
"symfony/psr-http-message-bridge": "^1",
2525
"zendframework/zend-diactoros": "^1.8.6",
26-
"overblog/graphiql-bundle": "^0.1.2",
27-
"mouf/classname-mapper": "^1.0"
26+
"overblog/graphiql-bundle": "^0.1.2"
2827
},
2928
"require-dev": {
3029
"symfony/security-bundle": "^4.1.9",

0 commit comments

Comments
 (0)