From 5b45a0cdfdf440aa28298c914c4df32263046e95 Mon Sep 17 00:00:00 2001 From: Mohammadreza_73 Date: Sun, 5 Feb 2023 00:12:24 +0330 Subject: [PATCH] Add components: Add core files to components directory, Add response class. --- app/Core/{ => Components/Http}/Request.php | 8 +- app/Core/Components/Http/Response.php | 245 +++++++++++++++++++ app/Core/{ => Components}/Routing/Route.php | 2 +- app/Core/{ => Components}/Routing/Router.php | 6 +- bootstrap/init.php | 2 +- public/index.php | 2 +- routes/web.php | 2 +- 7 files changed, 258 insertions(+), 9 deletions(-) rename app/Core/{ => Components/Http}/Request.php (92%) create mode 100644 app/Core/Components/Http/Response.php rename app/Core/{ => Components}/Routing/Route.php (98%) rename app/Core/{ => Components}/Routing/Router.php (97%) diff --git a/app/Core/Request.php b/app/Core/Components/Http/Request.php similarity index 92% rename from app/Core/Request.php rename to app/Core/Components/Http/Request.php index f96a06e..34057ad 100644 --- a/app/Core/Request.php +++ b/app/Core/Components/Http/Request.php @@ -1,7 +1,11 @@ 'Continue', + 101 => 'Switching Protocols', + 102 => 'Processing', // RFC2518 + 103 => 'Early Hints', + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + 207 => 'Multi-Status', // RFC4918 + 208 => 'Already Reported', // RFC5842 + 226 => 'IM Used', // RFC3229 + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 307 => 'Temporary Redirect', + 308 => 'Permanent Redirect', // RFC7238 + 400 => 'Bad Request', + 401 => 'Unauthorized', + 402 => 'Payment Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Content Too Large', // RFC-ietf-httpbis-semantics + 414 => 'URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Range Not Satisfiable', + 417 => 'Expectation Failed', + 418 => 'I\'m a teapot', // RFC2324 + 421 => 'Misdirected Request', // RFC7540 + 422 => 'Unprocessable Content', // RFC-ietf-httpbis-semantics + 423 => 'Locked', // RFC4918 + 424 => 'Failed Dependency', // RFC4918 + 425 => 'Too Early', // RFC-ietf-httpbis-replay-04 + 426 => 'Upgrade Required', // RFC2817 + 428 => 'Precondition Required', // RFC6585 + 429 => 'Too Many Requests', // RFC6585 + 431 => 'Request Header Fields Too Large', // RFC6585 + 451 => 'Unavailable For Legal Reasons', // RFC7725 + 500 => 'Internal Server Error', + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported', + 506 => 'Variant Also Negotiates', // RFC2295 + 507 => 'Insufficient Storage', // RFC4918 + 508 => 'Loop Detected', // RFC5842 + 510 => 'Not Extended', // RFC2774 + 511 => 'Network Authentication Required', // RFC6585 + ]; + + public function __construct(?string $content = '', int $status = 200, string $headers = '') + { + $this->content = $content; + $this->status_code = $status; + $this->headers = $headers; + } + + public function __toString() + { + return sprintf('HTTP/%s %s %s', $this->version, $this->status_code, $this->status_text) . "\r\n" . + $this->headers . "\r\n" . + $this->getContent(); + } + + public function setStatusCode(int $status): Response + { + $this->status_code = $status; + + return $this; + } + + public function getStatusCode(): int + { + return $this->status_code; + } + + public function setCharset(string $charset): Response + { + $this->charset = $charset; + + return $this; + } + + public function getCharset(): string + { + return $this->charset; + } + + public function setContent(?string $content): Response + { + $this->content = $content ?? ''; + + return $this; + } + + public function getContent(): string + { + return $this->content; + } + + /** + * Json encode + * + * @param $data + * @throws RuntimeException + * @return string|null + */ + public function jsonEncode($data): ?string + { + return json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT); + } + + public function setHeader(string $header): Response + { + $this->headers = header("Content-Type: $header"); + + return $this; + } +} \ No newline at end of file diff --git a/app/Core/Routing/Route.php b/app/Core/Components/Routing/Route.php similarity index 98% rename from app/Core/Routing/Route.php rename to app/Core/Components/Routing/Route.php index 6dd9158..44bb252 100644 --- a/app/Core/Routing/Route.php +++ b/app/Core/Components/Routing/Route.php @@ -1,6 +1,6 @@ pushHandler(new \Whoops\Handler\PrettyPageHandler); $whoops->register(); -$request = new \App\Core\Request(); +$request = new \App\Core\Components\Http\Request(); include base_path('routes/web.php'); diff --git a/public/index.php b/public/index.php index 7e5aca5..058599a 100644 --- a/public/index.php +++ b/public/index.php @@ -2,7 +2,7 @@ require_once '../bootstrap/init.php'; -use App\Core\Routing\Router; +use App\Core\Components\Routing\Router; $router = new Router(); $router->run(); diff --git a/routes/web.php b/routes/web.php index ad03093..4ba4991 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,6 @@