|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Plugin; |
|
12
|
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Definition\DefinitionInterface; |
|
14
|
|
|
use Ynlo\GraphQLBundle\Definition\HasExtensionsInterface; |
|
15
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
|
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinition; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Util\ClassUtils; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* This plugin automatically load CRUD extensions |
|
22
|
|
|
* based on object interfaces and registered interfaces |
|
23
|
|
|
*/ |
|
24
|
|
|
class CRUDExtensionResolverPlugin extends AbstractDefinitionPlugin |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getName(): string |
|
30
|
|
|
{ |
|
31
|
|
|
return 'extension'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritDoc} |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config): void |
|
38
|
|
|
{ |
|
39
|
2 |
|
if ($definition instanceof InterfaceDefinition && $definition->getImplementors()) { |
|
40
|
1 |
|
$this->resolveInterfaceExtension($definition, $endpoint); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
if ($definition instanceof ObjectDefinition) { |
|
44
|
1 |
|
$this->resolveObjectRealInterfaceExtensions($definition); |
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Using naming convention resolve CRUD extension for given interface definition and automatically register this extension |
|
50
|
|
|
* for all interface implementors |
|
51
|
|
|
* |
|
52
|
|
|
* @param InterfaceDefinition $definition |
|
53
|
|
|
* @param Endpoint $endpoint |
|
54
|
|
|
*/ |
|
55
|
1 |
|
protected function resolveInterfaceExtension(InterfaceDefinition $definition, Endpoint $endpoint): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
$bundleNamespace = ClassUtils::relatedBundleNamespace($definition->getClass()); |
|
58
|
1 |
|
$extensionClass = ClassUtils::applyNamingConvention($bundleNamespace, 'Extension', null, $definition->getName().'Extension'); |
|
59
|
1 |
|
if (class_exists($extensionClass)) { |
|
60
|
1 |
|
foreach ($definition->getImplementors() as $implementor) { |
|
61
|
1 |
|
$definition->addExtension($extensionClass); |
|
62
|
1 |
|
$object = $endpoint->getType($implementor); |
|
63
|
1 |
|
if ($object instanceof HasExtensionsInterface) { |
|
64
|
1 |
|
$object->addExtension($extensionClass); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Using naming convention resolve all extensions for given object |
|
72
|
|
|
* based on implemented interfaces. |
|
73
|
|
|
* |
|
74
|
|
|
* This method use PHP real interfaces instead of registered interface types. |
|
75
|
|
|
* |
|
76
|
|
|
* @param ObjectDefinition $definition |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \ReflectionException |
|
79
|
|
|
*/ |
|
80
|
1 |
|
protected function resolveObjectRealInterfaceExtensions(ObjectDefinition $definition): void |
|
81
|
|
|
{ |
|
82
|
1 |
|
$class = $definition->getClass(); |
|
83
|
|
|
|
|
84
|
1 |
|
if (class_exists($class)) { |
|
85
|
1 |
|
$refClass = new \ReflectionClass($definition->getClass()); |
|
86
|
1 |
|
if ($interfaces = $refClass->getInterfaceNames()) { |
|
87
|
1 |
|
foreach ($interfaces as $interface) { |
|
88
|
1 |
|
$bundleNamespace = ClassUtils::relatedBundleNamespace($interface); |
|
89
|
1 |
|
if (preg_match('/(\w+)Interface?$/', $interface, $matches)) { |
|
90
|
1 |
|
$extensionClass = ClassUtils::applyNamingConvention($bundleNamespace, 'Extension', null, $matches[1].'Extension'); |
|
91
|
1 |
|
if (class_exists($extensionClass)) { |
|
92
|
1 |
|
if ($definition instanceof HasExtensionsInterface) { |
|
93
|
1 |
|
$definition->addExtension($extensionClass); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
1 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|