1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Controller; |
4
|
|
|
|
5
|
|
|
use PhpBoot\Application; |
6
|
|
|
use PhpBoot\Metas\ReturnMeta; |
7
|
|
|
use PhpBoot\Utils\ArrayAdaptor; |
8
|
|
|
use PhpBoot\Utils\ArrayHelper; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
class ResponseHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* 设置输出映射 |
16
|
|
|
* @param $target |
17
|
|
|
* @param ReturnMeta $src |
18
|
|
|
*/ |
19
|
4 |
|
public function setMapping($target, ReturnMeta $src) |
20
|
|
|
{ |
21
|
4 |
|
$this->mappings[$target] = $src; |
22
|
4 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $target |
26
|
|
|
* @return ReturnMeta |
27
|
|
|
*/ |
28
|
3 |
|
public function eraseMapping($target) |
29
|
|
|
{ |
30
|
3 |
|
if(!isset($this->mappings[$target])){ |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
3 |
|
$ori = $this->mappings[$target]; |
34
|
3 |
|
unset($this->mappings[$target]); |
35
|
3 |
|
return $ori; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $target |
40
|
|
|
* @return ReturnMeta |
41
|
|
|
*/ |
42
|
|
|
public function getMapping($target) |
43
|
|
|
{ |
44
|
|
|
if(!array_key_exists($target, $this->mappings)){ |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
return $this->mappings[$target]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $source |
52
|
|
|
* @return array [string,ReturnMeta] |
53
|
|
|
*/ |
54
|
4 |
|
public function getMappingBySource($source) |
55
|
|
|
{ |
56
|
4 |
|
foreach ($this->mappings as $k=>$v){ |
57
|
4 |
|
if($v->source == $source){ |
58
|
4 |
|
return [$k, $v]; |
59
|
|
|
} |
60
|
3 |
|
} |
61
|
|
|
return [null,null]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Application $app |
67
|
|
|
* @param $return |
68
|
|
|
* @param $params |
69
|
|
|
* @return Response |
70
|
|
|
*/ |
71
|
1 |
|
public function handle(Application $app, $return, $params) |
72
|
|
|
{ |
73
|
|
|
$input = [ |
74
|
1 |
|
'return'=>$return, |
75
|
|
|
'params'=>$params |
76
|
1 |
|
]; |
77
|
|
|
|
78
|
1 |
|
if($return instanceof Response){ //直接返回Response时, 对return不再做映射 |
79
|
|
|
return $return; |
80
|
|
|
} |
81
|
1 |
|
$mappings = $this->getMappings(); |
82
|
|
|
|
83
|
1 |
|
$output = []; |
84
|
1 |
|
foreach($mappings as $key=>$map){ |
85
|
1 |
|
$val = \JmesPath\search($map->source, $input); |
86
|
1 |
View Code Duplication |
if(substr($key, 0, strlen('response.')) == 'response.'){ |
|
|
|
|
87
|
1 |
|
$key = substr($key, strlen('response.')); |
88
|
1 |
|
} |
89
|
1 |
|
ArrayHelper::set($output, $key, $val); |
90
|
1 |
|
} |
91
|
1 |
|
$renderer = $app->get(ResponseRenderer::class); |
92
|
1 |
|
return $renderer->render($output); |
93
|
|
|
} |
94
|
|
|
/** |
95
|
|
|
* @return ReturnMeta[] |
96
|
|
|
*/ |
97
|
5 |
|
public function getMappings() |
98
|
|
|
{ |
99
|
5 |
|
return $this->mappings; |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
|
|
private $mappings; |
105
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.