src/Application.php
public function run(Request $request = null) {
        if ($request === null) {
            $request = new Request();
        }
        $this->request = $request;
        $requestBak = Request::current($request);

        // Grab all of the matched routes.
        $routes = $this->matchRoutes($this->request);

        // Try all of the matched routes in turn.
        $dispatched = false;
        $result = null;
        try {
            foreach ($routes as $route_args) {
                list($route, $args) = $route_args;

                try {
                    // Dispatch the first matched route.
                    ob_start();
                    $response = $route->dispatch($request, $args);
                    $body = ob_get_clean();

                    $result = [
                        'routing' => $args,
                        'response' => $response,
                        'body' => $body
                    ];

                    // Once a route has been successfully dispatched we break and don't dispatch anymore.
                    $dispatched = true;
                    break;
                } catch (Exception\Pass $pex) {
                    ob_end_clean();
                    // If the route throws a pass then continue on to the next route.
                    continue;
                } catch (\Exception $ex) {
                    ob_end_clean();
                    throw $ex;
                }
            }

            if (!$dispatched) {
                throw new NotFoundException();
            }
        } catch (\Exception $ex) {
            $result = $ex;
        }

        $result = $this->finalize($result);
        Request::current($requestBak);

        return $result;
    }
public function run(Request $request = null) {
        if ($request === null) {
            $request = new Request();
        }
        $this->request = $request;
        $requestBak = Request::current($request);

        // Grab all of the matched routes.
        $routes = $this->matchRoutes($this->request);

        // Try all of the matched routes in turn.
        $dispatched = false;
        $result = null;
        try {
            foreach ($routes as $route_args) {
                list($route, $args) = $route_args;

                try {
                    // Dispatch the first matched route.
                    ob_start();
                    $response = $route->dispatch($request, $args);
                    $body = ob_get_clean();

                    $result = [
                        'routing' => $args,
                        'response' => $response,
                        'body' => $body
                    ];

                    // Once a route has been successfully dispatched we break and don't dispatch anymore.
                    $dispatched = true;
                    break;
                } catch (Exception\Pass $pex) {
                    ob_end_clean();
                    // If the route throws a pass then continue on to the next route.
                    continue;
                } catch (\Exception $ex) {
                    ob_end_clean();
                    throw $ex;
                }
            }

            if (!$dispatched) {
                throw new NotFoundException();
            }
        } catch (\Exception $ex) {
            $result = $ex;
        }

        $result = $this->finalize($result);
        Request::current($requestBak);

        return $result;
    }
Run the application against a {@link Request}.

@param Request|null $request A {@link Request} to run the application against or null to run against a request
on the current environment.
@return mixed Returns a response appropriate to the request's ACCEPT header.