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