Skip to content

Commit 6ccf2d1

Browse files
authored
Merge pull request #3 from maplephp/develop
Add callable that is passed to controller request handler
2 parents 1fa2c7e + 719f819 commit 6ccf2d1

File tree

2 files changed

+163
-156
lines changed

2 files changed

+163
-156
lines changed

src/ControllerRequestHandler.php

Lines changed: 108 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MaplePHP\Emitron;
66

7+
use Closure;
78
use MaplePHP\Container\Reflection;
89
use MaplePHP\Http\StreamFactory;
910
use Psr\Http\Message\ResponseFactoryInterface;
@@ -14,105 +15,111 @@
1415

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

src/Kernel.php

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,69 @@
2323

2424
class Kernel extends AbstractKernel
2525
{
26-
/**
27-
* Run the emitter and init all routes, middlewares and configs
28-
*
29-
* @param ServerRequestInterface $request
30-
* @param StreamInterface|null $stream
31-
* @return void
32-
*/
33-
public function run(ServerRequestInterface $request, ?StreamInterface $stream = null): void
34-
{
35-
$this->dispatchConfig->getRouter()->dispatch(function ($data, $args, $middlewares) use ($request, $stream) {
26+
/**
27+
* Run the emitter and init all routes, middlewares and configs
28+
*
29+
* @param ServerRequestInterface $request
30+
* @param StreamInterface|null $stream
31+
* @param callable|null $call
32+
* @return void
33+
*/
34+
public function run(ServerRequestInterface $request, ?StreamInterface $stream = null, ?callable $call = null): void
35+
{
36+
$this->dispatchConfig->getRouter()->dispatch(function ($data, $args, $middlewares) use ($request, $stream, $call) {
3637

37-
if($args === null) {
38-
$args = $request->getCliArgs();
39-
}
38+
if ($args === null) {
39+
$args = $request->getCliArgs();
40+
}
4041

41-
$parts = isset($data[2]) && is_array($data[2]) ? $data[2] : [];
42-
$dispatchCode = (int)($data[0] ?? DispatchCodes::FOUND->value);
42+
$parts = isset($data[2]) && is_array($data[2]) ? $data[2] : [];
43+
$dispatchCode = (int)($data[0] ?? DispatchCodes::FOUND->value);
4344

44-
if($dispatchCode !== DispatchCodes::FOUND->value) {
45-
$data['handler'] = function (ServerRequestInterface $req, ResponseInterface $res): ResponseInterface
46-
{
47-
return $res->withStatus(404);
48-
};
49-
}
50-
//$dispatchCode = $data[0] ?? RouterDispatcher::FOUND;
51-
[$data, $args, $middlewares] = $this->reMap($data, $args, $middlewares);
45+
if ($dispatchCode !== DispatchCodes::FOUND->value) {
46+
$data['handler'] = function (ServerRequestInterface $req, ResponseInterface $res): ResponseInterface {
47+
return $res->withStatus(404);
48+
};
49+
}
50+
//$dispatchCode = $data[0] ?? RouterDispatcher::FOUND;
51+
[$data, $args, $middlewares] = $this->reMap($data, $args, $middlewares);
5252

53-
$this->container->set("request", $request);
54-
$this->container->set("args", $args);
55-
$this->container->set("configuration", $this->getDispatchConfig());
53+
$this->container->set("request", $request);
54+
$this->container->set("args", $args);
55+
$this->container->set("configuration", $this->getDispatchConfig());
5656

57-
$bodyStream = $this->getBody($stream);
58-
$factory = new ResponseFactory($bodyStream);
59-
$finalHandler = new ControllerRequestHandler($factory, $data['handler'] ?? []);
57+
$bodyStream = $this->getBody($stream);
58+
$factory = new ResponseFactory($bodyStream);
59+
$finalHandler = new ControllerRequestHandler($factory, $data['handler'] ?? [], $call);
6060
$path = new Path($parts, $request);
6161

62-
63-
$response = $this->initRequestHandler(
64-
request: $request,
65-
stream: $bodyStream,
62+
$response = $this->initRequestHandler(
63+
request: $request,
64+
stream: $bodyStream,
6665
path: $path,
67-
finalHandler: $finalHandler,
68-
middlewares: $middlewares
69-
);
70-
$this->createEmitter()->emit($response, $request);
71-
});
72-
}
66+
finalHandler: $finalHandler,
67+
middlewares: $middlewares
68+
);
69+
70+
$this->createEmitter()->emit($response, $request);
71+
});
72+
}
7373

7474

75-
function reMap($data, $args, $middlewares)
76-
{
77-
if (isset($data[1]) && $middlewares instanceof ServerRequestInterface) {
78-
$item = $data[1];
79-
return [
80-
["handler" => $item['controller']], ($args == null ? $_REQUEST : $args), ($item['data'] ?? [])
81-
];
82-
}
83-
if (!is_array($middlewares)) {
84-
$middlewares = [];
85-
}
86-
if (!is_array($args)) {
87-
$args = [];
88-
}
89-
return [$data, $args, $middlewares];
90-
}
75+
function reMap($data, $args, $middlewares)
76+
{
77+
if (isset($data[1]) && $middlewares instanceof ServerRequestInterface) {
78+
$item = $data[1];
79+
return [
80+
["handler" => $item['controller']], ($args == null ? $_REQUEST : $args), ($item['data'] ?? [])
81+
];
82+
}
83+
if (!is_array($middlewares)) {
84+
$middlewares = [];
85+
}
86+
if (!is_array($args)) {
87+
$args = [];
88+
}
89+
return [$data, $args, $middlewares];
90+
}
9191
}

0 commit comments

Comments
 (0)