Dispatch router for HTTP request.

@param ServerRequestInterface $request The current HTTP request object

@return array

@link   https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php
178066
public function dispatch(ServerRequestInterface $request)
    {
        parse_str($request->getUri()->getQuery(), $args);
        $uri = '/';
        if (isset($args['page'])) {
            $uri .= $args['page'];
        }

        return $this->createDispatcher()->dispatch(
            $request->getMethod(),
            $uri
        );
    }
890452
protected function prepRoutes(ServerRequestInterface $request) : void
    {
        $this->processGroups($request);
        $this->buildNameIndex();

        $routes = array_merge(array_values($this->routes), array_values($this->namedRoutes));

        foreach ($routes as $key => $route) {
            // check for scheme condition
            if (! is_null($route->getScheme()) && $route->getScheme() !== $request->getUri()->getScheme()) {
                continue;
            }

            // check for domain condition
            if (! is_null($route->getHost()) && $route->getHost() !== $request->getUri()->getHost()) {
                continue;
            }

            // check for port condition
            if (! is_null($route->getPort()) && $route->getPort() !== $request->getUri()->getPort()) {
                continue;
            }

            if (is_null($route->getStrategy())) {
                $route->setStrategy($this->getStrategy());
            }

            $this->addRoute($route->getMethod(), $this->parseRoutePath($route->getPath()), $route);
        }
    }
1227773
public function dispatch(ServerRequestInterface $request, callable $default)
    {
        $handler = new Handler($this->middleware, $default);

        return $handler->handle($request);
    }
132083
public function handleRequest(ServerRequestInterface $request) : ResponseInterface
    {
        list($callback, $params) = $this->internalRequestHandle($request);

        return call_user_func($callback, $params);
    }
983434
private function injectRoutes(ServerRequestInterface $request): void
    {
        foreach ($this->routes as $key => $route) {
            // check for scheme condition
            if (! is_null($route->getScheme()) && $route->getScheme() !== $request->getUri()->getScheme()) {
                continue;
            }
            // check for domain condition
            if (! is_null($route->getHost()) && $route->getHost() !== $request->getUri()->getHost()) {
                continue;
            }
            // check for port condition
            if (! is_null($route->getPort()) && $route->getPort() !== $request->getUri()->getPort()) {
                continue;
            }

            // add a route strategy if no one is defined. Use the default router strategy.
            if (is_null($route->getStrategy())) {
                // Throw an exception if there is not a default strategy defined.
                if (is_null($this->getStrategy())) {
                    throw new RuntimeException('A defaut strategy should be defined in the Router, as there is no specific strategy defined for the Route.');
                }
                $route->setStrategy($this->getStrategy());
            }

            $routePath = $this->replaceAssertPatterns($route->getRequirements(), $route->getPath());
            $routePath = $this->replaceWordPatterns($routePath);

            $this->addRoute($route->getAllowedMethods(), $this->basePath . $routePath, $route->getIdentifier());
        }
    }
942659
public function dispatch($path, RouteRequest $request = null)
    {
        if ($route = $this->match($path)) {
            // When the callback is an integer, it's refereing to a submux object.
            if (is_integer($route[2])) {
                $submux = $this->submux[$route[2]];
                $options = $route[3];

                // sub-path and call submux to dispatch
                // for pcre pattern?
                if ($route[0]) {

                    $matchedString = $route[3]['vars'][0];

                    return $submux->dispatch(substr($path, strlen($matchedString)));

                } else {
                    $s = substr($path, strlen($route[1]));

                    return $submux->dispatch(
                        substr($path, strlen($route[1])) ?: ''
                    );
                }
            }

            return $route;
        }
    }
53043
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        try {
            $route = $this->matchRoute($request);
        } catch (RouteException $e) {
            throw new RouterException("Invalid route definition", $e->getCode(), $e);
        }

        if (empty($route)) {
            throw new RouteNotFoundException($request->getUri());
        }

        return $this->container->get(ScopeInterface::class)->runScope(
            [RouteInterface::class => $route],
            function () use ($route, $request) {
                return $route->handle($request);
            }
        );
    }
342437
public function dispatch(ServerRequestInterface $request): ResponseInterface
    {
        return $this->get($request)->process($request, $this);
    }
832688
private function dispatch($request, $route)
    {
        if ($route->matched()) {
            $request = $request->withPathToMatch($route->matched(), $route->trailing());
        }
        // dispatch the route!
        return $this->dispatcher->withRoute($route)->__invoke($request);
    }
288863
public function dispatch(RequestInterface $request)
    {
        if (!$request->getParam('section')) {
            $request->setParam('section', $this->configStructure->getFirstSection()->getId());
        }

        return parent::dispatch($request);
    }
