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