|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mcustiel\PowerRoute\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Mcustiel\PowerRoute\Common\RequestUrlAccess; |
|
6
|
|
|
use Mcustiel\PowerRoute\Common\TransactionData; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
trait PlaceholderEvaluator |
|
10
|
|
|
{ |
|
11
|
|
|
use RequestUrlAccess; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param mixed $value |
|
15
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $transactiondata |
|
16
|
|
|
* |
|
17
|
|
|
* @return mixed |
|
18
|
|
|
*/ |
|
19
|
7 |
|
public function getValueOrPlaceholder($value, TransactionData $transactiondata) |
|
20
|
|
|
{ |
|
21
|
7 |
|
return preg_replace_callback( |
|
22
|
7 |
|
'/\{\{\s*(var|uri|get|post|header|cookie|method)(?:\.([a-z0-9-_]+))?\s*\}\}/i', |
|
23
|
7 |
|
function ($matches) use ($transactiondata) { |
|
24
|
2 |
|
return $this->getValueFromPlaceholder( |
|
25
|
2 |
|
$matches[1], |
|
26
|
2 |
|
isset($matches[2]) ? $matches[2] : null, |
|
27
|
|
|
$transactiondata |
|
28
|
2 |
|
); |
|
29
|
7 |
|
}, |
|
30
|
|
|
$value |
|
31
|
7 |
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $from |
|
36
|
|
|
* @param string|null $name |
|
37
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $transactionData |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
2 |
|
private function getValueFromPlaceholder($from, $name, TransactionData $transactionData) |
|
42
|
|
|
{ |
|
43
|
|
|
switch ($from) { |
|
44
|
2 |
|
case 'var': |
|
45
|
|
|
return $transactionData->get($name); |
|
46
|
2 |
|
case 'method': |
|
47
|
|
|
return $transactionData->getRequest()->getMethod(); |
|
48
|
2 |
|
case 'uri': |
|
49
|
|
|
return $this->getParsedUrl($name, $transactionData->getRequest()); |
|
50
|
2 |
|
case 'get': |
|
51
|
1 |
|
return $transactionData->getRequest()->getQueryParams()[$name]; |
|
52
|
1 |
|
case 'header': |
|
53
|
|
|
return $transactionData->getRequest()->getHeader($name); |
|
54
|
1 |
|
case 'cookie': |
|
55
|
1 |
|
return $transactionData->getRequest()->getCookieParams()[$name]; |
|
56
|
|
|
case 'post': |
|
57
|
|
|
case 'bodyParam': |
|
58
|
|
|
return $this->getValueFromParsedBody($name, $transactionData->getRequest()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function getParsedUrl($name, ServerRequestInterface $request) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($name !== null) { |
|
65
|
|
|
return $this->getValueFromUrlPlaceholder($name, $request->getUri()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $request->getUri()->__toString(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getValueFromParsedBody($name, ServerRequestInterface $request) |
|
72
|
|
|
{ |
|
73
|
|
|
$data = $request->getParsedBody(); |
|
74
|
|
|
|
|
75
|
|
|
return is_array($data) ? $data[$name] : $data->$name; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|