1244354
protected function dispatchMethod($request, $route)
    {
        list($path, $var) = $this->parseUrlPath($route);

        $route = str_replace('/', '@', implode('/', $path));
        $method = strpos($route, '@') ? explode('@', $route) : $route;

        return new CallbackDispatch($request, $this, $method, $var);
    }
622798
private function route(ServerRequestInterface $request)
    {
        $route = $request->getAttribute('route', false);
        if (! $route instanceof Route) {
            throw new BadHttpStackConfigurationException(
                "Dispatcher works with router middleware in order to process incoming requests. " .
                "Please check that the HTTP stack has the router middleware placed before the dispatcher. ".
                "Missing route in the request."
            );
        }
        return $route;
    }
285870
public function dispatch(Request $request)
    {
        $this->authorize($request);

        return $this->next->dispatch($request);
    }
253648
public function handle(ServerRequestInterface $request):ResponseInterface {
		$path = $request->getUri()->getPath();
// TODO: Abstract response type needed.
		$response = new PageResponse();

		try {
			$templateDirectory = implode(DIRECTORY_SEPARATOR, [
				$this->router->getBaseViewLogicPath(),
				"_component",
			]);
			$viewAssembly = $this->router->getViewAssembly($path);
			$view = $this->getView(
				$response->getBody(),
				(string)$viewAssembly,
				$templateDirectory,
				$path,
				$request->getHeaderLine("accept")
			);
		}
		catch(BasenameNotFoundException $exception) {
// TODO: Handle view not found.
			die("The requested view is not found!!!");
		}

		LogicFactory::setView($view);
		$baseLogicDirectory = $this->router->getBaseViewLogicPath();

		$logicAssembly = $this->router->getLogicAssembly($path);

		$logicObjects = $this->createLogicObjects(
			$logicAssembly,
			$baseLogicDirectory,
			$request->getUri()
		);

		$this->dispatchLogicObjects($logicObjects);
		$this->injectCsrf($view);
		$view->stream();

		return $response;
	}
292091
public function dispatch(ServerRequestInterface $request)
    {
        if ($this->routeMatched) {
            if (!is_subclass_of($this->actionNamespace, 'App\Action\AppAction')) {
                throw new BaseException(
                    sprintf('Action "%s" does not extend App\Action\AppAction', $this->actionNamespace)
                );
            }

            $actionInstance = new $this->actionNamespace();

            $beforeDispatchEvent = $this->dispatchEvent(
                self::EVENT_BEFORE_DISPATCH,
                $this,
                ['request' => $request, 'action' => $actionInstance]
            );

            if ($beforeDispatchEvent->getResult() instanceof Response) {
                return $beforeDispatchEvent->getResult();
            }

            $response = call_user_func_array([$actionInstance, 'invoker'], $this->params);
            $this->dispatchEvent(self::EVENT_AFTER_DISPATCH, $this, ['request' => $request, 'response' => $response]);

            return $response;
        } else {
            throw new NotFoundException(
                sprintf('Route "%s" does not found', $request->getServerParams()['REQUEST_URI'])
            );
        }
    }
1149897
protected function sendRequestThroughRouter($request)
    {
        $this->refreshRequest($request = Request::createFromBase($request));

        $this->boot();

        // Create a Pipeline instance.
        $pipeline = new Pipeline(
            $this, $this->shouldSkipMiddleware() ? array() : $this->middleware
        );

        return $pipeline->handle($request, function ($request)
        {
            $this->instance('request', $request);

            return $this->router->dispatch($request);
        });
    }
1350960
public function route(ServerRequestInterface $request)
    {

        /** @var Route[] $routes */
        $routes = array_reverse($this->routes);
        foreach ($routes as $route) {

            $path = $request->getUri()->getPath();
            $baseUri = $this->getOption('baseUri', '');

            $len = strlen($baseUri);
            if ($len > 0) {

                if (strncmp($baseUri, $path, $len) !== 0)
                    continue;

                $path = substr($path, $len);
            }

            if (empty($path))
                $path = '/';

            $vars = null;
            if (!in_array($request->getMethod(), $route->getMethods())
                || ($vars = $route->match($path)) === false)
                continue;

            $subRequest = $request;
            foreach ($vars as $key => $value)
                $subRequest = $subRequest->withAttribute($key, $value);

            $this->app->prepend(function($request, $response, $next) use ($route, $subRequest) {

                $handler = $this->app->prepareMiddleware($route->getHandler());
                return $handler($subRequest, $response, $next);
            });
        }

        return $this;
    }