1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
8
|
|
|
use function array_keys; |
9
|
|
|
use function call_user_func_array; |
10
|
|
|
use function get_class; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_callable; |
13
|
|
|
use function sprintf; |
14
|
|
|
|
15
|
|
|
abstract class AbstractResolver implements FluentResolverInterface |
16
|
|
|
{ |
17
|
|
|
private array $solutions = []; |
18
|
|
|
private array $aliases = []; |
19
|
|
|
private array $solutionOptions = []; |
20
|
|
|
private array $fullyLoadedSolutions = []; |
21
|
|
|
|
22
|
139 |
|
public function addSolution(string $id, $solutionOrFactory, array $aliases = [], array $options = []): self |
23
|
|
|
{ |
24
|
139 |
|
$this->fullyLoadedSolutions[$id] = false; |
25
|
139 |
|
$this->addAliases($id, $aliases); |
26
|
|
|
|
27
|
139 |
|
$this->solutions[$id] = function () use ($id, $solutionOrFactory) { |
28
|
126 |
|
$solution = $solutionOrFactory; |
29
|
126 |
|
if (self::isSolutionFactory($solutionOrFactory)) { |
30
|
125 |
|
if (!isset($solutionOrFactory[1])) { |
31
|
|
|
$solutionOrFactory[1] = []; |
32
|
|
|
} |
33
|
125 |
|
$solution = call_user_func_array(...$solutionOrFactory); |
|
|
|
|
34
|
|
|
} |
35
|
125 |
|
$this->checkSolution($id, $solution); |
36
|
|
|
|
37
|
124 |
|
return $solution; |
38
|
|
|
}; |
39
|
139 |
|
$this->solutionOptions[$id] = $options; |
40
|
|
|
|
41
|
139 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
130 |
|
public function hasSolution(string $id): bool |
45
|
|
|
{ |
46
|
130 |
|
$id = $this->resolveAlias($id); |
47
|
|
|
|
48
|
130 |
|
return isset($this->solutions[$id]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
125 |
|
public function getSolution(string $id) |
55
|
|
|
{ |
56
|
125 |
|
return $this->loadSolution($id); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
public function getSolutions(): array |
60
|
|
|
{ |
61
|
5 |
|
return $this->loadSolutions(); |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
public function getSolutionAliases(string $id): array |
65
|
|
|
{ |
66
|
5 |
|
return array_keys($this->aliases, $id); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getAliases(): array |
70
|
|
|
{ |
71
|
|
|
return $this->aliases; |
72
|
70 |
|
} |
73
|
|
|
|
74
|
70 |
|
/** |
75
|
|
|
* @return mixed |
76
|
70 |
|
*/ |
77
|
|
|
public function getSolutionOptions(string $id) |
78
|
|
|
{ |
79
|
|
|
$id = $this->resolveAlias($id); |
80
|
|
|
|
81
|
|
|
return isset($this->solutionOptions[$id]) ? $this->solutionOptions[$id] : []; |
82
|
74 |
|
} |
83
|
|
|
|
84
|
74 |
|
/** |
85
|
|
|
* @param mixed $solution |
86
|
|
|
*/ |
87
|
|
|
protected function onLoadSolution($solution): void |
88
|
|
|
{ |
89
|
130 |
|
} |
90
|
|
|
|
91
|
130 |
|
/** |
92
|
130 |
|
* @return mixed |
93
|
7 |
|
*/ |
94
|
|
|
private function loadSolution(string $id) |
95
|
|
|
{ |
96
|
126 |
|
$id = $this->resolveAlias($id); |
97
|
23 |
|
if (!$this->hasSolution($id)) { |
98
|
|
|
return null; |
99
|
126 |
|
} |
100
|
126 |
|
|
101
|
124 |
|
if ($this->fullyLoadedSolutions[$id]) { |
102
|
124 |
|
return $this->solutions[$id]; |
103
|
|
|
} else { |
104
|
124 |
|
$loader = $this->solutions[$id]; |
105
|
|
|
$this->solutions[$id] = $solution = $loader(); |
106
|
|
|
$this->onLoadSolution($solution); |
107
|
|
|
$this->fullyLoadedSolutions[$id] = true; |
108
|
139 |
|
|
109
|
|
|
return $solution; |
110
|
139 |
|
} |
111
|
139 |
|
} |
112
|
|
|
|
113
|
139 |
|
private function addAliases(string $id, array $aliases): void |
114
|
|
|
{ |
115
|
|
|
foreach ($aliases as $alias) { |
116
|
|
|
$this->aliases[$alias] = $id; |
117
|
|
|
} |
118
|
126 |
|
} |
119
|
|
|
|
120
|
126 |
|
/** |
121
|
|
|
* @param object|array $solutionOrFactory |
122
|
|
|
*/ |
123
|
130 |
|
private static function isSolutionFactory($solutionOrFactory): bool |
124
|
|
|
{ |
125
|
130 |
|
return is_array($solutionOrFactory) && isset($solutionOrFactory[0]) && is_callable($solutionOrFactory[0]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function resolveAlias(string $alias): string |
129
|
|
|
{ |
130
|
|
|
return isset($this->aliases[$alias]) ? $this->aliases[$alias] : $alias; |
131
|
5 |
|
} |
132
|
|
|
|
133
|
5 |
|
/** |
134
|
5 |
|
* @return mixed[] |
135
|
|
|
*/ |
136
|
|
|
private function loadSolutions(): array |
137
|
5 |
|
{ |
138
|
|
|
foreach ($this->solutions as $name => &$solution) { |
139
|
|
|
$solution = $this->loadSolution($name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->solutions; |
143
|
125 |
|
} |
144
|
|
|
|
145
|
125 |
|
/** |
146
|
|
|
* @param mixed $solution |
147
|
125 |
|
*/ |
148
|
|
|
protected function supportsSolution($solution): bool |
149
|
|
|
{ |
150
|
|
|
$supportedClass = $this->supportedSolutionClass(); |
|
|
|
|
151
|
|
|
|
152
|
|
|
return null === $supportedClass || $solution instanceof $supportedClass; |
153
|
125 |
|
} |
154
|
|
|
|
155
|
125 |
|
/** |
156
|
1 |
|
* @param mixed $solution |
157
|
1 |
|
*/ |
158
|
|
|
protected function checkSolution(string $id, $solution): void |
159
|
|
|
{ |
160
|
124 |
|
if (!$this->supportsSolution($solution)) { |
161
|
|
|
throw new UnsupportedResolverException( |
162
|
|
|
sprintf('Resolver "%s" must be "%s" "%s" given.', $id, $this->supportedSolutionClass(), get_class($solution)) |
|
|
|
|
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
74 |
|
/** |
168
|
|
|
* default return null to accept mixed type. |
169
|
74 |
|
* |
170
|
|
|
* @return string|null supported class name |
171
|
|
|
*/ |
172
|
|
|
protected function supportedSolutionClass(): ?string |
173
|
|
|
{ |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.