1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CakeDC\Api\Middleware; |
4
|
|
|
|
5
|
|
|
use CakeDC\Api\Service\ConfigReader; |
6
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
7
|
|
|
use Cake\Core\Configure; |
8
|
|
|
use Cake\Http\RequestTransformer; |
9
|
|
|
use Cake\Http\ResponseTransformer; |
10
|
|
|
use Cake\Routing\Exception\RedirectException; |
11
|
|
|
use Cake\Routing\Router; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Zend\Diactoros\Response\RedirectResponse; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Applies routing rules to the request and creates the controller |
18
|
|
|
* instance if possible. |
19
|
|
|
*/ |
20
|
|
|
class RequestHandlerMiddleware |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Request object |
26
|
|
|
* |
27
|
|
|
* @var \Cake\Network\Request |
28
|
|
|
*/ |
29
|
|
|
public $request; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request The request. |
33
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response The response. |
34
|
|
|
* @param callable $next The next middleware to call. |
35
|
|
|
* @return \Psr\Http\Message\ResponseInterface A response. |
36
|
|
|
*/ |
37
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) |
38
|
|
|
{ |
39
|
|
|
$inputTypeMap = [ |
40
|
|
|
'json' => ['json_decode', true], |
41
|
|
|
'xml' => [[$this, 'convertXml']], |
42
|
|
|
]; |
43
|
|
|
$this->request = RequestTransformer::toCake($request); |
44
|
|
|
$this->response = ResponseTransformer::toCake($response); |
|
|
|
|
45
|
|
|
$parsedBody = $request->getParsedBody(); |
46
|
|
|
|
47
|
|
|
foreach ($inputTypeMap as $type => $handler) { |
48
|
|
|
if (!is_callable($handler[0])) { |
49
|
|
|
throw new RuntimeException(sprintf("Invalid callable for '%s' type.", $type)); |
50
|
|
|
} |
51
|
|
|
if (empty($parsedBody) && $this->requestedWith($type)) { |
52
|
|
|
$input = call_user_func_array([$this->request, 'input'], $handler); |
53
|
|
|
return $next($request->withParsedBody($input), $response); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $next($request, $response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Determines the content type of the data the client has sent (i.e. in a POST request) |
63
|
|
|
* |
64
|
|
|
* @param string|array|null $type Can be null (or no parameter), a string type name, or an array of types |
65
|
|
|
* @return mixed If a single type is supplied a boolean will be returned. If no type is provided |
66
|
|
|
* The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type |
67
|
|
|
* in the request content type will be returned. |
68
|
|
|
*/ |
69
|
|
|
public function requestedWith($type = null) |
70
|
|
|
{ |
71
|
|
|
$request = $this->request; |
72
|
|
|
if (!$request->is('post') && |
73
|
|
|
!$request->is('put') && |
74
|
|
|
!$request->is('patch') && |
75
|
|
|
!$request->is('delete') |
76
|
|
|
) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
if (is_array($type)) { |
80
|
|
|
foreach ($type as $t) { |
81
|
|
|
if ($this->requestedWith($t)) { |
82
|
|
|
return $t; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
list($contentType) = explode(';', $request->contentType()); |
90
|
|
|
$response = $this->response; |
91
|
|
|
if ($type === null) { |
92
|
|
|
return $response->mapType($contentType); |
93
|
|
|
} |
94
|
|
|
if (is_string($type)) { |
95
|
|
|
return ($type === $response->mapType($contentType)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Helper method to parse xml input data, due to lack of anonymous functions |
101
|
|
|
* this lives here. |
102
|
|
|
* |
103
|
|
|
* @param string $xml XML string. |
104
|
|
|
* @return array Xml array data |
105
|
|
|
*/ |
106
|
|
|
public function convertXml($xml) |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
$xml = Xml::build($xml, ['readFile' => false]); |
110
|
|
|
if (isset($xml->data)) { |
111
|
|
|
return Xml::toArray($xml->data); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return Xml::toArray($xml); |
115
|
|
|
} catch (XmlException $e) { |
|
|
|
|
116
|
|
|
return []; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: