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