Skip to content

Commit 38b706e

Browse files
committed
Make TypesConfiguration and InheritanceProcessor extendable (dirty quick solution)
1 parent 624fa15 commit 38b706e

File tree

11 files changed

+165
-20
lines changed

11 files changed

+165
-20
lines changed

src/Config/CustomScalarTypeDefinition.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88

99
class CustomScalarTypeDefinition extends TypeDefinition
1010
{
11+
public const CONFIG_NAME = '_custom_scalar_config';
12+
13+
public static function getName(): string
14+
{
15+
return static::CONFIG_NAME;
16+
}
17+
1118
public function getDefinition(): ArrayNodeDefinition
1219
{
1320
/** @var ArrayNodeDefinition $node */
14-
$node = self::createNode('_custom_scalar_config');
21+
$node = self::createNode(static::CONFIG_NAME);
1522

1623
/** @phpstan-ignore-next-line */
1724
$node

src/Config/EnumTypeDefinition.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111

1212
class EnumTypeDefinition extends TypeDefinition
1313
{
14+
public const CONFIG_NAME = '_enum_config';
15+
16+
public static function getName(): string
17+
{
18+
return static::CONFIG_NAME;
19+
}
20+
1421
public function getDefinition(): ArrayNodeDefinition
1522
{
1623
/** @var ArrayNodeDefinition $node */
17-
$node = self::createNode('_enum_config');
24+
$node = self::createNode(static::CONFIG_NAME);
1825

1926
/** @phpstan-ignore-next-line */
2027
$node

src/Config/InputObjectTypeDefinition.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99

1010
class InputObjectTypeDefinition extends TypeDefinition
1111
{
12+
public const CONFIG_NAME = '_input_object_config';
13+
14+
public static function getName(): string
15+
{
16+
return static::CONFIG_NAME;
17+
}
18+
1219
public function getDefinition(): ArrayNodeDefinition
1320
{
1421
/** @var ArrayNodeDefinition $node */
15-
$node = self::createNode('_input_object_config');
22+
$node = self::createNode(static::CONFIG_NAME);
1623

1724
/** @phpstan-ignore-next-line */
1825
$node

src/Config/InterfaceTypeDefinition.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88

99
class InterfaceTypeDefinition extends TypeWithOutputFieldsDefinition
1010
{
11+
public const CONFIG_NAME = '_interface_config';
12+
13+
public static function getName(): string
14+
{
15+
return static::CONFIG_NAME;
16+
}
17+
1118
public function getDefinition(): ArrayNodeDefinition
1219
{
1320
/** @var ArrayNodeDefinition $node */
14-
$node = self::createNode('_interface_config');
21+
$node = self::createNode(static::CONFIG_NAME);
1522

1623
/** @phpstan-ignore-next-line */
1724
$node

src/Config/ObjectTypeDefinition.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
class ObjectTypeDefinition extends TypeWithOutputFieldsDefinition
1212
{
13-
protected const CONFIG_NAME = '_object_config';
13+
public const CONFIG_NAME = '_object_config';
14+
15+
public static function getName(): string
16+
{
17+
return static::CONFIG_NAME;
18+
}
1419

1520
public function getDefinition(): ArrayNodeDefinition
1621
{

src/Config/Parser/GraphQL/ASTConverter/NodeInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88

99
interface NodeInterface
1010
{
11+
/**
12+
* @return array<string,mixed>
13+
*/
1114
public static function toConfig(Node $node): array;
1215
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Config;
6+
7+
use Overblog\GraphQLBundle\Enum\TypeEnum;
8+
9+
/**
10+
* TODO: refactor. This is dirty solution but quick and with minimal impact on existing structure.
11+
*/
12+
class PermittedInheritTypeProvider
13+
{
14+
/**
15+
* @return string[]
16+
*/
17+
public function getAllowedTypes(string $type): array
18+
{
19+
return [$type, ...$this->getExtraTypes($type)];
20+
}
21+
22+
/**
23+
* @return string[]
24+
*/
25+
protected function getExtraTypes(string $type): array
26+
{
27+
$allowedTypes = [];
28+
if (TypeEnum::OBJECT === $type) {
29+
$allowedTypes[] = TypeEnum::INTERFACE;
30+
}
31+
32+
return $allowedTypes;
33+
}
34+
}

src/Config/Processor/InheritanceProcessor.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88
use InvalidArgumentException;
9+
use Overblog\GraphQLBundle\Config\PermittedInheritTypeProvider;
910
use Overblog\GraphQLBundle\Enum\TypeEnum;
1011
use function array_column;
1112
use function array_filter;
@@ -29,6 +30,24 @@ final class InheritanceProcessor implements ProcessorInterface
2930
public const HEIRS_KEY = 'heirs';
3031
public const INHERITS_KEY = 'inherits';
3132

33+
/**
34+
* TODO: refactor. This is dirty solution but quick and with minimal impact on existing structure.
35+
*
36+
* @var class-string<PermittedInheritTypeProvider>
37+
*/
38+
private static $permittedInheritTypeProviderClass = PermittedInheritTypeProvider::class;
39+
40+
/**
41+
* @param class-string<PermittedInheritTypeProvider> $fqcn
42+
*/
43+
public static function setPermittedInheritTypeProviderClass(string $fqcn): void
44+
{
45+
if (!is_subclass_of($fqcn, PermittedInheritTypeProvider::class, true)) {
46+
throw new \InvalidArgumentException(sprintf('Options must be a FQCN implementing %s', PermittedInheritTypeProvider::class));
47+
}
48+
self::$permittedInheritTypeProviderClass = $fqcn;
49+
}
50+
3251
public static function process(array $configs): array
3352
{
3453
$configs = self::processConfigsHeirs($configs);
@@ -80,10 +99,7 @@ private static function processConfigsInherits(array $configs): array
8099
continue;
81100
}
82101

83-
$allowedTypes = [$config['type']];
84-
if (TypeEnum::OBJECT === $config['type']) {
85-
$allowedTypes[] = TypeEnum::INTERFACE;
86-
}
102+
$allowedTypes = self::getAllowedTypes($config['type']);
87103
$flattenInherits = self::flattenInherits($name, $configs, $allowedTypes);
88104
if (empty($flattenInherits)) {
89105
continue;
@@ -198,4 +214,15 @@ private static function mergeConfigs(array ...$configs): array
198214

199215
return $result;
200216
}
217+
218+
/**
219+
* @return string[]
220+
*/
221+
private static function getAllowedTypes(string $type): array
222+
{
223+
/** @var class-string<PermittedInheritTypeProvider> $class */
224+
$class = self::$permittedInheritTypeProviderClass;
225+
226+
return (new $class)->getAllowedTypes($type);
227+
}
201228
}

src/Config/TypeDefinition.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static function create(): self
3232
return new static();
3333
}
3434

35+
abstract public static function getName(): string;
36+
3537
protected function resolveTypeSection(): VariableNodeDefinition
3638
{
3739
return self::createNode('resolveType', 'variable');

src/Config/UnionTypeDefinition.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88

99
class UnionTypeDefinition extends TypeDefinition
1010
{
11+
public const CONFIG_NAME = '_union_config';
12+
13+
public static function getName(): string
14+
{
15+
return static::CONFIG_NAME;
16+
}
17+
1118
public function getDefinition(): ArrayNodeDefinition
1219
{
1320
/** @var ArrayNodeDefinition $node */
14-
$node = self::createNode('_union_config');
21+
$node = self::createNode(static::CONFIG_NAME);
1522

1623
/** @phpstan-ignore-next-line */
1724
$node

0 commit comments

Comments
 (0)