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