/** * Run the app, parsing the requested URL and dispatching to the appropriate Route * @internal \Microsite\App|null $parent * @return bool|string Upon successful execution, the string of output produced, otherwise false */ public function run() { try { $do_output = false; $has_output = false; $response = $this->response(); if (!$response->did_output) { $response->did_output = true; $do_output = true; } $output = false; foreach ($this->routes as $route) { /** @var Route $route */ if ($route->match($this) && ($this->route == null || $this->route->match_type() < $route->match_type() && $this->route->get_url() == $route->get_url())) { $this->route = $route; } } if (isset($this->route)) { foreach ($this->middleware as $middleware) { $this->exec_params($middleware); } $result = $this->route->run($this); if ($result) { $output = (string) $result; $has_output = true; } } if ($do_output) { if ($has_output) { echo $output; } else { $this->header('HTTP/1.1 404 Not Found'); echo $response->render('404.php'); } } return $output; } catch (\Exception $e) { $response['error'] = $e; $this->header('HTTP/1.1 500 Internal Server Error'); if ($response instanceof Response) { $output = $response->render('error.php'); } else { $output = var_export($e, true); } } echo $output; return $output; }