1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
6
|
|
|
|
7
|
|
|
use function array_keys; |
8
|
|
|
use function is_array; |
9
|
|
|
use function is_callable; |
10
|
|
|
|
11
|
|
|
abstract class AbstractResolver implements FluentResolverInterface |
12
|
|
|
{ |
13
|
|
|
private array $solutions = []; |
14
|
|
|
private array $aliases = []; |
15
|
|
|
private array $solutionOptions = []; |
16
|
|
|
private array $fullyLoadedSolutions = []; |
17
|
|
|
|
18
|
138 |
|
public function addSolution(string $id, callable $factory, array $aliases = [], array $options = []): self |
19
|
|
|
{ |
20
|
138 |
|
$this->fullyLoadedSolutions[$id] = false; |
21
|
138 |
|
$this->addAliases($id, $aliases); |
22
|
|
|
|
23
|
138 |
|
$this->solutions[$id] = $factory; |
24
|
138 |
|
$this->solutionOptions[$id] = $options; |
25
|
|
|
|
26
|
138 |
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
129 |
|
public function hasSolution(string $id): bool |
30
|
|
|
{ |
31
|
129 |
|
$id = $this->resolveAlias($id); |
32
|
|
|
|
33
|
129 |
|
return isset($this->solutions[$id]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
124 |
|
public function getSolution(string $id) |
40
|
|
|
{ |
41
|
124 |
|
return $this->loadSolution($id); |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
public function getSolutions(): array |
45
|
|
|
{ |
46
|
5 |
|
return $this->loadSolutions(); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
public function getSolutionAliases(string $id): array |
50
|
|
|
{ |
51
|
5 |
|
return array_keys($this->aliases, $id); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getAliases(): array |
55
|
|
|
{ |
56
|
|
|
return $this->aliases; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
70 |
|
public function getSolutionOptions(string $id) |
63
|
|
|
{ |
64
|
70 |
|
$id = $this->resolveAlias($id); |
65
|
|
|
|
66
|
70 |
|
return $this->solutionOptions[$id] ?? []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $solution |
71
|
|
|
*/ |
72
|
74 |
|
protected function onLoadSolution($solution): void |
|
|
|
|
73
|
|
|
{ |
74
|
74 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
129 |
|
private function loadSolution(string $id) |
80
|
|
|
{ |
81
|
129 |
|
$id = $this->resolveAlias($id); |
82
|
129 |
|
if (!$this->hasSolution($id)) { |
83
|
7 |
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
125 |
|
if ($this->fullyLoadedSolutions[$id]) { |
87
|
23 |
|
return $this->solutions[$id]; |
88
|
|
|
} else { |
89
|
125 |
|
$loader = $this->solutions[$id]; |
90
|
125 |
|
$this->solutions[$id] = $solution = $loader(); |
91
|
124 |
|
$this->onLoadSolution($solution); |
92
|
124 |
|
$this->fullyLoadedSolutions[$id] = true; |
93
|
|
|
|
94
|
124 |
|
return $solution; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
138 |
|
private function addAliases(string $id, array $aliases): void |
99
|
|
|
{ |
100
|
138 |
|
foreach ($aliases as $alias) { |
101
|
138 |
|
$this->aliases[$alias] = $id; |
102
|
|
|
} |
103
|
138 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param object|array $solutionOrFactory |
107
|
|
|
*/ |
108
|
|
|
private static function isSolutionFactory($solutionOrFactory): bool |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return is_array($solutionOrFactory) && isset($solutionOrFactory[0]) && is_callable($solutionOrFactory[0]); |
111
|
|
|
} |
112
|
|
|
|
113
|
129 |
|
private function resolveAlias(string $alias): string |
114
|
|
|
{ |
115
|
129 |
|
return $this->aliases[$alias] ?? $alias; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return mixed[] |
120
|
|
|
*/ |
121
|
5 |
|
private function loadSolutions(): array |
122
|
|
|
{ |
123
|
5 |
|
foreach ($this->solutions as $name => &$solution) { |
124
|
5 |
|
$solution = $this->loadSolution($name); |
125
|
|
|
} |
126
|
|
|
|
127
|
5 |
|
return $this->solutions; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.