1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/******************************************************************************* |
4
|
|
|
* This file is part of the GraphQL Bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) YnloUltratech <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
******************************************************************************/ |
11
|
|
|
|
12
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Plugin; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\DefinitionInterface; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\ImplementorInterface; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
19
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
20
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
21
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This plugin remove non used definitions |
25
|
|
|
*/ |
26
|
|
|
class CleanUpDefinitionPlugin extends AbstractDefinitionPlugin |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Endpoint |
30
|
|
|
*/ |
31
|
|
|
protected $endpoint; |
32
|
|
|
|
33
|
|
|
protected $used = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function buildConfig(ArrayNodeDefinition $root): void |
39
|
|
|
{ |
40
|
|
|
$root |
41
|
|
|
->info('Remove non used definitions') |
42
|
|
|
->scalarPrototype(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function configureEndpoint(Endpoint $endpoint): void |
49
|
|
|
{ |
50
|
2 |
|
if ($endpoint->getName() === DefinitionRegistry::DEFAULT_ENDPOINT) { |
51
|
1 |
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$this->used = []; |
55
|
1 |
|
$this->endpoint = $endpoint; |
56
|
|
|
|
57
|
1 |
|
$this->processOperations($endpoint->allQueries()); |
58
|
1 |
|
$this->processOperations($endpoint->allMutations()); |
59
|
|
|
|
60
|
1 |
|
foreach ($endpoint->allTypes() as $type) { |
61
|
1 |
|
if (!\in_array($type->getName(), $this->used)) { |
62
|
1 |
|
$endpoint->removeType($type->getName()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param QueryDefinition[] $operations |
69
|
|
|
*/ |
70
|
1 |
|
protected function processOperations($operations) |
71
|
|
|
{ |
72
|
1 |
|
foreach ($operations as $operation) { |
73
|
1 |
|
$this->used($operation->getType()); |
74
|
1 |
|
foreach ($operation->getArguments() as $argument) { |
75
|
1 |
|
$this->used($argument->getType()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string|DefinitionInterface $definition |
82
|
|
|
*/ |
83
|
1 |
|
protected function used($definition): void |
84
|
|
|
{ |
85
|
1 |
|
if (\is_string($definition)) { |
86
|
1 |
|
if (!$this->endpoint->hasType($definition)) { |
87
|
1 |
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$definition = $this->endpoint->getType($definition); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
if (\in_array($definition->getName(), $this->used)) { |
94
|
1 |
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$this->used[] = $definition->getName(); |
98
|
1 |
|
if ($definition instanceof FieldsAwareDefinitionInterface) { |
99
|
1 |
|
foreach ($definition->getFields() as $field) { |
100
|
1 |
|
$this->used($field->getType()); |
101
|
1 |
|
foreach ($field->getArguments() as $argument) { |
102
|
1 |
|
$this->used($argument->getType()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
if ($definition instanceof InterfaceDefinition) { |
108
|
1 |
|
foreach ($definition->getImplementors() as $implementor) { |
109
|
1 |
|
$this->used($implementor); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
if ($definition instanceof ImplementorInterface) { |
114
|
1 |
|
foreach ($definition->getInterfaces() as $interface) { |
115
|
1 |
|
$this->used($interface); |
116
|
|
|
} |
117
|
|
|
} |
118
|
1 |
|
} |
119
|
|
|
} |
120
|
|
|
|