1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Services; |
4
|
|
|
|
5
|
|
|
use Mouf\Mvc\Splash\Utils\SplashException; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A class that holds all parameter fetchers. |
10
|
|
|
*/ |
11
|
|
|
class ParameterFetcherRegistry |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ParameterFetcher[] |
15
|
|
|
*/ |
16
|
|
|
private $parameterFetchers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ParameterFetcher[] $parameterFetchers |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $parameterFetchers = []) |
22
|
|
|
{ |
23
|
|
|
$this->parameterFetchers = $parameterFetchers; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Builds a registry with the default fetchers. |
28
|
|
|
* |
29
|
|
|
* @return ParameterFetcherRegistry |
30
|
|
|
*/ |
31
|
|
|
public static function buildDefaultControllerRegistry() : ParameterFetcherRegistry |
32
|
|
|
{ |
33
|
|
|
return new self([ |
34
|
|
|
new SplashRequestFetcher(), |
35
|
|
|
new SplashRequestParameterFetcher(), |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Adds a parameter fetcher. It will be executed at the top of the list (first). |
41
|
|
|
* |
42
|
|
|
* @param ParameterFetcher $parameterFetcher |
43
|
|
|
* |
44
|
|
|
* @return ParameterFetcherRegistry |
45
|
|
|
*/ |
46
|
|
|
public function registerParameterFetcher(ParameterFetcher $parameterFetcher) : ParameterFetcherRegistry |
47
|
|
|
{ |
48
|
|
|
array_unshift($this->parameterFetchers, $parameterFetcher); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Analyses the method and returns an array of SplashRequestParameterFetcher. |
55
|
|
|
* Note: the return from this method is meant to be cached. |
56
|
|
|
* |
57
|
|
|
* @param ReflectionMethod $refMethod |
58
|
|
|
* @param string $url |
59
|
|
|
* |
60
|
|
|
* @return array[] An array representing serializable fetchers. Each fetcher is represented as an array with 2 keys: "fetcherId" (an ID for the fetcher) and "data" (data required by the fetcher) |
61
|
|
|
* |
62
|
|
|
* @throws SplashException |
63
|
|
|
*/ |
64
|
|
|
public function mapParameters(ReflectionMethod $refMethod, string $url = null) : array |
65
|
|
|
{ |
66
|
|
|
$parameters = $refMethod->getParameters(); |
67
|
|
|
|
68
|
|
|
$values = []; |
69
|
|
|
|
70
|
|
|
foreach ($parameters as $parameter) { |
71
|
|
|
$found = false; |
72
|
|
|
foreach ($this->parameterFetchers as $id => $fetcher) { |
73
|
|
|
if ($fetcher->canHandle($parameter)) { |
74
|
|
|
$data = $fetcher->getFetcherData($parameter, $url); |
75
|
|
|
$values[] = ['fetcherId' => $id, 'data' => $data]; |
76
|
|
|
$found = true; |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (!$found) { |
81
|
|
|
throw new SplashException('Unable to handle parameter $'.$parameter->getName().' in '.$parameter->getDeclaringClass()->getName().'::'.$parameter->getDeclaringFunction()->getName()); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $values; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Maps data returned by mapParameters to real arguments to be passed to the action. |
90
|
|
|
* |
91
|
|
|
* @param SplashRequestContext $context |
92
|
|
|
* @param array $parametersMap |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function toArguments(SplashRequestContext $context, array $parametersMap) : array |
97
|
|
|
{ |
98
|
|
|
$arguments = []; |
99
|
|
|
foreach ($parametersMap as $parameter) { |
100
|
|
|
$fetcherid = $parameter['fetcherId']; |
101
|
|
|
$data = $parameter['data']; |
102
|
|
|
$arguments[] = $this->parameterFetchers[$fetcherid]->fetchValue($data, $context); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $arguments; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|