Skip to content

Commit be24aff

Browse files
committed
Setting globTtl to 2 seconds only in dev env
The globTtl setting (that commands when to do file scans for changed files) is now set to 2 seconds for the "dev" environment and set to null (i.e. cache forever) for other environments. This should result in a performance boost for production machines (especially if they receive less than 1 request every 2 seconds).
1 parent cd9167f commit be24aff

File tree

1 file changed

+9
-95
lines changed

1 file changed

+9
-95
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use TheCodingMachine\GraphQLite\TypeGenerator;
3636
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
3737
use TheCodingMachine\GraphQLite\Types\ResolvableInputObjectType;
38+
use function var_dump;
3839

3940
/**
4041
* Detects controllers and types automatically and tag them.
@@ -51,29 +52,20 @@ class GraphqliteCompilerPass implements CompilerPassInterface
5152
*/
5253
public function process(ContainerBuilder $container)
5354
{
54-
/**
55-
* @var array<string, string> An array matching class name to the the container identifier of a factory creating a type.
56-
*/
57-
$types = [];
58-
59-
/**
60-
* @var array<string, string> An array matching class name to the the container identifier of a factory creating an input type.
61-
*/
62-
$inputTypes = [];
63-
64-
/**
65-
* @var array<string, string> An array matching a Graphql type name to the the container identifier of a factory creating a type.
66-
*/
67-
$typesByName = [];
68-
69-
$namingStrategy = new NamingStrategy();
7055
$reader = $this->getAnnotationReader();
7156
//$inputTypeUtils = new InputTypeUtils($reader, $namingStrategy);
7257

7358
// Let's scan the whole container and tag the services that belong to the namespace we want to inspect.
7459
$controllersNamespaces = $container->getParameter('graphqlite.namespace.controllers');
7560
$typesNamespaces = $container->getParameter('graphqlite.namespace.types');
7661

62+
// 2 seconds of TTL in environment mode. Otherwise, let's cache forever!
63+
$env = $container->getParameter('kernel.environment');
64+
$globTtl = null;
65+
if ($env === 'dev') {
66+
$globTtl = 2;
67+
}
68+
7769
foreach ($container->getDefinitions() as $id => $definition) {
7870
if ($definition->isAbstract() || $definition->getClass() === null) {
7971
continue;
@@ -144,73 +136,11 @@ public function process(ContainerBuilder $container)
144136
$container->setDefinition($controllerIdentifier, $queryProvider);
145137
}
146138
}
147-
/*
148-
foreach ($container->findTaggedServiceIds('graphql.annotated.type') as $id => $tag) {
149-
$used = false;
150-
$definition = $container->findDefinition($id);
151-
$class = $definition->getClass();
152-
if ($class === null) {
153-
throw new \RuntimeException(sprintf('Service %s has no class defined.', $id));
154-
}
155-
156-
$reflectionClass = new ReflectionClass($class);
157-
foreach ($reflectionClass->getMethods() as $method) {
158-
$factory = $reader->getFactoryAnnotation($method);
159-
if ($factory !== null) {
160-
$objectTypeIdentifier = $class.'__'.$method->getName().'__InputType';
161-
162-
$objectType = new Definition(ResolvableInputObjectType::class);
163-
$objectType->setPrivate(false);
164-
$objectType->setFactory([self::class, 'createInputObjectType']);
165-
$objectType->addArgument(new Reference($id));
166-
$objectType->addArgument($method->getName());
167-
$objectType->addArgument(new Reference(InputTypeGenerator::class));
168-
$objectType->addArgument(new Reference(RecursiveTypeMapperInterface::class));
169-
$container->setDefinition($objectTypeIdentifier, $objectType);
170-
171-
[$inputName, $inputClassName] = $inputTypeUtils->getInputTypeNameAndClassName($method);
172-
173-
$inputTypes[$inputClassName] = $objectTypeIdentifier;
174-
$typesByName[$inputName] = $objectTypeIdentifier;
175-
176-
$used = true;
177-
}
178-
}
179-
180-
$typeAnnotation = $this->annotationReader->getTypeAnnotation($reflectionClass);
181-
if ($typeAnnotation !== null) {
182-
$objectTypeIdentifier = $class.'__Type';
183-
184-
$objectType = new Definition(ObjectType::class);
185-
$objectType->setPrivate(false);
186-
$objectType->setFactory([self::class, 'createObjectType']);
187-
$objectType->addArgument($id);
188-
$objectType->addArgument(new Reference(TypeGenerator::class));
189-
$objectType->addArgument(new Reference(RecursiveTypeMapperInterface::class));
190-
$container->setDefinition($objectTypeIdentifier, $objectType);
191-
192-
$types[$typeAnnotation->getClass()] = $objectTypeIdentifier;
193-
$typesByName[$namingStrategy->getOutputTypeName($class, $typeAnnotation)] = $objectTypeIdentifier;
194-
//$definition->addTag('graphql.annotated_type');
195-
196-
$used = true;
197-
}
198-
199-
// If the service is used for GraphQL, since it is referenced by service name in the factories, let's make it public
200-
if ($used) {
201-
$container->findDefinition($id)->setPublic(true);
202-
}
203-
}
204-
205-
$containerFetcherTypeMapper = $container->getDefinition(ContainerFetcherTypeMapper::class);
206-
$containerFetcherTypeMapper->replaceArgument(1, $types);
207-
$containerFetcherTypeMapper->replaceArgument(2, $inputTypes);
208-
$containerFetcherTypeMapper->replaceArgument(3, $typesByName);
209-
*/
210139

211140
foreach ($typesNamespaces as $typesNamespace) {
212141
$definition = new Definition(GlobTypeMapper::class);
213142
$definition->addArgument($typesNamespace);
143+
$definition->setArgument('$globTtl', $globTtl);
214144
$definition->setAutowired(true);
215145
$definition->addTag('graphql.type_mapper');
216146
$container->setDefinition('globTypeMapper_'.str_replace('\\', '__', $typesNamespace), $definition);
@@ -254,22 +184,6 @@ public static function createQueryProvider($controller, FieldsBuilderFactory $fi
254184
return new ControllerQueryProvider($controller, $fieldsBuilderFactory->buildFieldsBuilder($recursiveTypeMapper));
255185
}
256186

257-
/**
258-
* @param string $typeClass
259-
*/
260-
/*public static function createObjectType(string $typeClass, TypeGenerator $typeGenerator, RecursiveTypeMapperInterface $recursiveTypeMapper): MutableObjectType
261-
{
262-
return $typeGenerator->mapAnnotatedObject($typeClass, $recursiveTypeMapper);
263-
}*/
264-
265-
/**
266-
* @param object $factory
267-
*/
268-
/*public static function createInputObjectType($factory, string $methodName, InputTypeGenerator $inputTypeGenerator, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
269-
{
270-
return $inputTypeGenerator->mapFactoryMethod($factory, $methodName, $recursiveTypeMapper);
271-
}*/
272-
273187
/**
274188
* Returns a cached Doctrine annotation reader.
275189
* Note: we cannot get the annotation reader service in the container as we are in a compiler pass.

0 commit comments

Comments
 (0)