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