1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flatdown\Middlewares; |
4
|
|
|
|
5
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
6
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
7
|
|
|
use Middlewares\Utils; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Wraps the request handler callback in another one that will |
13
|
|
|
* try to convert the handler result into a string. |
14
|
|
|
* |
15
|
|
|
* By default arrays and objects will be converted to JSON. |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class ResultConverter implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string Attribute name for handler reference |
22
|
|
|
*/ |
23
|
|
|
private $attribute = 'request-handler'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var callable Converter for non scalar values |
27
|
|
|
*/ |
28
|
|
|
private $converter; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set the attribute name to store handler reference. |
32
|
|
|
* |
33
|
|
|
* @param string $attribute |
34
|
|
|
* |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function attribute($attribute) |
38
|
|
|
{ |
39
|
|
|
$this->attribute = $attribute; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Replaces the default converter |
46
|
|
|
* |
47
|
|
|
* @param callable $converter Converter for non scalar values |
48
|
|
|
* |
49
|
|
|
* @return self |
50
|
|
|
*/ |
51
|
|
|
public function converter(callable $converter) |
52
|
|
|
{ |
53
|
|
|
$this->converter = $converter; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Process a server request and return a response. |
60
|
|
|
* |
61
|
|
|
* @param ServerRequestInterface $request |
62
|
|
|
* @param DelegateInterface $delegate |
63
|
|
|
* |
64
|
|
|
* @return ResponseInterface |
65
|
|
|
*/ |
66
|
12 |
|
public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
67
|
|
|
{ |
68
|
12 |
|
$handler = $request->getAttribute($this->attribute); |
69
|
|
|
|
70
|
12 |
|
if (!is_callable($handler)) { |
71
|
|
|
return $delegate->process($request); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$converter = $this->converter ?: function ($value) { |
75
|
|
|
if ((is_array($value) || is_object($value)) && !($value instanceof \Closure)) { |
76
|
|
|
$response = Utils\Factory::createResponse(200); |
77
|
|
|
$response->getBody()->write(json_encode($value)); |
78
|
|
|
|
79
|
|
|
return $response->withHeader('content-type', 'application/json'); |
80
|
|
|
} |
81
|
|
|
throw new \RuntimeException("Incompatible return value returned in the route handler."); |
82
|
12 |
|
}; |
83
|
|
|
|
84
|
12 |
|
$handlerWrapper = function (ServerRequestInterface $request, ...$args) use ($handler, $converter) { |
85
|
12 |
|
$result = call_user_func($handler, $request, ...$args); |
86
|
|
|
|
87
|
9 |
|
if (($result instanceof ResponseInterface) || is_scalar($result)) { |
88
|
9 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (is_object($result) && method_exists($result, '__toString')) { |
92
|
|
|
return $result->__toString(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $converter($result); |
96
|
12 |
|
}; |
97
|
|
|
|
98
|
12 |
|
$request = $request->withAttribute($this->attribute, $handlerWrapper); |
99
|
|
|
|
100
|
12 |
|
return $delegate->process($request); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|