trails_root = $trails_root; $this->trails_uri = $trails_uri; $this->default_controller = $default_controller; } /** * Maps a string to a response which is then rendered. * * @param string The requested URI. * * @return void */ function dispatch($uri) { # E_USER_ERROR|E_USER_WARNING|E_USER_NOTICE|E_RECOVERABLE_ERROR = 5888 $old_handler = set_error_handler(array($this, 'error_handler'), 5888); ob_start(); $level = ob_get_level(); $this->map_uri_to_response($this->clean_request_uri((string) $uri))->output(); while (ob_get_level() >= $level) { ob_end_flush(); } if (isset($old_handler)) { set_error_handler($old_handler); } } /** * Maps an URI to a response by figuring out first what controller to * instantiate, then delegating the unconsumed part of the URI to the * controller who returns an appropriate response object or throws a * Trails_Exception. * * @param string the URI string * * @return mixed a response object */ function map_uri_to_response($uri) { try { list($controller_path, $unconsumed) = '' === $uri ? $this->default_route() : $this->parse($uri); $controller = $this->load_controller($controller_path); $response = $controller->perform($unconsumed); } catch (Exception $e) { $response = isset($controller) ? $controller->rescue($e) : $this->trails_error($e); } return $response; } /** * * @return array an array containing the default controller and an * empty unconsumed route */ function default_route() { if (!$this->file_exists($this->default_controller . '.php')) { throw new Trails_MissingFile( "Default controller '{$this->default_controller}' not found'"); } return array($this->default_controller, ''); } function trails_error($exception) { ob_clean(); # show details for local requests $detailed = @$_SERVER['REMOTE_ADDR'] === '127.0.0.1'; $body = sprintf('
%s', htmlentities($exception->__toString()), $detailed ? htmlentities($exception->getTraceAsString()) : ''); if ($exception instanceof Trails_Exception) { $response = new Trails_Response($body, $exception->headers, $exception->getCode(), $exception->getMessage()); } else { $response = new Trails_Response($body, array(), 500, $exception->getMessage()); } return $response; } /** * Clean up URI string by removing the query part and leading slashes. * * @param string an URI string * * @return string the cleaned string */ function clean_request_uri($uri) { if (FALSE !== ($pos = strpos($uri, '?'))) { $uri = substr($uri, 0, $pos); } return ltrim($uri, '/'); } /** *