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