public function dispatch(ServerRequestInterface $request): ServerRequestInterface
    {
        $dispatcher = simpleDispatcher([$this, 'addRoutes']);

        $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());

        if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
            throw new MethodNotAllowedException($request->getMethod(), $routeInfo[1]);
        }

        if ($routeInfo[0] === Dispatcher::NOT_FOUND) {
            throw new NotFoundHttpException(sprintf("Route '%s' not found.", $request->getUri()->getPath()));
        }

        foreach ($routeInfo[2] as $key => $value) {
            $request = $request->withAttribute($key, $value);
        }

        return $request->withAttribute('route', $this->routes[$routeInfo[1]]);
    }