pushHandler(new PrettyPageHandler()); } else { $whoops->pushHandler(function (Throwable $e): void { error_log("Error: " . $e->getMessage(), $e->getCode()); echo 'An Error happened'; }); } $whoops->register(); /** @var ContainerInterface $container */ $container = require __DIR__ . '/../config/dependencies.php'; /** @var ServerRequestInterface $request */ $request = $container->get(ServerRequestInterface::class); $routeDefinitionCallback = require __DIR__ . '/../config/routes.php'; $dispatcher = simpleDispatcher($routeDefinitionCallback); $routeInfo = $dispatcher->dispatch( $request->getMethod(), $request->getUri()->getPath(), ); try { switch ($routeInfo[0]) { case Dispatcher::METHOD_NOT_ALLOWED: throw new NotAllowedException(); case Dispatcher::FOUND: /** @var RequestHandlerInterface $handler */ $handler = $container->get($routeInfo[1][0]); $method = $routeInfo[1][1]; if (!$handler instanceof RequestHandlerInterface) { throw new InternalServerErrorException(); } foreach ($routeInfo[2] as $attributeName => $attributeValue) { $request = $request->withAttribute($attributeName, $attributeValue); } $response = $handler->$method($request); break; case Dispatcher::NOT_FOUND: default: throw new NotFoundException(); } } catch (NotAllowedException) { $response = $container->get(NotAllowed::class)->handle($request); } catch (NotFoundException) { $response = $container->get(NotFound::class)->handle($request); } catch (Exception) { $response = $container->get(InternalServerError::class)->handle($request); } /** @var string $name */ foreach ($response->getHeaders() as $name => $values) { $first = strtolower($name) !== 'set-cookie'; foreach ($values as $value) { $header = sprintf('%s: %s', $name, $value); header($header, $first); $first = false; } } $statusLine = sprintf( 'HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase() ); header($statusLine, true, $response->getStatusCode()); echo $response->getBody();