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