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\DataCollector; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
16
|
|
|
use Symfony\Component\VarDumper\Cloner\Data; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\DefinitionInterface; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\InputObjectDefinition; |
19
|
|
|
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition; |
20
|
|
|
use Ynlo\GraphQLBundle\Definition\MutationDefinition; |
21
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinition; |
22
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
23
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
24
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
25
|
|
|
use Ynlo\GraphQLBundle\Extension\EndpointNotValidException; |
26
|
|
|
use Ynlo\GraphQLBundle\Security\EndpointResolver; |
27
|
|
|
|
28
|
|
|
class GraphQLDataCollector extends DataCollector |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DefinitionRegistry |
32
|
|
|
*/ |
33
|
|
|
protected $registry; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EndpointResolver |
37
|
|
|
*/ |
38
|
|
|
protected $endpointResolver; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* GraphQLDataCollector constructor. |
42
|
|
|
* |
43
|
|
|
* @param DefinitionRegistry $registry |
44
|
|
|
* @param EndpointResolver $endpointResolver |
45
|
|
|
*/ |
46
|
22 |
|
public function __construct(DefinitionRegistry $registry, EndpointResolver $endpointResolver) |
47
|
|
|
{ |
48
|
22 |
|
$this->registry = $registry; |
49
|
22 |
|
$this->endpointResolver = $endpointResolver; |
50
|
22 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
22 |
|
public function getName() |
56
|
|
|
{ |
57
|
22 |
|
return 'graphql'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function reset() |
64
|
|
|
{ |
65
|
|
|
$this->data = []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$name = $this->endpointResolver->resolveEndpoint($request); |
75
|
|
|
$this->data = [ |
76
|
|
|
'defaultEndpoint' => $this->registry->getEndpoint(), |
77
|
|
|
'endpoint' => $this->registry->getEndpoint($name), |
78
|
|
|
]; |
79
|
|
|
} catch (EndpointNotValidException $exception) { |
80
|
|
|
//do nothing |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isOperationAvailable(QueryDefinition $definition) |
85
|
|
|
{ |
86
|
|
|
if ($definition instanceof MutationDefinition) { |
87
|
|
|
return $this->data['endpoint']->hasMutation($definition->getName()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->data['endpoint']->hasQuery($definition->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function isTypeAvailable(DefinitionInterface $definition) |
94
|
|
|
{ |
95
|
|
|
return $this->data['endpoint']->hasType($definition->getName()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Endpoint |
100
|
|
|
*/ |
101
|
|
|
public function getEndpoint() |
102
|
|
|
{ |
103
|
|
|
return $this->data['defaultEndpoint'] ?? null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isInputObject($definition) |
107
|
|
|
{ |
108
|
|
|
return $definition instanceof InputObjectDefinition; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isInterface($definition) |
112
|
|
|
{ |
113
|
|
|
return $definition instanceof InterfaceDefinition; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isObject($definition) |
117
|
|
|
{ |
118
|
|
|
return $definition instanceof ObjectDefinition; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $data |
123
|
|
|
* |
124
|
|
|
* @return Data |
125
|
|
|
*/ |
126
|
|
|
public static function arrayToData(array $data) |
127
|
|
|
{ |
128
|
|
|
return new Data($data); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|