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