Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Suggestions / Revamp #3

Merged
merged 55 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
8628582
Renamed Router::mount() to Router::group()
CommandString Mar 7, 2023
046c47e
Use HttpSoft ServerRequestCreator
CommandString Mar 7, 2023
7595f65
Use match instead of switch
CommandString Mar 7, 2023
6137243
variable protection
CommandString Mar 7, 2023
d726a88
beforeMiddleware -> middleware & afterMiddleware -> postware
CommandString Mar 7, 2023
946681d
Install http-runner
CommandString Mar 7, 2023
5e4cbc9
Use HttpRunner for emitting routes
CommandString Mar 7, 2023
c9ee08c
Split HttpCodesApi example up
CommandString Mar 9, 2023
344d64e
Total API revamp
CommandString Mar 9, 2023
6056c00
Added interface for handling parts of a request
CommandString Mar 9, 2023
b09e8e0
Middleware object
CommandString Mar 9, 2023
8b12b77
Container for resolved route data
CommandString Mar 9, 2023
5a9faf3
README
CommandString Mar 9, 2023
9f83e8a
Removed unneeded packages
CommandString Mar 9, 2023
d1b774c
Fixed wrong Interface being used
CommandString Mar 10, 2023
abffd0d
No longer need to list all catchable classes
CommandString Mar 10, 2023
41d077b
Moved
CommandString Mar 10, 2023
bd20a7f
Removed extra line
CommandString Mar 10, 2023
b827e9d
I actually need httpsoft/http-response
CommandString Mar 10, 2023
6562075
Easier implementation of this class for custom exceptions
CommandString Mar 10, 2023
4071260
Fixed implementation check
CommandString Mar 10, 2023
c957617
Default Http response code is 500 & added checks for buildJsonRespons…
CommandString Mar 10, 2023
4f4b149
Added check to make sure controller implements RequestHandlerInterface
CommandString Mar 10, 2023
4047697
Updated docs
CommandString Mar 10, 2023
2097284
Finished updating docs
CommandString Mar 10, 2023
ee45497
Update README.md
CommandString Mar 10, 2023
cccc93c
Update examples/HttpCodesApi/ListCodes.php
CommandString Mar 10, 2023
9865658
Added extra line cause GH ate the other one
CommandString Mar 10, 2023
407b284
Merge branch 'exanlv/suggestions' of github.com:tnapf/router into exa…
CommandString Mar 10, 2023
78cc74c
Added extra line
CommandString Mar 10, 2023
75ad32d
HTML Moved
CommandString Mar 10, 2023
36322b1
Added namespace
CommandString Mar 10, 2023
4090ac4
Separated HTML from Exception logic
CommandString Mar 10, 2023
3171f18
Removed requires
CommandString Mar 10, 2023
28459bc
Added stricter check
CommandString Mar 10, 2023
3dfaf23
Better condition for phraseHtml
CommandString Mar 10, 2023
126599f
Added code sniffer
CommandString Mar 10, 2023
f1a4aa8
Coder sniffer fixes
CommandString Mar 10, 2023
d642dc8
Removed HttpCodesApi catcher
CommandString Mar 10, 2023
e772eeb
CS fixes
CommandString Mar 10, 2023
6b85716
Added type hinting
CommandString Mar 10, 2023
9e48937
Removed unnecessary if statement
CommandString Mar 11, 2023
e392c7c
Fixed class name
CommandString Mar 13, 2023
eb9e35f
Removed extra line
CommandString Mar 13, 2023
a481d53
PSR fix
CommandString Mar 13, 2023
c56200d
Moved logic out
CommandString Mar 13, 2023
70ed435
Using "
CommandString Mar 16, 2023
f04d922
Fixed HTTPVersionNotSupported name
CommandString Mar 16, 2023
f5f9953
Added scripts for generating exception docs / classes
CommandString Mar 16, 2023
480fe4f
Updated docs
CommandString Mar 16, 2023
bb0f297
Added index.php & HttpExceptions.md
CommandString Mar 16, 2023
cf8f46b
Added code sniffer
CommandString Mar 16, 2023
141e3ea
Removed / replaced next middleware handler
CommandString Mar 16, 2023
e90ae0e
PSR thing
CommandString Mar 16, 2023
db71137
Fixed awkward styling
CommandString Mar 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 171 additions & 189 deletions README.md

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
}
],
"require": {
"httpsoft/http-response": "^1.0",
"httpsoft/http-server-request": "^1.0",
"php": ">=8.1",
"commandstring/utils": "^1.4",
"httpsoft/http-emitter": "^1.0"
"httpsoft/http-emitter": "^1.0",
"httpsoft/http-response": "^1.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.7"
}
}
62 changes: 60 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 0 additions & 75 deletions examples/HttpCodesApi.php

