|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractResolver implements FluentResolverInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array */ |
|
10
|
|
|
private $solutions = []; |
|
11
|
|
|
|
|
12
|
|
|
private $aliases = []; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
private $solutionOptions = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
private $fullyLoadedSolutions = []; |
|
19
|
|
|
|
|
20
|
123 |
|
/** @var bool */ |
|
21
|
|
|
private $ignoreCase = true; |
|
22
|
123 |
|
|
|
23
|
123 |
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->ignoreCase = \version_compare(Kernel::VERSION, '3.3.0') < 0; |
|
26
|
118 |
|
} |
|
27
|
118 |
|
|
|
28
|
117 |
|
public function addSolution($id, $solutionOrFactory, array $aliases = [], array $options = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$id = $this->cleanIdOrAlias($id); |
|
31
|
117 |
|
$this->fullyLoadedSolutions[$id] = false; |
|
32
|
|
|
$this->addAliases($id, $aliases); |
|
33
|
117 |
|
|
|
34
|
|
|
$this->solutions[$id] = function () use ($id, $solutionOrFactory) { |
|
35
|
116 |
|
$solution = $solutionOrFactory; |
|
36
|
|
|
if (self::isSolutionFactory($solutionOrFactory)) { |
|
37
|
123 |
|
if (!isset($solutionOrFactory[1])) { |
|
38
|
|
|
$solutionOrFactory[1] = []; |
|
39
|
123 |
|
} |
|
40
|
|
|
$solution = \call_user_func_array(...$solutionOrFactory); |
|
41
|
|
|
} |
|
42
|
121 |
|
$this->checkSolution($id, $solution); |
|
43
|
|
|
|
|
44
|
121 |
|
return $solution; |
|
45
|
|
|
}; |
|
46
|
121 |
|
$this->solutionOptions[$id] = $options; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function hasSolution($id) |
|
52
|
|
|
{ |
|
53
|
|
|
$id = $this->resolveAlias($id); |
|
54
|
116 |
|
|
|
55
|
|
|
return isset($this->solutions[$id]); |
|
56
|
116 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $id |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed |
|
62
|
5 |
|
*/ |
|
63
|
|
|
public function getSolution($id) |
|
64
|
5 |
|
{ |
|
65
|
|
|
return $this->loadSolution($id); |
|
66
|
|
|
} |
|
67
|
5 |
|
|
|
68
|
|
|
/** |
|
69
|
5 |
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getSolutions() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->loadSolutions(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getSolutionAliases($id) |
|
77
|
50 |
|
{ |
|
78
|
|
|
return \array_keys($this->aliases, $id); |
|
79
|
50 |
|
} |
|
80
|
|
|
|
|
81
|
50 |
|
/** |
|
82
|
|
|
* @param $id |
|
83
|
|
|
* |
|
84
|
54 |
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
54 |
|
public function getSolutionOptions($id) |
|
87
|
|
|
{ |
|
88
|
|
|
$id = $this->resolveAlias($id); |
|
89
|
|
|
|
|
90
|
|
|
return isset($this->solutionOptions[$id]) ? $this->solutionOptions[$id] : []; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
121 |
|
/** |
|
94
|
|
|
* @param string $id |
|
95
|
121 |
|
* |
|
96
|
121 |
|
* @return mixed |
|
97
|
6 |
|
*/ |
|
98
|
|
|
private function loadSolution($id) |
|
99
|
|
|
{ |
|
100
|
118 |
|
$id = $this->resolveAlias($id); |
|
101
|
30 |
|
if (!$this->hasSolution($id)) { |
|
102
|
|
|
return null; |
|
103
|
118 |
|
} |
|
104
|
118 |
|
|
|
105
|
116 |
|
if ($this->fullyLoadedSolutions[$id]) { |
|
106
|
116 |
|
return $this->solutions[$id]; |
|
107
|
|
|
} else { |
|
108
|
116 |
|
$loader = $this->solutions[$id]; |
|
109
|
|
|
$this->solutions[$id] = $loader(); |
|
110
|
|
|
$this->fullyLoadedSolutions[$id] = true; |
|
111
|
|
|
|
|
112
|
123 |
|
return $this->solutions[$id]; |
|
113
|
|
|
} |
|
114
|
123 |
|
} |
|
115
|
123 |
|
|
|
116
|
|
|
private function addAliases($id, $aliases) |
|
117
|
123 |
|
{ |
|
118
|
|
|
foreach ($aliases as $alias) { |
|
119
|
118 |
|
$this->aliases[$this->cleanIdOrAlias($alias)] = $id; |
|
120
|
|
|
} |
|
121
|
118 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
private static function isSolutionFactory($solutionOrFactory) |
|
124
|
121 |
|
{ |
|
125
|
|
|
return \is_array($solutionOrFactory) && isset($solutionOrFactory[0]) && \is_callable($solutionOrFactory[0]); |
|
126
|
121 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function resolveAlias($alias) |
|
129
|
|
|
{ |
|
130
|
|
|
$alias = $this->cleanIdOrAlias($alias); |
|
131
|
|
|
|
|
132
|
5 |
|
return isset($this->aliases[$alias]) ? $this->aliases[$alias] : $alias; |
|
133
|
|
|
} |
|
134
|
5 |
|
|
|
135
|
5 |
|
private function cleanIdOrAlias($idOrAlias) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->ignoreCase ? \strtolower($idOrAlias) : $idOrAlias; |
|
138
|
5 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return mixed[] |
|
142
|
|
|
*/ |
|
143
|
|
|
private function loadSolutions() |
|
144
|
|
|
{ |
|
145
|
|
|
foreach ($this->solutions as $name => &$solution) { |
|
146
|
117 |
|
$solution = $this->loadSolution($name); |
|
147
|
|
|
} |
|
148
|
117 |
|
|
|
149
|
|
|
return $this->solutions; |
|
150
|
117 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
117 |
|
* @param mixed $solution |
|
154
|
|
|
* |
|
155
|
117 |
|
* @return bool |
|
156
|
1 |
|
*/ |
|
157
|
1 |
|
protected function supportsSolution($solution) |
|
158
|
|
|
{ |
|
159
|
|
|
$supportedClass = $this->supportedSolutionClass(); |
|
160
|
116 |
|
|
|
161
|
|
|
return null === $supportedClass || $solution instanceof $supportedClass; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function checkSolution($id, $solution) |
|
165
|
|
|
{ |
|
166
|
|
|
if (!$this->supportsSolution($solution)) { |
|
167
|
54 |
|
throw new UnsupportedResolverException( |
|
168
|
|
|
\sprintf('Resolver "%s" must be "%s" "%s" given.', $id, $this->supportedSolutionClass(), \get_class($solution)) |
|
169
|
54 |
|
); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* default return null to accept mixed type. |
|
175
|
|
|
* |
|
176
|
|
|
* @return null|string supported class name |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function supportedSolutionClass() |
|
179
|
|
|
{ |
|
180
|
|
|
return null; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|