Skip to content

Commit a624e24

Browse files
committed
refactor: Clean up and code refeactor
1 parent c58af99 commit a624e24

5 files changed

Lines changed: 93 additions & 59 deletions

File tree

src/AbstractConfigProps.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ abstract class AbstractConfigProps implements ConfigPropsInterface
1414
public ?string $path = null;
1515
public ?string $test = null;
1616

17+
private array $propDesc = [];
18+
private static array $childPropCache = [];
19+
1720
/**
1821
* Hydrate the properties/object with expected data, and handle unexpected data
1922
*
@@ -66,6 +69,32 @@ public function setProp(string $key, mixed $value): self
6669
return $this;
6770
}
6871

72+
73+
/**
74+
* Add description to prop
75+
*
76+
* @param string $key
77+
* @param string $desc
78+
* @return $this
79+
*/
80+
protected function setPropDesc(string $key, string $desc): self
81+
{
82+
$this->propDesc[$key] = $desc;
83+
return $this;
84+
}
85+
86+
/**
87+
* Get description to prop
88+
*
89+
* @param string $key
90+
* @param string $desc
91+
* @return string
92+
*/
93+
public function getPropDesc(string $key): string
94+
{
95+
return $this->propDesc[$key] ?? "";
96+
}
97+
6998
/**
7099
* Set multiple config props
71100
*
@@ -101,15 +130,22 @@ public function get(string $key): mixed
101130
return ($newKey !== false) ? $this->{$newKey} : null;
102131
}
103132

104-
/**
105-
* Return props object as array
106-
*
107-
* @return array
108-
*/
109-
public function toArray(): array
110-
{
111-
return get_object_vars($this);
112-
}
133+
/**
134+
* Return public properties defined on the concrete class
135+
*/
136+
public function toArray(): array
137+
{
138+
$vars = get_object_vars($this);
139+
140+
if (!isset(self::$childPropCache[static::class])) {
141+
$childDefaults = get_class_vars(static::class);
142+
$baseDefaults = get_class_vars(self::class);
143+
144+
self::$childPropCache[static::class] = array_diff_key($childDefaults, $baseDefaults);
145+
}
146+
147+
return array_intersect_key($vars, self::$childPropCache[static::class]);
148+
}
113149

114150
/**
115151
* Get value as bool value

src/Emitters/HttpEmitter.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ class HttpEmitter implements EmitterInterface
2222
*/
2323
public function emit(ResponseInterface $response, ServerRequestInterface $request): void
2424
{
25-
2625
$body = $response->getBody();
2726
$status = $response->getStatusCode();
2827
$method = strtoupper($request->getMethod());
29-
$skipBody = in_array($status, [204, 304]) || ($status >= 100 && $status < 200) || $method === 'HEAD';
30-
31-
// Default to 204 No Content if nobody was written
32-
if (!$response->getBody()->getSize() && $this->isSuccessfulResponse($response)) {
33-
$response = $response->withStatus(204);
34-
$response->getBody()->write("No Content\n");
35-
}
28+
$skipBody = in_array($status, [204, 304], true)
29+
|| ($status >= 100 && $status < 200)
30+
|| $method === 'HEAD';
3631

32+
// Create headers
3733
$this->createHeaders($response);
38-
if (!$skipBody && $body->isSeekable()) {
39-
$body->rewind();
40-
}
41-
echo $skipBody ? '' : $body->getContents();
34+
35+
// Detach body if HEAD or other detachable status code
36+
if (!$skipBody) {
37+
if ($body->isSeekable()) {
38+
$body->rewind();
39+
}
40+
echo $body->getContents();
41+
}
4242
}
4343

