1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Controller; |
4
|
|
|
|
5
|
|
|
use PhpBoot\Application; |
6
|
|
|
use PhpBoot\Metas\ParamMeta; |
7
|
|
|
use PhpBoot\Utils\ArrayAdaptor; |
8
|
|
|
use PhpBoot\Validator\Validator; |
9
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
12
|
|
|
|
13
|
|
|
class RequestHandler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param ParamMeta[] $paramMates |
17
|
|
|
*/ |
18
|
4 |
|
public function __construct(array $paramMates=[]){ |
19
|
4 |
|
$this->paramMetas = $paramMates; |
20
|
4 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Application $app |
24
|
|
|
* @param Request $request |
25
|
|
|
* @param array $params |
26
|
|
|
* @param array $reference |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
1 |
|
public function handle(Application $app, Request $request, array &$params, array &$reference){ |
|
|
|
|
30
|
|
|
|
31
|
1 |
|
$vld = new Validator(); |
32
|
1 |
|
$req = ['request'=>$request]; |
33
|
1 |
|
$requestArray = new ArrayAdaptor($req); |
34
|
1 |
|
$inputs = []; |
35
|
1 |
View Code Duplication |
foreach ($this->paramMetas as $k=>$meta){ |
|
|
|
|
36
|
|
|
if($meta->isPassedByReference){ |
37
|
|
|
// param PassedByReference is used to output |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
$source = \JmesPath\search($meta->source, $requestArray); |
41
|
|
|
if ($source !== null){ |
42
|
|
|
$source = ArrayAdaptor::strip($source); |
43
|
|
|
if($source instanceof ParameterBag){ |
44
|
|
|
$source = $source->all(); |
45
|
|
|
} |
46
|
|
|
if($meta->container){ |
47
|
|
|
$inputs[$meta->name] = $meta->container->make($source); |
48
|
|
|
}else{ |
49
|
|
|
$inputs[$meta->name] = $source; |
50
|
|
|
} |
51
|
|
|
if($meta->validation){ |
52
|
|
|
$vld->rule($meta->validation, $meta->name); |
53
|
|
|
} |
54
|
|
|
}else{ |
55
|
|
|
$meta->isOptional or \PhpBoot\abort(new BadRequestHttpException("the parameter \"{$meta->source}\" is missing")); |
56
|
|
|
$inputs[$meta->name] = $meta->default; |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
1 |
|
$vld = $vld->withData($inputs); |
60
|
1 |
|
$vld->validate() or \PhpBoot\abort( |
61
|
|
|
new \InvalidArgumentException( |
62
|
|
|
json_encode( |
63
|
|
|
$vld->errors(), |
64
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
65
|
|
|
) |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
1 |
|
$pos = 0; |
70
|
1 |
|
foreach ($this->paramMetas as $meta){ |
71
|
|
|
if($meta->isPassedByReference){ |
72
|
|
|
$params[$pos] = &$reference[$meta->name]; |
73
|
|
|
}else{ |
74
|
|
|
$params[$pos] = $inputs[$meta->name]; |
75
|
|
|
} |
76
|
|
|
$pos++; |
77
|
|
|
|
78
|
1 |
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
public function getParamNames(){ |
82
|
|
|
return array_map(function($meta){return $meta->name;}, $this->paramMetas); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* 获取参数列表 |
87
|
|
|
* @return ParamMeta[] |
88
|
|
|
*/ |
89
|
5 |
|
public function getParamMetas(){ |
90
|
5 |
|
return $this->paramMetas; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 获取指定参数信息 |
95
|
|
|
* @param $name |
96
|
|
|
* @return ParamMeta|null |
97
|
|
|
*/ |
98
|
3 |
|
public function getParamMeta($name){ |
99
|
3 |
|
foreach ($this->paramMetas as $meta){ |
100
|
3 |
|
if($meta->name == $name){ |
101
|
3 |
|
return $meta; |
102
|
|
|
} |
103
|
3 |
|
} |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param \PhpBoot\Metas\ParamMeta[] $paramMetas |
109
|
|
|
*/ |
110
|
4 |
|
public function setParamMetas($paramMetas) |
111
|
|
|
{ |
112
|
4 |
|
$this->paramMetas = $paramMetas; |
113
|
4 |
|
} |
114
|
|
|
/** |
115
|
|
|
* @var ParamMeta[] |
116
|
|
|
*/ |
117
|
|
|
private $paramMetas = []; |
118
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.