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