-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerRequestHandler.php
More file actions
125 lines (103 loc) · 3.12 KB
/
ControllerRequestHandler.php
File metadata and controls
125 lines (103 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace MaplePHP\Emitron;
use Closure;
use MaplePHP\Container\Reflection;
use MaplePHP\Http\StreamFactory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class ControllerRequestHandler implements RequestHandlerInterface
{
public function __construct(
private readonly ResponseFactoryInterface $factory,
private readonly array|Closure $controller,
private readonly ?Closure $call = null
)
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$response = $this->factory->createResponse();
$this->appendInterfaces([
"ResponseInterface" => $response,
]);
$controller = $this->controller;
if (is_callable($controller)) {
return $controller($request, $response);
}
if (!isset($controller[1])) {
$controller[1] = '__invoke';
}
if (count($controller) !== 2) {
$response->getBody()->write("ERROR: Invalid controller handler.\n");
return $response;
}
[$class, $method] = $controller;
if (!method_exists($class, $method)) {
$response->getBody()->write("ERROR: Could not load Controller {$class}::{$method}().\n");
return $response;
}
// Your DI wiring
$reflect = new Reflection($class);
$classInst = $reflect->dependencyInjector();
$call = $this->call;
if ($call !== null) {
$call($classInst, $response);
}
// This should INVOKE the method and return its result (ResponseInterface or something else)
$result = $reflect->dependencyInjector($classInst, $method);
return $this->createResponse($response, $result);
}
/**
* Will create a PSR valid Response instance form mixed result
*
* @param ResponseInterface $response
* @param mixed $result
* @return ResponseInterface
*/
protected function createResponse(ResponseInterface $response, mixed $result): ResponseInterface
{
if ($result instanceof ResponseInterface) {
return $result;
}
if ($result instanceof StreamInterface) {
return $response->withBody($result);
}
if (is_array($result) || is_object($result)) {
return $this->createStream($response, json_encode($result, JSON_UNESCAPED_UNICODE))
->withHeader("Content-Type", "application/json");
}
if (is_string($result) || is_numeric($result)) {
return $this->createStream($response, $result);
}
return $response;
}
/**
* A helper method to create a new stream instance
*
* @param ResponseInterface $response
* @param mixed $result
* @return ResponseInterface
*/
protected function createStream(ResponseInterface $response, mixed $result): ResponseInterface
{
$streamFactory = new StreamFactory();
$stream = $streamFactory->createStream($result);
return $response->withBody($stream);
}
/**
* Append interface helper method
*
* @param array $bindings
* @return void
*/
protected function appendInterfaces(array $bindings)
{
Reflection::interfaceFactory(function (string $className) use ($bindings) {
return $bindings[$className] ?? null;
});
}
}