Skip to content

Commit 7bec07f

Browse files
ruudkJeremiah VALERIE
authored andcommitted
Do not use container in compiler passes
By manually constructing the `TypeGenerator` in `TypeGeneratorPass` we gain performance benefits because we don't have the boot the whole container while we are in fact compiling the container. The container should not be used in compiler passes: > As a rule, only work with services definition in a compiler pass and do not create service instances. In practice, this means using the methods has(), findDefinition(), getDefinition(), setDefinition(), etc. instead of get(), set(), etc. Fixes 899
1 parent 1c0f579 commit 7bec07f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/DependencyInjection/Compiler/TypeGeneratorPass.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
use Exception;
88
use Overblog\GraphQLBundle\Definition\Builder\TypeFactory;
9+
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage;
10+
use Overblog\GraphQLBundle\Generator\Converter\ExpressionConverter;
11+
use Overblog\GraphQLBundle\Generator\TypeBuilder;
912
use Overblog\GraphQLBundle\Generator\TypeGenerator;
13+
use Overblog\GraphQLBundle\Generator\TypeGeneratorOptions;
1014
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1115
use Symfony\Component\DependencyInjection\ContainerBuilder;
1216
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\EventDispatcher\EventDispatcher;
1318
use function preg_replace;
1419
use function strrchr;
1520
use function substr;
@@ -21,12 +26,30 @@ class TypeGeneratorPass implements CompilerPassInterface
2126
*/
2227
public function process(ContainerBuilder $container): void
2328
{
29+
// We construct the TypeGenerator manually so that we don't have to boot the container
30+
// while we are in compilation phase.
31+
// See https://github.com/overblog/GraphQLBundle/issues/899
32+
$typeGenerator = new TypeGenerator(
33+
$container->getParameter('overblog_graphql_types.config'),
34+
new TypeBuilder(
35+
new ExpressionConverter(new ExpressionLanguage()),
36+
$container->getParameter('overblog_graphql.class_namespace')
37+
),
38+
new EventDispatcher(),
39+
new TypeGeneratorOptions(
40+
$container->getParameter('overblog_graphql.class_namespace'),
41+
$container->getParameter('overblog_graphql.cache_dir'),
42+
$container->getParameter('overblog_graphql.use_classloader_listener'),
43+
$container->getParameter('kernel.cache_dir'),
44+
$container->getParameter('overblog_graphql.cache_dir_permissions'),
45+
)
46+
);
47+
2448
/**
2549
* @var array<class-string, string> $generatedClasses
2650
* @phpstan-ignore-next-line
2751
*/
28-
$generatedClasses = $container->get('overblog_graphql.cache_compiler')
29-
->compile(TypeGenerator::MODE_MAPPING_ONLY);
52+
$generatedClasses = $typeGenerator->compile(TypeGenerator::MODE_MAPPING_ONLY);
3053

3154
foreach ($generatedClasses as $class => $file) {
3255
$portion = strrchr($class, '\\');

0 commit comments

Comments
 (0)