From a84f36f731071b39943d232b823578a37d3f1803 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 2 Oct 2024 19:32:38 +0000 Subject: [PATCH] Reorganised Http namespacing structure. --- VERSION | 2 +- .../BencodeHttpContent.php} | 22 ++++----- .../BencodeHttpContentHandler.php} | 12 ++--- .../FormContent.php => FormHttpContent.php} | 17 ++++--- ...orHandler.php => HtmlHttpErrorHandler.php} | 8 ++-- src/Http/{Content => }/HttpContent.php | 2 +- ...tentHandler.php => HttpContentHandler.php} | 8 ++-- .../ErrorHandler.php => HttpErrorHandler.php} | 8 ++-- src/Http/HttpMessage.php | 29 ++++++------ src/Http/HttpMessageBuilder.php | 5 +- src/Http/HttpRequest.php | 8 ++-- src/Http/HttpResponse.php | 2 - ...rHandler.php => PlainHttpErrorHandler.php} | 8 ++-- src/Http/Routing/HttpRouter.php | 46 ++++++++++--------- ...tringContent.php => StringHttpContent.php} | 22 ++++----- .../JsonHttpContent.php} | 21 +++++---- .../JsonHttpContentHandler.php} | 11 ++--- 17 files changed, 110 insertions(+), 121 deletions(-) rename src/{Http/Content/BencodedContent.php => Bencode/BencodeHttpContent.php} (68%) rename src/{Http/ContentHandling/BencodeContentHandler.php => Bencode/BencodeHttpContentHandler.php} (58%) rename src/Http/{Content/FormContent.php => FormHttpContent.php} (90%) rename src/Http/{ErrorHandling/HtmlErrorHandler.php => HtmlHttpErrorHandler.php} (85%) rename src/Http/{Content => }/HttpContent.php (86%) rename src/Http/{ContentHandling/ContentHandler.php => HttpContentHandler.php} (83%) rename src/Http/{ErrorHandling/ErrorHandler.php => HttpErrorHandler.php} (82%) rename src/Http/{ErrorHandling/PlainErrorHandler.php => PlainHttpErrorHandler.php} (70%) rename src/Http/{Content/StringContent.php => StringHttpContent.php} (65%) rename src/{Http/Content/JsonContent.php => Json/JsonHttpContent.php} (70%) rename src/{Http/ContentHandling/JsonContentHandler.php => Json/JsonHttpContentHandler.php} (67%) diff --git a/VERSION b/VERSION index 7ff7cf6..a1be6dd 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2410.21617 +0.2410.21928 diff --git a/src/Http/Content/BencodedContent.php b/src/Bencode/BencodeHttpContent.php similarity index 68% rename from src/Http/Content/BencodedContent.php rename to src/Bencode/BencodeHttpContent.php index 2a7a0b4..0ede0cb 100644 --- a/src/Http/Content/BencodedContent.php +++ b/src/Bencode/BencodeHttpContent.php @@ -1,17 +1,17 @@ hasContentType()) $response->setTypePlain(); - $response->setContent(new BencodedContent($content)); + $response->setContent(new BencodeHttpContent($content)); } } diff --git a/src/Http/Content/FormContent.php b/src/Http/FormHttpContent.php similarity index 90% rename from src/Http/Content/FormContent.php rename to src/Http/FormHttpContent.php index 3069e3f..6b1e842 100644 --- a/src/Http/Content/FormContent.php +++ b/src/Http/FormHttpContent.php @@ -1,17 +1,16 @@ $postFields Form fields. * @param array> $uploadedFiles Uploaded files. @@ -95,10 +94,10 @@ class FormContent implements HttpContent { * * @param array $post Form fields. * @param array $files Uploaded files. - * @return FormContent Instance representing the request body. + * @return FormHttpContent Instance representing the request body. */ - public static function fromRaw(array $post, array $files): FormContent { - return new FormContent( + public static function fromRaw(array $post, array $files): FormHttpContent { + return new FormHttpContent( $post, HttpUploadedFile::createFromPhpFiles($files) ); @@ -107,9 +106,9 @@ class FormContent implements HttpContent { /** * Creates an instance from the $_POST and $_FILES superglobals. * - * @return FormContent Instance representing the request body. + * @return FormHttpContent Instance representing the request body. */ - public static function fromRequest(): FormContent { + public static function fromRequest(): FormHttpContent { /** @var array $postVars */ $postVars = $_POST; diff --git a/src/Http/ErrorHandling/HtmlErrorHandler.php b/src/Http/HtmlHttpErrorHandler.php similarity index 85% rename from src/Http/ErrorHandling/HtmlErrorHandler.php rename to src/Http/HtmlHttpErrorHandler.php index b672ac4..34d6782 100644 --- a/src/Http/ErrorHandling/HtmlErrorHandler.php +++ b/src/Http/HtmlHttpErrorHandler.php @@ -1,16 +1,14 @@ diff --git a/src/Http/Content/HttpContent.php b/src/Http/HttpContent.php similarity index 86% rename from src/Http/Content/HttpContent.php rename to src/Http/HttpContent.php index 6f017c9..4d1af43 100644 --- a/src/Http/Content/HttpContent.php +++ b/src/Http/HttpContent.php @@ -3,7 +3,7 @@ // Created: 2022-02-08 // Updated: 2024-10-02 -namespace Index\Http\Content; +namespace Index\Http; use Stringable; diff --git a/src/Http/ContentHandling/ContentHandler.php b/src/Http/HttpContentHandler.php similarity index 83% rename from src/Http/ContentHandling/ContentHandler.php rename to src/Http/HttpContentHandler.php index 334f9ed..df81174 100644 --- a/src/Http/ContentHandling/ContentHandler.php +++ b/src/Http/HttpContentHandler.php @@ -1,16 +1,14 @@ content instanceof JsonContent; + return $this->content instanceof JsonHttpContent; } /** - * Checks if the body content is FormContent. + * Checks if the body content is FormHttpContent. * - * @return bool true if it is FormContent. + * @return bool true if it is FormHttpContent. */ public function isFormContent(): bool { - return $this->content instanceof FormContent; + return $this->content instanceof FormHttpContent; } /** - * Checks if the body content is StringContent. + * Checks if the body content is StringHttpContent. * - * @return bool true if it is StringContent. + * @return bool true if it is StringHttpContent. */ public function isStringContent(): bool { - return $this->content instanceof StringContent; + return $this->content instanceof StringHttpContent; } /** - * Checks if the body content is BencodedContent. + * Checks if the body content is BencodeHttpContent. * - * @return bool true if it is BencodedContent. + * @return bool true if it is BencodeHttpContent. */ - public function isBencodedContent(): bool { - return $this->content instanceof BencodedContent; + public function isBencodeContent(): bool { + return $this->content instanceof BencodeHttpContent; } } diff --git a/src/Http/HttpMessageBuilder.php b/src/Http/HttpMessageBuilder.php index 6aa89ca..b3d0e75 100644 --- a/src/Http/HttpMessageBuilder.php +++ b/src/Http/HttpMessageBuilder.php @@ -8,7 +8,6 @@ namespace Index\Http; use InvalidArgumentException; use RuntimeException; use Stringable; -use Index\Http\Content\{HttpContent,StringContent}; /** * Represents a base HTTP message builder. @@ -130,7 +129,7 @@ class HttpMessageBuilder { } if(is_scalar($content) || $content instanceof Stringable) { - $this->content = new StringContent((string)$content); + $this->content = new StringHttpContent((string)$content); return; } @@ -139,7 +138,7 @@ class HttpMessageBuilder { if($content === false) throw new RuntimeException('was unable to read the stream resource in $content'); - $this->content = new StringContent($content); + $this->content = new StringHttpContent($content); return; } diff --git a/src/Http/HttpRequest.php b/src/Http/HttpRequest.php index 99e5769..5d9a1c4 100644 --- a/src/Http/HttpRequest.php +++ b/src/Http/HttpRequest.php @@ -8,7 +8,7 @@ namespace Index\Http; use InvalidArgumentException; use RuntimeException; use Index\MediaType; -use Index\Http\Content\{HttpContent,FormContent,JsonContent,StringContent}; +use Index\Json\JsonHttpContent; /** * Represents a HTTP request message. @@ -182,12 +182,12 @@ class HttpRequest extends HttpMessage { if($contentType !== null && ($contentType->equals('application/json') || $contentType->equals('application/x-json'))) - $build->setContent(JsonContent::fromRequest()); + $build->setContent(JsonHttpContent::fromRequest()); elseif($contentType !== null && ($contentType->equals('application/x-www-form-urlencoded') || $contentType->equals('multipart/form-data'))) - $build->setContent(FormContent::fromRequest()); + $build->setContent(FormHttpContent::fromRequest()); elseif($contentLength > 0) - $build->setContent(StringContent::fromRequest()); + $build->setContent(StringHttpContent::fromRequest()); return $build->toRequest(); } diff --git a/src/Http/HttpResponse.php b/src/Http/HttpResponse.php index 6265f9f..c28ba5f 100644 --- a/src/Http/HttpResponse.php +++ b/src/Http/HttpResponse.php @@ -5,8 +5,6 @@ namespace Index\Http; -use Index\Http\Content\HttpContent; - /** * Represents a HTTP response message. */ diff --git a/src/Http/ErrorHandling/PlainErrorHandler.php b/src/Http/PlainHttpErrorHandler.php similarity index 70% rename from src/Http/ErrorHandling/PlainErrorHandler.php rename to src/Http/PlainHttpErrorHandler.php index bd65532..5a02aca 100644 --- a/src/Http/ErrorHandling/PlainErrorHandler.php +++ b/src/Http/PlainHttpErrorHandler.php @@ -1,16 +1,14 @@ setTypePlain(); $response->setContent(sprintf('HTTP %03d %s', $code, $message)); diff --git a/src/Http/Routing/HttpRouter.php b/src/Http/Routing/HttpRouter.php index ca28306..b63f8da 100644 --- a/src/Http/Routing/HttpRouter.php +++ b/src/Http/Routing/HttpRouter.php @@ -7,10 +7,12 @@ namespace Index\Http\Routing; use stdClass; use InvalidArgumentException; -use Index\Http\{HttpResponse,HttpResponseBuilder,HttpRequest}; -use Index\Http\Content\StringContent; -use Index\Http\ContentHandling\{BencodeContentHandler,ContentHandler,JsonContentHandler}; -use Index\Http\ErrorHandling\{HtmlErrorHandler,ErrorHandler,PlainErrorHandler}; +use Index\Bencode\BencodeHttpContentHandler; +use Index\Http\{ + HtmlHttpErrorHandler,HttpContentHandler,HttpErrorHandler,HttpResponse, + HttpResponseBuilder,HttpRequest,PlainHttpErrorHandler,StringHttpContent +}; +use Index\Json\JsonHttpContentHandler; class HttpRouter implements Router { use RouterTrait; @@ -24,19 +26,19 @@ class HttpRouter implements Router { /** @var array> */ private array $dynamicRoutes = []; - /** @var ContentHandler[] */ + /** @var HttpContentHandler[] */ private array $contentHandlers = []; - private ErrorHandler $errorHandler; + private HttpErrorHandler $errorHandler; /** * @param string $charSet Default character set to specify when none is present. - * @param ErrorHandler|string $errorHandler Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation. + * @param HttpErrorHandler|string $errorHandler Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation. * @param bool $registerDefaultContentHandlers true to register default content handlers for JSON, Bencode, etc. */ public function __construct( private string $charSet = '', - ErrorHandler|string $errorHandler = 'html', + HttpErrorHandler|string $errorHandler = 'html', bool $registerDefaultContentHandlers = true, ) { $this->setErrorHandler($errorHandler); @@ -65,19 +67,19 @@ class HttpRouter implements Router { /** * Retrieves the error handler instance. * - * @return ErrorHandler The error handler. + * @return HttpErrorHandler The error handler. */ - public function getErrorHandler(): ErrorHandler { + public function getErrorHandler(): HttpErrorHandler { return $this->errorHandler; } /** * Changes the active error handler. * - * @param ErrorHandler|string $handler Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation. + * @param HttpErrorHandler|string $handler Error handling to use for error responses with an empty body. 'html' for the default HTML implementation, 'plain' for the plaintext implementation. */ - public function setErrorHandler(ErrorHandler|string $handler): void { - if($handler instanceof ErrorHandler) + public function setErrorHandler(HttpErrorHandler|string $handler): void { + if($handler instanceof HttpErrorHandler) $this->errorHandler = $handler; elseif($handler === 'html') $this->setHTMLErrorHandler(); @@ -88,23 +90,23 @@ class HttpRouter implements Router { /** * Set the error handler to the basic HTML one. */ - public function setHTMLErrorHandler(): void { - $this->errorHandler = new HtmlErrorHandler; + public function setHtmlErrorHandler(): void { + $this->errorHandler = new HtmlHttpErrorHandler; } /** * Set the error handler to the plain text one. */ public function setPlainErrorHandler(): void { - $this->errorHandler = new PlainErrorHandler; + $this->errorHandler = new PlainHttpErrorHandler; } /** * Register a message body content handler. * - * @param ContentHandler $contentHandler Content handler to register. + * @param HttpContentHandler $contentHandler Content handler to register. */ - public function registerContentHandler(ContentHandler $contentHandler): void { + public function registerContentHandler(HttpContentHandler $contentHandler): void { if(!in_array($contentHandler, $this->contentHandlers)) $this->contentHandlers[] = $contentHandler; } @@ -113,8 +115,8 @@ class HttpRouter implements Router { * Register the default content handlers. */ public function registerDefaultContentHandlers(): void { - $this->registerContentHandler(new JsonContentHandler); - $this->registerContentHandler(new BencodeContentHandler); + $this->registerContentHandler(new JsonHttpContentHandler); + $this->registerContentHandler(new BencodeHttpContentHandler); } /** @@ -280,7 +282,7 @@ class HttpRouter implements Router { if(!$response->hasStatusCode() && $result >= 100 && $result < 600) $this->writeErrorPage($response, $request, $result); else - $response->setContent(new StringContent((string)$result)); + $response->setContent(new StringHttpContent((string)$result)); } elseif(!$response->hasContent()) { foreach($this->contentHandlers as $contentHandler) if($contentHandler->match($result)) { @@ -290,7 +292,7 @@ class HttpRouter implements Router { if(!$response->hasContent() && is_scalar($result)) { $result = (string)$result; - $response->setContent(new StringContent($result)); + $response->setContent(new StringHttpContent($result)); if(!$response->hasContentType()) { if(strtolower(substr($result, 0, 14)) === 'hasContentType()) $response->setTypeJson(); - $response->setContent(new JsonContent($content)); + $response->setContent(new JsonHttpContent($content)); } }