Skip to content

Commit

Permalink
v5
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Sep 18, 2022
1 parent 63d5c63 commit 88a6253
Show file tree
Hide file tree
Showing 18 changed files with 369 additions and 477 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ A minimal, highly [performant][1] middleware [PSR-15][8] microframework built wi
## Requirements

* php: ^8.0
* [psr/container][20]: ^1.0|^2.0
* [psr/http-factory][21]: ^1.0.1
* [psr/http-message-implementation][22]: ^1.0
* [psr/http-message][23]: ^1.0.1
* [psr/http-server-handler][24]: ^1.0.1
* [psr/http-server-middleware][25]: ^1.0.1
* [psr/log][25]: ^1.1
* [chubbyphp/chubbyphp-http-exception][20]: ^1.0
* [fig/http-message-util][21]: ^1.1.5
* [psr/container][22]: ^1.0|^2.0
* [psr/http-factory][23]: ^1.0.1
* [psr/http-message][24]: ^1.0.1
* [psr/http-message-implementation][25]: ^1.0
* [psr/http-server-handler][26]: ^1.0.1
* [psr/http-server-middleware][27]: ^1.0.1
* [psr/log][28]: ^1.1

## Suggest

Expand Down Expand Up @@ -178,13 +180,15 @@ Dominik Zogg 2022

[15]: https://travis-ci.org/chubbyphp/chubbyphp-framework

[20]: https://packagist.org/packages/psr/container
[21]: https://packagist.org/packages/psr/http-factory
[22]: https://packagist.org/packages/psr/http-message-implementation
[23]: https://packagist.org/packages/psr/http-message
[24]: https://packagist.org/packages/psr/http-server-handler
[25]: https://packagist.org/packages/psr/http-server-middleware
[26]: https://packagist.org/packages/psr/log
[20]: https://packagist.org/packages/chubbyphp/chubbyphp-http-exception
[21]: https://packagist.org/packages/fig/http-message-util
[22]: https://packagist.org/packages/psr/container
[23]: https://packagist.org/packages/psr/http-factory
[24]: https://packagist.org/packages/psr/http-message
[25]: https://packagist.org/packages/psr/http-message-implementation
[26]: https://packagist.org/packages/psr/http-server-handler
[27]: https://packagist.org/packages/psr/http-server-middleware
[28]: https://packagist.org/packages/psr/log

[30]: https://github.com/chubbyphp/chubbyphp-framework-router-fastroute#usage

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
],
"require": {
"php": "^8.0",
"chubbyphp/chubbyphp-http-exception": "^1.0",
"fig/http-message-util": "^1.1.5",
"psr/container": "^1.0|^2.0",
"psr/http-factory": "^1.0.1",
"psr/http-message-implementation": "^1.0",
"psr/http-message": "^1.0.1",
"psr/http-message-implementation": "^1.0",
"psr/http-server-handler": "^1.0.1",
"psr/http-server-middleware": "^1.0.1",
"psr/log": "^1.1.4|^2.0|^3.0"
Expand Down
36 changes: 36 additions & 0 deletions src/Middleware/ExceptionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Chubbyphp\Framework\Middleware;

use Chubbyphp\HttpException\HttpExceptionInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -74,6 +75,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

private function handleException(\Throwable $exception): ResponseInterface
{
if ($exception instanceof HttpExceptionInterface) {
return $this->handleHttpException($exception);
}

$exceptionsData = $this->toExceptionArray($exception);

$this->logger->error('Exception', ['exceptions' => $exceptionsData]);
Expand All @@ -96,6 +101,37 @@ private function handleException(\Throwable $exception): ResponseInterface
return $response;
}

private function handleHttpException(HttpExceptionInterface $exception): ResponseInterface
{
$data = $exception->jsonSerialize();

$logMethod = $exception->getStatus() < 500 ? 'info' : 'error';

$this->logger->{$logMethod}('Http Exception', [
'data' => $data,
'exceptions' => $this->toExceptionArray($exception),
]);

$lines = [
...(isset($data['detail']) ? [$data['detail']] : []),
...(isset($data['instance']) ? [$data['instance']] : []),
];

$response = $this->responseFactory->createResponse($exception->getStatus());
$response = $response->withHeader('Content-Type', 'text/html');
$response->getBody()->write(sprintf(
self::HTML,
$exception->getTitle(),
sprintf(
'<h1>%s</h1>%s',
$exception->getTitle(),
implode('', array_map(static fn ($line) => sprintf('<p>%s</p>', $line), $lines)),
)
));

return $response;
}

/**
* @return array<int, array<string, mixed>>
*/
Expand Down
79 changes: 3 additions & 76 deletions src/Middleware/RouteMatcherMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,21 @@

namespace Chubbyphp\Framework\Middleware;

use Chubbyphp\Framework\Router\Exceptions\HttpExceptionInterface;
use Chubbyphp\Framework\Router\RouteMatcherInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class RouteMatcherMiddleware implements MiddlewareInterface
{
private const HTML = <<<'EOT'
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>%s</title>
<style>
body {
margin: 0;
padding: 30px;
font: 12px/1.5 Helvetica, Arial, Verdana, sans-serif;
}
h1 {
margin: 0;
font-size: 48px;
font-weight: normal;
line-height: 48px;
}
.block {
margin-bottom: 20px;
}
.key {
width: 100px;
display: inline-flex;
}
.value {
display: inline-flex;
}
</style>
</head>
<body>
%s
</body>
</html>
EOT;

private LoggerInterface $logger;

public function __construct(
private RouteMatcherInterface $routeMatcher,
private ResponseFactoryInterface $responseFactory,
?LoggerInterface $logger = null
) {
$this->logger = $logger ?? new NullLogger();
public function __construct(private RouteMatcherInterface $routeMatcher)
{
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$route = $this->routeMatcher->match($request);
} catch (HttpExceptionInterface $routerException) {
return $this->routeExceptionResponse($routerException);
}

$route = $this->routeMatcher->match($request);
$request = $request->withAttribute('route', $route);

foreach ($route->getAttributes() as $attribute => $value) {
Expand All @@ -81,23 +27,4 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

return $handler->handle($request);
}

private function routeExceptionResponse(HttpExceptionInterface $routerException): ResponseInterface
{
$this->logger->info('Route exception', [
'title' => $routerException->getTitle(),
'message' => $routerException->getMessage(),
'code' => $routerException->getCode(),
]);

$response = $this->responseFactory->createResponse($routerException->getCode());
$response = $response->withHeader('Content-Type', 'text/html');
$response->getBody()->write(sprintf(
self::HTML,
$routerException->getTitle(),
'<h1>'.$routerException->getTitle().'</h1>'.'<p>'.$routerException->getMessage().'</p>'
));

return $response;
}
}
22 changes: 0 additions & 22 deletions src/Router/Exceptions/HttpExceptionInterface.php

This file was deleted.

46 changes: 0 additions & 46 deletions src/Router/Exceptions/MethodNotAllowedException.php

This file was deleted.

12 changes: 1 addition & 11 deletions src/Router/Exceptions/MissingRouteByNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@

final class MissingRouteByNameException extends RouterException
{
private string $name;

private function __construct(string $message, int $code, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

public static function create(string $name): self
{
$self = new self(sprintf('Missing route: "%s"', $name), 2);
$self->name = $name;

return $self;
}

public function getName(): string
{
return $this->name;
return new self(sprintf('Missing route: "%s"', $name), 2);
}
}
42 changes: 0 additions & 42 deletions src/Router/Exceptions/NotFoundException.php

This file was deleted.

0 comments on commit 88a6253

Please sign in to comment.