From fcf5bf2653bddfba6549e0ccd4253eee1cd29792 Mon Sep 17 00:00:00 2001 From: lubiana Date: Mon, 30 May 2022 14:27:48 +0200 Subject: [PATCH] update chapter 10 solutions --- implementation/10/config/routes.php | 4 ++-- implementation/10/src/Action/Hello.php | 3 +-- implementation/10/src/Bootstrap.php | 10 ++++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/implementation/10/config/routes.php b/implementation/10/config/routes.php index 23ff0ef..b68712f 100644 --- a/implementation/10/config/routes.php +++ b/implementation/10/config/routes.php @@ -3,10 +3,10 @@ use FastRoute\RouteCollector; use Lubian\NoFramework\Action\Hello; use Lubian\NoFramework\Action\Other; -use Psr\Http\Message\ResponseInterface as RI; +use Psr\Http\Message\ResponseInterface; return function (RouteCollector $r) { $r->addRoute('GET', '/hello[/{name}]', Hello::class); $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)); }; diff --git a/implementation/10/src/Action/Hello.php b/implementation/10/src/Action/Hello.php index 379ea73..dfb8df8 100644 --- a/implementation/10/src/Action/Hello.php +++ b/implementation/10/src/Action/Hello.php @@ -11,8 +11,7 @@ final class Hello ResponseInterface $response, Clock $clock, string $name = 'Stranger' - ): ResponseInterface - { + ): ResponseInterface { $body = $response->getBody(); $body->write('Hello ' . $name . '!
'); diff --git a/implementation/10/src/Bootstrap.php b/implementation/10/src/Bootstrap.php index 26fd2ea..8acaa81 100644 --- a/implementation/10/src/Bootstrap.php +++ b/implementation/10/src/Bootstrap.php @@ -11,7 +11,6 @@ use Lubian\NoFramework\Exception\NotFound; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\RequestHandlerInterface; use Throwable; use Whoops\Handler\PrettyPageHandler; use Whoops\Run; @@ -59,12 +58,15 @@ $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->g try { switch ($routeInfo[0]) { case Dispatcher::FOUND: + $routeTarget = $routeInfo[1]; + $args = $routeInfo[2]; foreach ($routeInfo[2] as $attributeName => $attributeValue) { $request = $request->withAttribute($attributeName, $attributeValue); } - $routeInfo[2]['request'] = $request; - assert($container instanceof InvokerInterface); - $response = $container->call($routeInfo[1], $routeInfo[2]); + $args['request'] = $request; + $invoker = $container->get(InvokerInterface::class); + assert($invoker instanceof InvokerInterface); + $response = $invoker->call($routeTarget, $args); assert($response instanceof ResponseInterface); break; case Dispatcher::METHOD_NOT_ALLOWED: