update chapter 10 solutions

This commit is contained in:
lubiana 2022-05-30 14:27:48 +02:00 committed by Andre Lubian
parent e91a166816
commit fcf5bf2653
3 changed files with 9 additions and 8 deletions

View file

@ -3,10 +3,10 @@
use FastRoute\RouteCollector; use FastRoute\RouteCollector;
use Lubian\NoFramework\Action\Hello; use Lubian\NoFramework\Action\Hello;
use Lubian\NoFramework\Action\Other; use Lubian\NoFramework\Action\Other;
use Psr\Http\Message\ResponseInterface as RI; use Psr\Http\Message\ResponseInterface;
return function (RouteCollector $r) { return function (RouteCollector $r) {
$r->addRoute('GET', '/hello[/{name}]', Hello::class); $r->addRoute('GET', '/hello[/{name}]', Hello::class);
$r->addRoute('GET', '/other', [Other::class, 'handle']); $r->addRoute('GET', '/other', [Other::class, 'handle']);
$r->addRoute('GET', '/', fn (RI $r) => $r->withStatus(302)->withHeader('Location', '/hello')); $r->addRoute('GET', '/', fn (ResponseInterface $r) => $r->withHeader('Location', '/hello') ->withStatus(302));
}; };

View file

@ -11,8 +11,7 @@ final class Hello
ResponseInterface $response, ResponseInterface $response,
Clock $clock, Clock $clock,
string $name = 'Stranger' string $name = 'Stranger'
): ResponseInterface ): ResponseInterface {
{
$body = $response->getBody(); $body = $response->getBody();
$body->write('Hello ' . $name . '!<br />'); $body->write('Hello ' . $name . '!<br />');

View file

@ -11,7 +11,6 @@ use Lubian\NoFramework\Exception\NotFound;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable; use Throwable;
use Whoops\Handler\PrettyPageHandler; use Whoops\Handler\PrettyPageHandler;
use Whoops\Run; use Whoops\Run;
@ -59,12 +58,15 @@ $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->g
try { try {
switch ($routeInfo[0]) { switch ($routeInfo[0]) {
case Dispatcher::FOUND: case Dispatcher::FOUND:
$routeTarget = $routeInfo[1];
$args = $routeInfo[2];
foreach ($routeInfo[2] as $attributeName => $attributeValue) { foreach ($routeInfo[2] as $attributeName => $attributeValue) {
$request = $request->withAttribute($attributeName, $attributeValue); $request = $request->withAttribute($attributeName, $attributeValue);
} }
$routeInfo[2]['request'] = $request; $args['request'] = $request;
assert($container instanceof InvokerInterface); $invoker = $container->get(InvokerInterface::class);
$response = $container->call($routeInfo[1], $routeInfo[2]); assert($invoker instanceof InvokerInterface);
$response = $invoker->call($routeTarget, $args);
assert($response instanceof ResponseInterface); assert($response instanceof ResponseInterface);
break; break;
case Dispatcher::METHOD_NOT_ALLOWED: case Dispatcher::METHOD_NOT_ALLOWED: