Skip to content

Commit cd7009b

Browse files
authored
Merge pull request #209 from moufmouf/optimize_controller_instantiations
Optimize controllers instantiations
2 parents 022e0b6 + ff5341e commit cd7009b

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Changelog
44
sidebar_label: Changelog
55
---
66

7-
## 4.0-beta1
7+
## 4.0
88

99
This is a complete refactoring from 3.x. While existing annotations are kept compatible, the internals have completely
1010
changed.

src/GlobControllerQueryProvider.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
use Psr\Container\ContainerInterface;
1010
use Psr\SimpleCache\CacheInterface;
1111
use ReflectionClass;
12+
use ReflectionMethod;
1213
use Symfony\Component\Cache\Adapter\Psr16Adapter;
1314
use Symfony\Contracts\Cache\CacheInterface as CacheContractInterface;
1415
use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer;
16+
use TheCodingMachine\GraphQLite\Annotations\Mutation;
17+
use TheCodingMachine\GraphQLite\Annotations\Query;
1518
use Webmozart\Assert\Assert;
1619
use function class_exists;
1720
use function interface_exists;
@@ -45,13 +48,15 @@ final class GlobControllerQueryProvider implements QueryProviderInterface
4548
private $recursive;
4649
/** @var CacheContractInterface */
4750
private $cacheContract;
51+
/** @var AnnotationReader */
52+
private $annotationReader;
4853

4954
/**
5055
* @param string $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation)
5156
* @param ContainerInterface $container The container we will fetch controllers from.
5257
* @param bool $recursive Whether subnamespaces of $namespace must be analyzed.
5358
*/
54-
public function __construct(string $namespace, FieldsBuilder $fieldsBuilder, ContainerInterface $container, CacheInterface $cache, ?ClassNameMapper $classNameMapper = null, ?int $cacheTtl = null, bool $recursive = true)
59+
public function __construct(string $namespace, FieldsBuilder $fieldsBuilder, ContainerInterface $container, AnnotationReader $annotationReader, CacheInterface $cache, ?ClassNameMapper $classNameMapper = null, ?int $cacheTtl = null, bool $recursive = true)
5560
{
5661
$this->namespace = $namespace;
5762
$this->container = $container;
@@ -61,6 +66,7 @@ public function __construct(string $namespace, FieldsBuilder $fieldsBuilder, Con
6166
$this->cacheTtl = $cacheTtl;
6267
$this->fieldsBuilder = $fieldsBuilder;
6368
$this->recursive = $recursive;
69+
$this->annotationReader = $annotationReader;
6470
}
6571

6672
private function getAggregateControllerQueryProvider(): AggregateControllerQueryProvider
@@ -105,6 +111,9 @@ private function buildInstancesList(): array
105111
if (! $refClass->isInstantiable()) {
106112
continue;
107113
}
114+
if (! $this->hasQueriesOrMutations($refClass)) {
115+
continue;
116+
}
108117
if (! $this->container->has($className)) {
109118
continue;
110119
}
@@ -115,6 +124,24 @@ private function buildInstancesList(): array
115124
return $instances;
116125
}
117126

127+
/**
128+
* @param ReflectionClass<object> $reflectionClass
129+
*/
130+
private function hasQueriesOrMutations(ReflectionClass $reflectionClass): bool
131+
{
132+
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $refMethod) {
133+
$queryAnnotation = $this->annotationReader->getRequestAnnotation($refMethod, Query::class);
134+
if ($queryAnnotation !== null) {
135+
return true;
136+
}
137+
$mutationAnnotation = $this->annotationReader->getRequestAnnotation($refMethod, Mutation::class);
138+
if ($mutationAnnotation !== null) {
139+
return true;
140+
}
141+
}
142+
return false;
143+
}
144+
118145
/**
119146
* @return FieldDefinition[]
120147
*/

src/SchemaFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ public function createSchema(): Schema
435435
$controllerNamespace,
436436
$fieldsBuilder,
437437
$this->container,
438+
$annotationReader,
438439
$this->cache,
439440
$this->classNameMapper,
440441
$this->globTtl

tests/Fixtures/TestController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
class TestController
1515
{
16+
/**
17+
* @Mutation
18+
* @param TestObject $testObject
19+
* @return TestObject
20+
*/
21+
public function mutation(TestObject $testObject): TestObject
22+
{
23+
return $testObject;
24+
}
25+
1626
/**
1727
* @Query
1828
* @param int $int
@@ -39,16 +49,6 @@ public function test(int $int, array $list, ?bool $boolean, ?float $float, ?\Dat
3949
return new TestObject($string.$int.$str.($boolean?'true':'false').$float.$dateTimeImmutable->format('YmdHis').$dateTime->format('YmdHis').$withDefault.($id !== null ? $id->val() : '').$enum->getValue());
4050
}
4151

42-
/**
43-
* @Mutation
44-
* @param TestObject $testObject
45-
* @return TestObject
46-
*/
47-
public function mutation(TestObject $testObject): TestObject
48-
{
49-
return $testObject;
50-
}
51-
5252
/**
5353
* @Query
5454
* @Logged

tests/GlobControllerQueryProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function has($id)
3636
}
3737
};
3838

39-
$globControllerQueryProvider = new GlobControllerQueryProvider('TheCodingMachine\\GraphQLite\\Fixtures', $this->getFieldsBuilder(), $container, new Psr16Cache(new NullAdapter()), null, false);
39+
$globControllerQueryProvider = new GlobControllerQueryProvider('TheCodingMachine\\GraphQLite\\Fixtures', $this->getFieldsBuilder(), $container, $this->getAnnotationReader(), new Psr16Cache(new NullAdapter()), null, false, false);
4040

4141
$queries = $globControllerQueryProvider->getQueries();
4242
$this->assertCount(7, $queries);

tests/Integration/EndToEndTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function createContainer(array $overloadedServices = []): ContainerInterf
9090
},
9191
QueryProviderInterface::class => function(ContainerInterface $container) {
9292
return new GlobControllerQueryProvider('TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Controllers', $container->get(FieldsBuilder::class),
93-
$container->get(BasicAutoWiringContainer::class), new Psr16Cache(new ArrayAdapter()));
93+
$container->get(BasicAutoWiringContainer::class), $container->get(AnnotationReader::class), new Psr16Cache(new ArrayAdapter()));
9494
},
9595
FieldsBuilder::class => function(ContainerInterface $container) {
9696
return new FieldsBuilder(

0 commit comments

Comments
 (0)