4444
/**

src/Enums/DispatchCodes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MaplePHP\Emitron\Enums;
4+
5+
enum DispatchCodes: int
6+
{
7+
case FOUND = 1;
8+
case NOT_FOUND = 0;
9+
case METHOD_NOT_ALLOWED = 2;
10+
}

src/Kernel.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515
namespace MaplePHP\Emitron;
1616

17-
use MaplePHP\Http\ResponseFactory;
17+
use Psr\Http\Message\ResponseInterface;
1818
use Psr\Http\Message\ServerRequestInterface;
1919
use Psr\Http\Message\StreamInterface;
20-
use MaplePHP\Log\InvalidArgumentException;
20+
use MaplePHP\Emitron\Enums\DispatchCodes;
21+
use MaplePHP\Http\ResponseFactory;
2122

2223
class Kernel extends AbstractKernel
2324
{
@@ -30,18 +31,19 @@ class Kernel extends AbstractKernel
3031
*/
3132
public function run(ServerRequestInterface $request, ?StreamInterface $stream = null): void
3233
{
33-
3434
$this->dispatchConfig->getRouter()->dispatch(function ($data, $args, $middlewares) use ($request, $stream) {
3535

36-
//$dispatchCode = $data[0] ?? RouterDispatcher::FOUND;
3736

37+
$dispatchCode = (int)($data[0] ?? DispatchCodes::FOUND->value);
38+
if($dispatchCode !== DispatchCodes::FOUND->value) {
39+
$data['handler'] = function (ServerRequestInterface $req, ResponseInterface $res): ResponseInterface
40+
{
41+
return $res->withStatus(404);
42+
};
43+
}
44+
//$dispatchCode = $data[0] ?? RouterDispatcher::FOUND;
3845
[$data, $args, $middlewares] = $this->reMap($data, $args, $middlewares);
3946

40-
if (!isset($data['handler'])) {
41-
throw new InvalidArgumentException("Missing 'handler' key.");
42-
}
43-
44-
4547
$this->container->set("request", $request);
4648
$this->container->set("args", $args);
4749
$this->container->set("configuration", $this->getDispatchConfig());
@@ -50,24 +52,13 @@ public function run(ServerRequestInterface $request, ?StreamInterface $stream =
5052
$factory = new ResponseFactory($bodyStream);
5153
$finalHandler = new ControllerRequestHandler($factory, $data['handler'] ?? []);
5254

53-
5455
$response = $this->initRequestHandler(
5556
request: $request,
5657
stream: $bodyStream,
5758
finalHandler: $finalHandler,
5859
middlewares: $middlewares
5960
);
6061

61-
/*
62-
if ($dispatchCode === Dispatcher::NOT_FOUND) {
63-
$response = $response->withStatus(404);
64-
}
65-
66-
if ($dispatchCode === Dispatcher::METHOD_NOT_ALLOWED) {
67-
$response = $response->withStatus(405);
68-
}
69-
*/
70-
7162
$this->createEmitter()->emit($response, $request);
7263
});
7364
}
@@ -84,6 +75,10 @@ function reMap($data, $args, $middlewares)
8475
if (!is_array($middlewares)) {
8576
$middlewares = [];
8677
}
78+
79+
if (!is_array($args)) {
80+
$args = [];
81+
}
8782
return [$data, $args, $middlewares];
8883
}
8984
}

src/Middlewares/HeadRequestMiddleware.php

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

55
namespace MaplePHP\Emitron\Middlewares;
66

7+
use MaplePHP\Http\StreamFactory;
78
use Psr\Http\Server\MiddlewareInterface;
89
use Psr\Http\Server\RequestHandlerInterface;
910
use Psr\Http\Message\ResponseInterface;
@@ -12,27 +13,19 @@
1213
class HeadRequestMiddleware implements MiddlewareInterface
1314
{
1415
/**
15-
* Set cache control if it does not exist
16-
*
17-
* Note: Clearing cache on dynamic content is a good standard to make sure that no
18-
* sensitive content will be cached.
16+
* Detach body on HEAD
1917
*
2018
* @param ServerRequestInterface $request
2119
* @param RequestHandlerInterface $handler
2220
* @return ResponseInterface
2321
*/
24-
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
25-
{
26-
$response = $handler->handle($request);
27-
28-
if (strtoupper($request->getMethod()) === 'HEAD') {
29-
$body = $response->getBody();
30-
if ($body->isWritable() && $body->isSeekable()) {
31-
$body->rewind();
32-
$body->write('');
33-
}
34-
}
35-
36-
return $response;
37-
}
22+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
23+
{
24+
$response = $handler->handle($request);
25+
if (strtoupper($request->getMethod()) !== 'HEAD') {
26+
return $response;
27+
}
28+
$streamFactory = new StreamFactory();
29+
return $response->withBody($streamFactory->createStream());
30+
}
3831
}

0 commit comments

Comments
 (0)