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