update implementation of chapter 9

This commit is contained in:
lubiana 2022-05-21 00:57:02 +02:00
parent 64ca2ece2e
commit f743663b21
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
7 changed files with 329 additions and 24 deletions

View file

@ -4,10 +4,11 @@ namespace Lubian\NoFramework;
use FastRoute\Dispatcher;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Lubian\NoFramework\Exception\InternalServerError;
use Lubian\NoFramework\Exception\MethodNotAllowed;
use Lubian\NoFramework\Exception\NotFound;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use Whoops\Handler\PrettyPageHandler;
@ -16,7 +17,6 @@ use Whoops\Run;
use function assert;
use function error_log;
use function error_reporting;
use function FastRoute\simpleDispatcher;
use function getenv;
use function header;
use function sprintf;
@ -43,12 +43,15 @@ if ($environment === 'dev') {
$whoops->register();
$request = ServerRequestFactory::fromGlobals();
$response = new Response;
$container = require __DIR__ . '/../config/container.php';
assert($container instanceof ContainerInterface);
$request = $container->get(ServerRequestInterface::class);
assert($request instanceof ServerRequestInterface);
$dispatcher = $container->get(Dispatcher::class);
assert($dispatcher instanceof Dispatcher);
$routeDefinitionCallback = require __DIR__ . '/../config/routes.php';
$dispatcher = simpleDispatcher($routeDefinitionCallback);
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->getPath(),);
@ -56,7 +59,7 @@ try {
switch ($routeInfo[0]) {
case Dispatcher::FOUND:
$className = $routeInfo[1];
$handler = new $className($response);
$handler = $container->get($className);
assert($handler instanceof RequestHandlerInterface);
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
$request = $request->withAttribute($attributeName, $attributeValue);