This file was deleted.

38 changes: 38 additions & 0 deletions examples/HttpCodesApi/GetCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace HttpCodesApi;

use HttpSoft\Response\HtmlResponse;
use HttpSoft\Response\JsonResponse;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use Tnapf\Router\Exceptions\HttpNotFound;
use Tnapf\Router\Interfaces\RequestHandlerInterface;
use Tnapf\Router\Routing\Next;

class GetCode implements RequestHandlerInterface
{
public static function handle(
ServerRequestInterface $request,
ResponseInterface $response,
stdClass $args,
?Next $next = null
): ResponseInterface {
foreach (getCodes() as $code) {
if ($args->code === $code->code) {
if ($args->type === "json") {
return new JsonResponse($code);
} else {
CommandString marked this conversation as resolved.
Show resolved Hide resolved
ob_start();
require "./HtmlResponse.php";
$html = ob_get_clean();

return new HtmlResponse($html);
}
}
}

return throw new HttpNotFound($request);
}
}
31 changes: 31 additions & 0 deletions examples/HttpCodesApi/HtmlResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE HTML>
CommandString marked this conversation as resolved.
Show resolved Hide resolved
<html lang='en'>
<head>
<title><?= $code->code ?> - <?= $code->phrase ?></title>
</head>
<body>
<style>
* {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}

body {
background: #1b1c1d;
color: white;
padding-top: calc(50vh - 95px);
}

body>div {
max-width: 90%;
margin: auto;
width: fit-content;
}
</style>
<div>
<h1><?= $code->code ?> - <a href='<?= $code->mdn ?>'><?= $code->phrase ?></a></h1>
<hr>
<p><?= $code->description ?></p>
</div>
</body>
</html>
22 changes: 22 additions & 0 deletions examples/HttpCodesApi/ListCodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace HttpCodesApi;

use HttpSoft\Response\JsonResponse;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use Tnapf\Router\Interfaces\RequestHandlerInterface;
use Tnapf\Router\Routing\Next;

class ListCodes implements RequestHandlerInterface
{
public static function handle(
ServerRequestInterface $request,
ResponseInterface $response,
stdClass $args,
?Next $next = null
): ResponseInterface {
return new JsonResponse(getCodes());
}
}
23 changes: 23 additions & 0 deletions examples/HttpCodesApi/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use HttpCodesApi\GetCode;
use HttpCodesApi\ListCodes;
use Tnapf\Router\Router;

require_once "../../vendor/autoload.php";

function getCodes(): array
{
return json_decode(file_get_contents(__DIR__ . "/../../tools/HttpExceptions/HttpCodes.json"));
}

Router::get("/{type}/{code}", GetCode::class)
->setParameter("code", "[0-9]{3}")
->setParameter("type", "json|html")
;

Router::get("/", ListCodes::class);

Router::emitHttpExceptions(Router::EMIT_HTML_RESPONSE);

Router::run();
CommandString marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/Enums/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ enum Methods: string
case OPTIONS = "OPTIONS";
case HEAD = "HEAD";
case PATCH = "PATCH";
}
}
4 changes: 2 additions & 2 deletions src/Exceptions/HttpBadGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tnapf\Router\Exceptions;

class HttpBadGateway extends HttpException {
class HttpBadGateway extends HttpException
{
public const CODE = 502;
public const PHRASE = "Bad Gateway";
public const DESCRIPTION = "Indicates that the server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.";
public const HREF = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502";
}

CommandString marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions src/Exceptions/HttpBadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tnapf\Router\Exceptions;

class HttpBadRequest extends HttpException {
class HttpBadRequest extends HttpException
{
public const CODE = 400;
public const PHRASE = "Bad Request";
public const DESCRIPTION = "Indicates that the server cannot or will not process the request because the received syntax is invalid, nonsensical, or exceeds some limitation on what the server is willing to process.";
public const HREF = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400";
}

CommandString marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions src/Exceptions/HttpConflict.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Tnapf\Router\Exceptions;

class HttpConflict extends HttpException {
class HttpConflict extends HttpException
{
public const CODE = 409;
public const PHRASE = "Conflict";
public const DESCRIPTION = "Indicates that the request could not be completed due to a conflict with the current state of the resource.";
public const HREF = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409";
}

CommandString marked this conversation as resolved.
Show resolved Hide resolved
Loading