Skip to content

Commit 3af1a4a

Browse files
authored
Merge pull request #845 from murtukov/refactoring/decouple_type_generator_props
Refactor `TypeGenerator`
2 parents 5b7ed69 + aee74dd commit 3af1a4a

File tree

9 files changed

+153
-127
lines changed

9 files changed

+153
-127
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+
array $typeConfigs,
52+
TypeBuilder $typeBuilder,
53+
EventDispatcherInterface $eventDispatcher,
54+
TypeGeneratorOptions $options
55+
)
56+
```
57+
`TypeBuilder` here is a new service `Overblog\GraphQLBundle\Generator\TypeBuilder`, which is also used internally.
58+
The rest of the arguments were moved into the separate class `Overblog\GraphQLBundle\Generator\TypeGeneratorOptions`
59+
with the following constructor signature:
60+
61+
```php
62+
public function __construct(
63+
string $namespace,
5364
?string $cacheDir,
54-
array $configs,
55-
+ TypeBuilder $typeBuilder
56-
+ EventDispatcherInterface $eventDispatcher
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ public function isOptional()
2727
}
2828

2929
/**
30-
* {@inheritdoc}
31-
*
3230
* @param string $cacheDir
3331
*
3432
* @return string[]
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+
$cacheBaseDir = $this->typeGenerator->getCacheBaseDir();
39+
40+
if (null === $this->typeGenerator->getCacheDir()) {
41+
$this->typeGenerator->setCacheBaseDir($cacheDir);
4342
}
43+
4444
$this->typeGenerator->compile(TypeGenerator::MODE_WRITE | TypeGenerator::MODE_OVERRIDE);
4545

46-
if (null !== $baseCacheDir) {
47-
$this->typeGenerator->setBaseCacheDir($baseCacheDir);
46+
if (null !== $cacheBaseDir) {
47+
$this->typeGenerator->setCacheBaseDir($cacheBaseDir);
4848
}
4949
}
5050

src/Generator/TypeGenerator.php

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,29 @@ 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+
private array $typeConfigs;
31+
private TypeGeneratorOptions $options;
3732
private TypeBuilder $typeBuilder;
38-
private EventDispatcherInterface $eventDispatcher;
33+
private EventDispatcherInterface $dispatcher;
3934

4035
public function __construct(
41-
string $classNamespace,
42-
?string $cacheDir,
43-
array $configs,
36+
array $typeConfigs,
4437
TypeBuilder $typeBuilder,
45-
EventDispatcherInterface $eventDispatcher,
46-
bool $useClassMap = true,
47-
?string $baseCacheDir = null,
48-
?int $cacheDirMask = null
38+
EventDispatcherInterface $dispatcher,
39+
TypeGeneratorOptions $options
4940
) {
50-
$this->cacheDir = $cacheDir;
51-
$this->configs = $configs;
52-
$this->useClassMap = $useClassMap;
53-
$this->baseCacheDir = $baseCacheDir;
41+
$this->typeConfigs = $typeConfigs;
5442
$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-
}
65-
66-
public function getBaseCacheDir(): ?string
67-
{
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;
43+
$this->dispatcher = $dispatcher;
44+
$this->options = $options;
9045
}
9146

9247
public function compile(int $mode): array
9348
{
94-
$cacheDir = $this->getCacheDir();
49+
$cacheDir = $this->getCacheDirOrDefault();
9550
$writeMode = $mode & self::MODE_WRITE;
9651

9752
// Configure write mode
@@ -100,20 +55,17 @@ public function compile(int $mode): array
10055
$fs->remove($cacheDir);
10156
}
10257

103-
// Process configs
104-
$configs = Processor::process($this->configs);
105-
10658
// Generate classes
10759
$classes = [];
108-
foreach ($configs as $name => $config) {
60+
foreach (Processor::process($this->typeConfigs) as $name => $config) {
10961
$config['config']['name'] ??= $name;
11062
$config['config']['class_name'] = $config['class_name'];
11163
$classMap = $this->generateClass($config, $cacheDir, $mode);
11264
$classes = array_merge($classes, $classMap);
11365
}
11466

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

11971
// replaced hard-coded absolute paths by __DIR__
@@ -125,7 +77,7 @@ public function compile(int $mode): array
12577
$this->loadClasses(true);
12678
}
12779

128-
$this->eventDispatcher->dispatch(new SchemaCompiledEvent());
80+
$this->dispatcher->dispatch(new SchemaCompiledEvent());
12981

13082
return $classes;
13183
}
@@ -145,12 +97,14 @@ public function generateClass(array $config, ?string $outputDirectory, int $mode
14597
}
14698
}
14799

148-
return ["$this->classNamespace\\$className" => $path];
100+
$namespace = $this->options->namespace;
101+
102+
return ["$namespace\\$className" => $path];
149103
}
150104

151105
public function loadClasses(bool $forceReload = false): void
152106
{
153-
if ($this->useClassMap && (!self::$classMapLoaded || $forceReload)) {
107+
if ($this->options->useClassMap && (!self::$classMapLoaded || $forceReload)) {
154108
$classMapFile = $this->getClassesMap();
155109
$classes = file_exists($classMapFile) ? require $classMapFile : [];
156110

@@ -171,8 +125,33 @@ public function loadClasses(bool $forceReload = false): void
171125
}
172126
}
173127

128+
public function getCacheDir(): ?string
129+
{
130+
return $this->options->cacheDir;
131+
}
132+
133+
public function getCacheDirMask(): int
134+
{
135+
return $this->options->cacheDirMask;
136+
}
137+
138+
public function getCacheBaseDir(): ?string
139+
{
140+
return $this->options->cacheBaseDir;
141+
}
142+
143+
public function setCacheBaseDir(string $dir): void
144+
{
145+
$this->options->cacheBaseDir = $dir;
146+
}
147+
148+
public function getCacheDirOrDefault(): string
149+
{
150+
return $this->options->cacheDir ?? $this->options->cacheBaseDir.'/overblog/graphql-bundle/__definitions__';
151+
}
152+
174153
private function getClassesMap(): string
175154
{
176-
return $this->getCacheDir().'/__classes.map';
155+
return $this->getCacheDirOrDefault().'/__classes.map';
177156
}
178157
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator;
6+
7+
class TypeGeneratorOptions
8+
{
9+
/**
10+
* PSR-4 namespace for generated GraphQL classes.
11+
*/
12+
public string $namespace;
13+
14+
/**
15+
* Relative path to a directory for generated GraphQL classes.
16+
* Equals `null` unless explicitly set by user.
17+
*/
18+
public ?string $cacheDir;
19+
20+
/**
21+
* Permission bitmask for the directory of generated classes.
22+
*/
23+
public int $cacheDirMask;
24+
25+
/**
26+
* Whether a class map should be generated.
27+
*/
28+
public bool $useClassMap = true;
29+
30+
/**
31+
* Base directory for generated classes.
32+
*/
33+
public ?string $cacheBaseDir;
34+
35+
public function __construct(
36+
string $namespace,
37+
?string $cacheDir,
38+
bool $useClassMap = true,
39+
?string $cacheBaseDir = null,
40+
?int $cacheDirMask = null
41+
) {
42+
$this->namespace = $namespace;
43+
$this->cacheDir = $cacheDir;
44+
$this->useClassMap = $useClassMap;
45+
$this->cacheBaseDir = $cacheBaseDir;
46+
47+
if (null === $cacheDirMask) {
48+
// Apply permission 0777 for default cache dir otherwise apply 0775.
49+
$cacheDirMask = null === $cacheDir ? 0777 : 0775;
50+
}
51+
52+
$this->cacheDirMask = $cacheDirMask;
53+
}
54+
}

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%"
65+
- '%overblog_graphql_types.config%'
6866
- '@Overblog\GraphQLBundle\Generator\TypeBuilder'
6967
- '@Symfony\Contracts\EventDispatcher\EventDispatcherInterface'
70-
- "%overblog_graphql.use_classloader_listener%"
71-
- "%kernel.cache_dir%"
72-
- "%overblog_graphql.cache_dir_permissions%"
68+
- !service
69+
class: Overblog\GraphQLBundle\Generator\TypeGeneratorOptions
70+
arguments:
71+
- '%overblog_graphql.class_namespace%'
72+
- '%overblog_graphql.cache_dir%'
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->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
}

0 commit comments

Comments
 (0)