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