Skip to content

Commit 1381982

Browse files
committed
Refactor TypeGenerator
* Move generator options into a separate class * Update UPGRADE.md
1 parent 5b7ed69 commit 1381982

File tree

9 files changed

+117
-130
lines changed

9 files changed

+117
-130
lines changed

UPGRADE.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,30 @@ $connectionBuilder = new ConnectionBuilder(
4343

4444
### Change arguments of `TypeGenerator` class
4545

46-
The `Overblog\GraphQLBundle\Generator\TypeGenerator` service is used internally for GraphQL types compilation. If you
46+
The `Overblog\GraphQLBundle\Generator\TypeGenerator` service is used internally for compilation of GraphQL types. If you
4747
overrode the service definition, please take into account the new constructor signature:
4848

49-
```diff
49+
```php
5050
public function __construct(
51-
string $classNamespace,
52-
- array $skeletonDirs,
51+
TypeBuilder $typeBuilder,
52+
EventDispatcherInterface $eventDispatcher,
53+
TypeGeneratorOptions $options
54+
)
55+
```
56+
`TypeBuilder` here is a new service `Overblog\GraphQLBundle\Generator\TypeBuilder`, which is also used internally.
57+
The rest of the arguments were moved into the separate class `Overblog\GraphQLBundle\Generator\TypeGeneratorOptions`
58+
with the following constructor signature:
59+
60+
```php
61+
public function __construct(
62+
string $namespace,
5363
?string $cacheDir,
54-
array $configs,
55-
+ TypeBuilder $typeBuilder
56-
+ EventDispatcherInterface $eventDispatcher
64+
array $types,
5765
bool $useClassMap = true,
58-
- callable $configProcessor = null,
59-
?string $baseCacheDir = null,
66+
?string $cacheBaseDir = null,
6067
?int $cacheDirMask = null
61-
) {
68+
)
6269
```
63-
`TypeBuilder` here is a new service `Overblog\GraphQLBundle\Generator\TypeBuilder`, which is also used internally.
64-
6570
### Add magic `__get` method to `ArgumentInterface` implementors
6671

6772
The interface `Overblog\GraphQLBundle\Definition\ArgumentInterface` as well as implementing it class

src/CacheWarmer/CompileCacheWarmer.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,33 @@ public function __construct(TypeGenerator $typeGenerator, bool $compiled = true)
1919
}
2020

2121
/**
22-
* {@inheritdoc}
22+
* @return bool
2323
*/
2424
public function isOptional()
2525
{
2626
return !$this->compiled;
2727
}
2828

2929
/**
30-
* {@inheritdoc}
31-
*
3230
* @param string $cacheDir
3331
*
34-
* @return string[]
32+
* @return array
3533
*/
3634
public function warmUp($cacheDir)
3735
{
3836
if ($this->compiled) {
39-
// use warm up cache dir if type generator cache dir not already explicitly declare
40-
$baseCacheDir = $this->typeGenerator->getBaseCacheDir();
41-
if (null === $this->typeGenerator->getCacheDir(false)) {
42-
$this->typeGenerator->setBaseCacheDir($cacheDir);
37+
// use warm up cache dir if type generator cache dir not already explicitly declared
38+
$options = $this->typeGenerator->options;
39+
$cacheBaseDir = $options->cacheBaseDir;
40+
41+
if (null === $options->cacheDir) {
42+
$options->cacheBaseDir = $cacheDir;
4343
}
44+
4445
$this->typeGenerator->compile(TypeGenerator::MODE_WRITE | TypeGenerator::MODE_OVERRIDE);
4546

46-
if (null !== $baseCacheDir) {
47-
$this->typeGenerator->setBaseCacheDir($baseCacheDir);
47+
if (null !== $cacheBaseDir) {
48+
$options->cacheBaseDir = $cacheBaseDir;
4849
}
4950
}
5051

src/Generator/TypeGenerator.php

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,23 @@ class TypeGenerator
2424
public const MODE_MAPPING_ONLY = 2;
2525
public const MODE_WRITE = 4;
2626
public const MODE_OVERRIDE = 8;
27-
2827
public const GRAPHQL_SERVICES = 'services';
2928

3029
private static bool $classMapLoaded = false;
31-
private ?string $cacheDir;
32-
protected int $cacheDirMask;
33-
private array $configs;
34-
private bool $useClassMap;
35-
private ?string $baseCacheDir;
36-
private string $classNamespace;
30+
public TypeGeneratorOptions $options;
3731
private TypeBuilder $typeBuilder;
38-
private EventDispatcherInterface $eventDispatcher;
39-
40-
public function __construct(
41-
string $classNamespace,
42-
?string $cacheDir,
43-
array $configs,
44-
TypeBuilder $typeBuilder,
45-
EventDispatcherInterface $eventDispatcher,
46-
bool $useClassMap = true,
47-
?string $baseCacheDir = null,
48-
?int $cacheDirMask = null
49-
) {
50-
$this->cacheDir = $cacheDir;
51-
$this->configs = $configs;
52-
$this->useClassMap = $useClassMap;
53-
$this->baseCacheDir = $baseCacheDir;
54-
$this->typeBuilder = $typeBuilder;
55-
$this->eventDispatcher = $eventDispatcher;
56-
$this->classNamespace = $classNamespace;
57-
58-
if (null === $cacheDirMask) {
59-
// Apply permission 0777 for default cache dir otherwise apply 0775.
60-
$cacheDirMask = null === $cacheDir ? 0777 : 0775;
61-
}
62-
63-
$this->cacheDirMask = $cacheDirMask;
64-
}
32+
private EventDispatcherInterface $dispatcher;
6533

66-
public function getBaseCacheDir(): ?string
34+
public function __construct(TypeBuilder $typeBuilder, EventDispatcherInterface $dispatcher, TypeGeneratorOptions $options)
6735
{
68-
return $this->baseCacheDir;
69-
}
70-
71-
public function setBaseCacheDir(string $baseCacheDir): void
72-
{
73-
$this->baseCacheDir = $baseCacheDir;
74-
}
75-
76-
public function getCacheDir(bool $useDefault = true): ?string
77-
{
78-
if ($useDefault) {
79-
return $this->cacheDir ?: $this->baseCacheDir.'/overblog/graphql-bundle/__definitions__';
80-
} else {
81-
return $this->cacheDir;
82-
}
83-
}
84-
85-
public function setCacheDir(?string $cacheDir): self
86-
{
87-
$this->cacheDir = $cacheDir;
88-
89-
return $this;
36+
$this->typeBuilder = $typeBuilder;
37+
$this->dispatcher = $dispatcher;
38+
$this->options = $options;
9039
}
9140

9241
public function compile(int $mode): array
9342
{
94-
$cacheDir = $this->getCacheDir();
43+
$cacheDir = $this->options->getCacheDirOrDefault();
9544
$writeMode = $mode & self::MODE_WRITE;
9645

9746
// Configure write mode
@@ -101,19 +50,19 @@ public function compile(int $mode): array
10150
}
10251

10352
// Process configs
104-
$configs = Processor::process($this->configs);
53+
$types = Processor::process($this->options->types);
10554

10655
// Generate classes
10756
$classes = [];
108-
foreach ($configs as $name => $config) {
57+
foreach ($types as $name => $config) {
10958
$config['config']['name'] ??= $name;
11059
$config['config']['class_name'] = $config['class_name'];
11160
$classMap = $this->generateClass($config, $cacheDir, $mode);
11261
$classes = array_merge($classes, $classMap);
11362
}
11463

11564
// Create class map file
116-
if ($writeMode && $this->useClassMap && count($classes) > 0) {
65+
if ($writeMode && $this->options->useClassMap && count($classes) > 0) {
11766
$content = "<?php\nreturn ".var_export($classes, true).';';
11867

11968
// replaced hard-coded absolute paths by __DIR__
@@ -125,7 +74,7 @@ public function compile(int $mode): array
12574
$this->loadClasses(true);
12675
}
12776

128-
$this->eventDispatcher->dispatch(new SchemaCompiledEvent());
77+
$this->dispatcher->dispatch(new SchemaCompiledEvent());
12978

13079
return $classes;
13180
}
@@ -145,12 +94,14 @@ public function generateClass(array $config, ?string $outputDirectory, int $mode
14594
}
14695
}
14796

148-
return ["$this->classNamespace\\$className" => $path];
97+
$namespace = $this->options->namespace;
98+
99+
return ["$namespace\\$className" => $path];
149100
}
150101

151102
public function loadClasses(bool $forceReload = false): void
152103
{
153-
if ($this->useClassMap && (!self::$classMapLoaded || $forceReload)) {
104+
if ($this->options->useClassMap && (!self::$classMapLoaded || $forceReload)) {
154105
$classMapFile = $this->getClassesMap();
155106
$classes = file_exists($classMapFile) ? require $classMapFile : [];
156107

@@ -173,6 +124,6 @@ public function loadClasses(bool $forceReload = false): void
173124

174125
private function getClassesMap(): string
175126
{
176-
return $this->getCacheDir().'/__classes.map';
127+
return $this->options->getCacheDirOrDefault().'/__classes.map';
177128
}
178129
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator;
6+
7+
class TypeGeneratorOptions
8+
{
9+
public string $namespace;
10+
public ?string $cacheDir;
11+
public array $types;
12+
public int $cacheDirMask;
13+
public bool $useClassMap = true;
14+
public ?string $cacheBaseDir;
15+
16+
public function __construct(
17+
string $namespace,
18+
?string $cacheDir,
19+
array $types,
20+
bool $useClassMap = true,
21+
?string $cacheBaseDir = null,
22+
?int $cacheDirMask = null
23+
) {
24+
$this->namespace = $namespace;
25+
$this->cacheDir = $cacheDir;
26+
$this->types = $types;
27+
$this->useClassMap = $useClassMap;
28+
$this->cacheBaseDir = $cacheBaseDir;
29+
30+
if (null === $cacheDirMask) {
31+
// Apply permission 0777 for default cache dir otherwise apply 0775.
32+
$cacheDirMask = null === $cacheDir ? 0777 : 0775;
33+
}
34+
35+
$this->cacheDirMask = $cacheDirMask;
36+
}
37+
38+
public function getCacheDirOrDefault(): string
39+
{
40+
return $this->cacheDir ?? $this->cacheBaseDir.'/overblog/graphql-bundle/__definitions__';
41+
}
42+
}

src/Resources/config/services.yaml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ services:
3535

3636
Overblog\GraphQLBundle\Resolver\TypeResolver:
3737
calls:
38-
- ["setDispatcher", ["@event_dispatcher"]]
38+
- ['setDispatcher', ['@event_dispatcher']]
3939
tags:
4040
- { name: overblog_graphql.service, alias: typeResolver }
4141

4242
Overblog\GraphQLBundle\Transformer\ArgumentsTransformer:
4343
arguments:
44-
- "@?validator"
45-
- "%overblog_graphql_types.classes_map%"
44+
- '@?validator'
45+
- '%overblog_graphql_types.classes_map%'
4646

4747
Overblog\GraphQLBundle\Resolver\QueryResolver:
4848
tags:
@@ -54,22 +54,25 @@ services:
5454

5555
Overblog\GraphQLBundle\Resolver\AccessResolver:
5656
arguments:
57-
- "@overblog_graphql.promise_adapter"
57+
- '@overblog_graphql.promise_adapter'
5858

5959
Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage:
6060
arguments:
61-
- "@?overblog_graphql.cache_expression_language_parser"
61+
- '@?overblog_graphql.cache_expression_language_parser'
6262

6363
Overblog\GraphQLBundle\Generator\TypeGenerator:
6464
arguments:
65-
- "%overblog_graphql.class_namespace%"
66-
- "%overblog_graphql.cache_dir%"
67-
- "%overblog_graphql_types.config%"
6865
- '@Overblog\GraphQLBundle\Generator\TypeBuilder'
6966
- '@Symfony\Contracts\EventDispatcher\EventDispatcherInterface'
70-
- "%overblog_graphql.use_classloader_listener%"
71-
- "%kernel.cache_dir%"
72-
- "%overblog_graphql.cache_dir_permissions%"
67+
- !service
68+
class: Overblog\GraphQLBundle\Generator\TypeGeneratorOptions
69+
arguments:
70+
- '%overblog_graphql.class_namespace%'
71+
- '%overblog_graphql.cache_dir%'
72+
- '%overblog_graphql_types.config%'
73+
- '%overblog_graphql.use_classloader_listener%'
74+
- '%kernel.cache_dir%'
75+
- '%overblog_graphql.cache_dir_permissions%'
7376

7477
Overblog\GraphQLBundle\Definition\ArgumentFactory:
7578
arguments:

tests/ExpressionLanguage/ExpressionFunction/Security/IsGrantedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security\IsGranted;
8+
use Overblog\GraphQLBundle\Generator\TypeGenerator;
89
use Overblog\GraphQLBundle\Tests\ExpressionLanguage\TestCase;
9-
use Overblog\GraphQLBundle\Tests\Generator\TypeGenerator;
1010

1111
class IsGrantedTest extends TestCase
1212
{

tests/Functional/Command/CompileCommandTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public function setUp(): void
3131
$this->typesMapping = static::$kernel->getContainer()->get('overblog_graphql.cache_compiler')
3232
->compile(TypeGenerator::MODE_MAPPING_ONLY);
3333

34-
// @phpstan-ignore-next-line
35-
$this->cacheDir = static::$kernel->getContainer()->get('overblog_graphql.cache_compiler')->getCacheDir();
34+
/** @var TypeGenerator $generator */
35+
$generator = static::$kernel->getContainer()->get('overblog_graphql.cache_compiler');
36+
$this->cacheDir = $generator->options->getCacheDirOrDefault();
3637
$this->commandTester = new CommandTester($command);
3738
}
3839

@@ -48,7 +49,7 @@ public function testGeneration(): void
4849
$this->commandTester->execute([]);
4950
$this->assertSame(0, $this->commandTester->getStatusCode());
5051
$this->assertSame($this->displayExpected(), $this->commandTester->getDisplay());
51-
foreach ($this->typesMapping as $class => $path) {
52+
foreach ($this->typesMapping as $path) {
5253
$this->assertFileExists($path);
5354
}
5455
}

tests/Generator/TypeGenerator.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)