Skip to content

Commit

Permalink
Merge 17aee5b into dcf65c7
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed May 11, 2021
2 parents dcf65c7 + 17aee5b commit 28613a0
Show file tree
Hide file tree
Showing 17 changed files with 565 additions and 136 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ namespace App;

use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\Middleware\ExceptionMiddleware;
use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\Middleware\UrlMatcherMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\Router\FastRoute\Router;
use Chubbyphp\Framework\Router\FastRoute\UrlMatcher;
use Chubbyphp\Framework\Router\Route;
use Chubbyphp\Framework\Router\Routes;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
Expand All @@ -101,7 +102,7 @@ $responseFactory = new ResponseFactory();

$app = new Application([
new ExceptionMiddleware($responseFactory, true),
new RouterMiddleware(new Router([
new UrlMatcherMiddleware(new UrlMatcher(new Routes([
Route::get('/hello/{name:[a-z]+}', 'hello', new CallbackRequestHandler(
static function (ServerRequestInterface $request) use ($responseFactory) {
$response = $responseFactory->createResponse();
Expand All @@ -110,7 +111,7 @@ $app = new Application([
return $response;
}
))
]), $responseFactory),
])), $responseFactory),
]);

$app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));
Expand All @@ -127,9 +128,9 @@ $app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));
* [LazyMiddleware][72]
* [MiddlewareDispatcher][73]
* [NewRelicRouteMiddleware][74]
* [RouterMiddleware][75]
* [SlimCallbackMiddleware][76]
* [SlimLazyMiddleware][77]
* [SlimCallbackMiddleware][75]
* [SlimLazyMiddleware][76]
* [UrlMatcherMiddleware][77]

### RequestHandler

Expand Down Expand Up @@ -213,9 +214,9 @@ Dominik Zogg 2021
[72]: doc/Middleware/LazyMiddleware.md
[73]: doc/Middleware/MiddlewareDispatcher.md
[74]: doc/Middleware/NewRelicRouteMiddleware.md
[75]: doc/Middleware/RouterMiddleware.md
[76]: doc/Middleware/SlimCallbackMiddleware.md
[77]: doc/Middleware/SlimLazyMiddleware.md
[75]: doc/Middleware/SlimCallbackMiddleware.md
[76]: doc/Middleware/SlimLazyMiddleware.md
[77]: doc/Middleware/UrlMatcherMiddleware.md

[80]: doc/RequestHandler/CallbackRequestHandler.md
[81]: doc/RequestHandler/LazyRequestHandler.md
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RouterMiddleware
# UrlMatcherMiddleware

## Methods

Expand All @@ -7,8 +7,8 @@
```php
<?php

use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\Router\Some\Router;
use Chubbyphp\Framework\Middleware\UrlMatcherMiddleware;
use Chubbyphp\Framework\Router\Some\UrlMatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -24,10 +24,10 @@ $handler = new class() implements RequestHandlerInterface {
}
};

$router = new Router();
$urlMatcher = new UrlMatcher();
$responseFactory = new ResponseFactory();

$routerMiddleware = new RouterMiddleware($router, $responseFactory);
$urlMatcherMiddleware = new UrlMatcherMiddleware($urlMatcher, $responseFactory);

$response = $routerMiddleware->process($request, $handler);
$response = $urlMatcherMiddleware->process($request, $handler);
```
96 changes: 13 additions & 83 deletions src/Middleware/RouterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,36 @@

namespace Chubbyphp\Framework\Middleware;

use Chubbyphp\Framework\Router\Exceptions\RouterExceptionInterface;
use Chubbyphp\Framework\Router\RouterInterface;
use Chubbyphp\Framework\Router\UrlMatcherInterface;
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;

/**
* @deprecated
*/
final class RouterMiddleware 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 RouterInterface $router;

private ResponseFactoryInterface $responseFactory;

private LoggerInterface $logger;
private UrlMatcherMiddleware $urlMatcherMiddleware;

public function __construct(
RouterInterface $router,
UrlMatcherInterface $urlMatcher,
ResponseFactoryInterface $responseFactory,
?LoggerInterface $logger = null
) {
$this->router = $router;
$this->responseFactory = $responseFactory;
$this->logger = $logger ?? new NullLogger();
}
@trigger_error(
sprintf('Use %s parameter instead of instead of "%s"', UrlMatcherMiddleware::class, self::class),
E_USER_DEPRECATED
);

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

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

foreach ($route->getAttributes() as $attribute => $value) {
$request = $request->withAttribute($attribute, $value);
}

return $handler->handle($request);
$this->urlMatcherMiddleware = new UrlMatcherMiddleware($urlMatcher, $responseFactory, $logger);
}

private function routeExceptionResponse(RouterExceptionInterface $routerException): ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): 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;
return $this->urlMatcherMiddleware->process($request, $handler);
}
}
109 changes: 109 additions & 0 deletions src/Middleware/UrlMatcherMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Framework\Middleware;

use Chubbyphp\Framework\Router\Exceptions\RouterExceptionInterface;
use Chubbyphp\Framework\Router\UrlMatcherInterface;
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 UrlMatcherMiddleware 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 UrlMatcherInterface $urlMatcher;

private ResponseFactoryInterface $responseFactory;

private LoggerInterface $logger;

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

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

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

foreach ($route->getAttributes() as $attribute => $value) {
$request = $request->withAttribute($attribute, $value);
}

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

private function routeExceptionResponse(RouterExceptionInterface $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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Chubbyphp\Framework\Router\Exceptions;

use Chubbyphp\Framework\Middleware\RouterMiddleware;
use Chubbyphp\Framework\Middleware\UrlMatcherMiddleware;
use Chubbyphp\Framework\Router\RouterException;

final class MissingRouteAttributeOnRequestException extends RouterException
Expand All @@ -22,7 +22,7 @@ public static function create($route): self
return new self(sprintf(
'Request attribute "route" missing or wrong type "%s", please add the "%s" middleware',
is_object($route) ? get_class($route) : gettype($route),
RouterMiddleware::class
UrlMatcherMiddleware::class
), 2);
}
}
27 changes: 2 additions & 25 deletions src/Router/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,7 @@

namespace Chubbyphp\Framework\Router;

use Psr\Http\Message\ServerRequestInterface;

interface RouterInterface
/** @deprecated */
interface RouterInterface extends UrlGeneratorInterface, UrlMatcherInterface
{
public function match(ServerRequestInterface $request): RouteInterface;

/**
* @param array<string, string> $attributes
* @param array<string, mixed> $queryParams
*
* @throws RouterException
*/
public function generateUrl(
ServerRequestInterface $request,
string $name,
array $attributes = [],
array $queryParams = []
): string;

/**
* @param array<string, string> $attributes
* @param array<string, mixed> $queryParams
*
* @throws RouterException
*/
public function generatePath(string $name, array $attributes = [], array $queryParams = []): string;
}

0 comments on commit 28613a0

Please sign in to comment.