protected function setUp() { // Save config $this->kohana_config['core.url_suffix'] = Kohana_Config::instance()->get('core.url_suffix'); // Save Server API $this->kohana_server_api = Kohana::$server_api; // Save Router members $this->router_vars = array('complete_uri' => Router::$complete_uri, 'controller' => Router::$controller, 'current_uri' => Router::$current_uri, 'query_string' => Router::$query_string, 'rsegments' => Router::$rsegments, 'routed_uri' => Router::$routed_uri, 'segments' => Router::$segments, 'url_suffix' => Router::$url_suffix); // Reset Router members Router::$complete_uri = ''; Router::$controller = NULL; Router::$current_uri = ''; Router::$query_string = ''; Router::$rsegments = NULL; Router::$routed_uri = ''; Router::$segments = NULL; Router::$url_suffix = ''; }
/** * Invokes method $method (with arguments $arguments) of the wrapped Controller, optionally replacing * $_GET and $_POST contents before making the call. * @param $method The name of the method to invoke. * @param $arguments The arguments to pass. * @param $get The array that should replace $_GET. * @param $post The array that should replace $_POST. * @param $capture If true (default) any output generated by the method is captured and returned, * otherwise the method's return value itself is returned. * @return The called method's output or its return value, depending on $capture. */ public function method($method, $arguments = null, $get = null, $post = null, $capture = true) { // If method does not exist, call the "__call" magic method // This can be made faster by catching an exception in the switch statement below if (!method_exists($this->controller, $method)) { $arguments = array($method, $arguments); $method = '__call'; } // Change Router state to reflect call //Router::$current_route = ''; //Router::$current_uri = ''; //Router::$complete_uri = ''; Router::$controller = $this->router_controller; Router::$method = $method; if ($arguments === null) { Router::$arguments = array(); } else { Router::$arguments = $arguments; } // If get or post parameters are passed, alter $_GET, $_POST and Router::$query_string accordingly // NOTE: Should we alter $_SERVER['QUERY_STRING'] too? if ($get !== null) { $old_get = $_GET; $_GET = $get; Router::$query_string = '?' . http_build_query($get); } if ($post !== null) { $old_post = $_POST; $_POST = $post; } // Start output capture if ($capture) { ob_implicit_flush(0); ob_start(); } if (is_string($arguments)) { $arguments = array($arguments); } // Invoke method switch (count($arguments)) { case 1: $result = $this->controller->{$method}($arguments[0]); break; case 2: $result = $this->controller->{$method}($arguments[0], $arguments[1]); break; case 3: $result = $this->controller->{$method}($arguments[0], $arguments[1], $arguments[2]); break; case 4: $result = $this->controller->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3]); break; default: // Resort to using call_user_func_array for many arguments (slower) $result = call_user_func_array(array($this->controller, $method), $arguments); break; } // Run system.post_controller Event::run('dispatch.post_controller'); // Stop output capture if ($capture) { $result = ob_get_contents(); ob_end_clean(); } // Revert $_GET and $_POST changes if ($get !== null) { $_GET = $old_get; } if ($post !== null) { $_POST = $old_post; } // Revert Router state //Router::$current_route = $this->router_state_backup['current_route']; //Router::$current_uri = $this->router_state_backup['current_uri']; //Router::$complete_uri = $this->router_state_backup['complete_uri']; Router::$query_string = $this->router_state_backup['query_string']; Router::$controller = $this->router_state_backup['controller']; Router::$method = $this->router_state_backup['method']; Router::$arguments = $this->router_state_backup['arguments']; return $result; }
/** * Router setup routine. Automatically called during Kohana setup process. * * @return void */ public static function setup() { if (!empty($_SERVER['QUERY_STRING'])) { // Set the query string to the current query string Router::$query_string = '?' . trim($_SERVER['QUERY_STRING'], '&/'); } if (Router::$routes === NULL) { // Load routes Router::$routes = Kohana::config('routes'); } // Default route status $default_route = FALSE; if (Router::$current_uri === '') { // Make sure the default route is set if (!isset(Router::$routes['_default'])) { throw new Kohana_Exception('core.no_default_route'); } // Use the default route when no segments exist Router::$current_uri = Router::$routes['_default']; // Default route is in use $default_route = TRUE; } // Make sure the URL is not tainted with HTML characters Router::$current_uri = html::specialchars(Router::$current_uri, FALSE); // Remove all dot-paths from the URI, they are not valid Router::$current_uri = preg_replace('#\\.[\\s./]*/#', '', Router::$current_uri); // At this point segments, rsegments, and current URI are all the same Router::$segments = Router::$rsegments = Router::$current_uri = trim(Router::$current_uri, '/'); // Set the complete URI Router::$complete_uri = Router::$current_uri . Router::$query_string; // Explode the segments by slashes Router::$segments = ($default_route === TRUE or Router::$segments === '') ? array() : explode('/', Router::$segments); if ($default_route === FALSE and count(Router::$routes) > 1) { // Custom routing Router::$rsegments = Router::routed_uri(Router::$current_uri); } // The routed URI is now complete Router::$routed_uri = Router::$rsegments; // Routed segments will never be empty Router::$rsegments = explode('/', Router::$rsegments); // Prepare to find the controller $controller_path = ''; $method_segment = NULL; // Paths to search $paths = Kohana::include_paths(); foreach (Router::$rsegments as $key => $segment) { // Add the segment to the search path $controller_path .= $segment; $found = FALSE; foreach ($paths as $dir) { // Search within controllers only $dir .= 'controllers/'; if (is_dir($dir . $controller_path) or is_file($dir . $controller_path . EXT)) { // Valid path $found = TRUE; // The controller must be a file that exists with the search path if ($c = str_replace('\\', '/', realpath($dir . $controller_path . EXT)) and is_file($c) and strpos($c, $dir) === 0) { // Set controller name Router::$controller = $segment; // Change controller path Router::$controller_path = $c; // Set the method segment $method_segment = $key + 1; // Stop searching break; } } } if ($found === FALSE) { // Maximum depth has been reached, stop searching break; } // Add another slash $controller_path .= '/'; } if ($method_segment !== NULL and isset(Router::$rsegments[$method_segment])) { // Set method Router::$method = Router::$rsegments[$method_segment]; if (isset(Router::$rsegments[$method_segment + 1])) { // Set arguments Router::$arguments = array_slice(Router::$rsegments, $method_segment + 1); } } // Last chance to set routing before a 404 is triggered Event::run('system.post_routing'); if (Router::$controller === NULL) { // No controller was found, so no page can be rendered Event::run('system.404'); } }