Skip to content

Commit

Permalink
Do not use container in compiler passes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ruudk authored and mcg-web committed Jul 5, 2022
1 parent 1c0f579 commit 7bec07f
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/DependencyInjection/Compiler/TypeGeneratorPass.php
Expand Up @@ -6,10 +6,15 @@

use Exception;
use Overblog\GraphQLBundle\Definition\Builder\TypeFactory;
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage;
use Overblog\GraphQLBundle\Generator\Converter\ExpressionConverter;
use Overblog\GraphQLBundle\Generator\TypeBuilder;
use Overblog\GraphQLBundle\Generator\TypeGenerator;
use Overblog\GraphQLBundle\Generator\TypeGeneratorOptions;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcher;
use function preg_replace;
use function strrchr;
use function substr;
Expand All @@ -21,12 +26,30 @@ class TypeGeneratorPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container): void
{
// We construct the TypeGenerator manually so that we don't have to boot the container
// while we are in compilation phase.
// See https://github.com/overblog/GraphQLBundle/issues/899
$typeGenerator = new TypeGenerator(
$container->getParameter('overblog_graphql_types.config'),
new TypeBuilder(
new ExpressionConverter(new ExpressionLanguage()),
$container->getParameter('overblog_graphql.class_namespace')
),
new EventDispatcher(),
new TypeGeneratorOptions(
$container->getParameter('overblog_graphql.class_namespace'),
$container->getParameter('overblog_graphql.cache_dir'),
$container->getParameter('overblog_graphql.use_classloader_listener'),
$container->getParameter('kernel.cache_dir'),
$container->getParameter('overblog_graphql.cache_dir_permissions'),
)
);

/**
* @var array<class-string, string> $generatedClasses
* @phpstan-ignore-next-line
*/
$generatedClasses = $container->get('overblog_graphql.cache_compiler')
->compile(TypeGenerator::MODE_MAPPING_ONLY);
$generatedClasses = $typeGenerator->compile(TypeGenerator::MODE_MAPPING_ONLY);

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

0 comments on commit 7bec07f

Please sign in to comment.