|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Philae\Middlewares; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
|
6
|
|
|
use Middlewares\Utils; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Wraps the request handler callback in another one that will |
|
12
|
|
|
* try to convert the request handler result into a PSR-7 Response |
|
13
|
|
|
* and/or apply some changes to the response object itself |
|
14
|
|
|
* (like setting/fixing the Content Length header). |
|
15
|
|
|
*/ |
|
16
|
|
|
class ResponseTransformer extends AbstractMiddleware |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string Attribute name for handler reference |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $handlerAttribute = 'request-handler'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set the attribute name to store handler reference. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $handlerAttribute |
|
27
|
|
|
* |
|
28
|
|
|
* @return self |
|
29
|
|
|
*/ |
|
30
|
|
|
public function handlerAttribute(string $handlerAttribute) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->handlerAttribute = $handlerAttribute; |
|
33
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Process a server request and return a response. |
|
39
|
|
|
* |
|
40
|
|
|
* @param ServerRequestInterface $request |
|
41
|
|
|
* @param DelegateInterface $delegate |
|
42
|
|
|
* |
|
43
|
|
|
* @return ResponseInterface |
|
44
|
|
|
*/ |
|
45
|
15 |
|
public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
|
46
|
|
|
{ |
|
47
|
15 |
|
$handler = $request->getAttribute($this->handlerAttribute); |
|
48
|
|
|
|
|
49
|
15 |
|
if (!is_callable($handler)) { |
|
50
|
|
|
return $delegate->process($request); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
15 |
|
$handlerWrapper = function (...$args) use ($handler) { |
|
54
|
15 |
|
$result = call_user_func($handler, ...$args); |
|
55
|
|
|
|
|
56
|
15 |
|
$response = $this->transform($result); |
|
57
|
|
|
|
|
58
|
15 |
|
if (!($response instanceof ResponseInterface)) { |
|
59
|
3 |
|
$type = is_object($response) ? get_class($response) : gettype($response); |
|
60
|
3 |
|
throw new \UnexpectedValueException( |
|
61
|
3 |
|
'Cannot convert the request handler result of type `' . $type . '` into a Response object.' |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
12 |
|
return $response; |
|
66
|
15 |
|
}; |
|
67
|
|
|
|
|
68
|
15 |
|
$request = $request->withAttribute($this->handlerAttribute, $handlerWrapper); |
|
69
|
|
|
|
|
70
|
15 |
|
return $delegate->process($request); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Tries to transform the given result into a Response object, otherwise it is returned back. |
|
75
|
|
|
* @param mixed $result |
|
76
|
|
|
* @return ResponseInterface|mixed |
|
77
|
|
|
*/ |
|
78
|
15 |
|
public function transform($result) |
|
79
|
|
|
{ |
|
80
|
15 |
|
if ($result instanceof ResponseInterface) { |
|
81
|
3 |
|
return $result->withHeader('Content-Length', $result->getBody()->getSize()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
12 |
|
if (is_scalar($result) || is_null($result)) { |
|
85
|
9 |
|
$result = strval($result); |
|
86
|
3 |
|
} elseif (is_object($result) && method_exists($result, '__toString')) { |
|
87
|
|
|
$result = $result->__toString(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
12 |
|
if (!is_string($result)) { |
|
91
|
3 |
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
9 |
|
$response = Utils\Factory::createResponse((strlen($result) > 0) ? 200 : 204); |
|
95
|
9 |
|
$response->getBody()->write($result); |
|
96
|
|
|
|
|
97
|
9 |
|
return $response; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|