/** * Parse an array of request parts (eg. $_GET["p"] exploded by "/"), work out what controller to set up, * instantiate it, and work out the method + arguments to dispatch to it. * * @param array $parts An array of parts of the request. * @param array $controllers An array of available controllers, with the keys as the controller names and the * values as the factory names. * @return array An array of information about the response: * 0 => the controller name * 1 => the controller instance * 2 => the method to dispatch * 3 => the arguments to pass when dispatching * 4 => the response type to use * * @package esoTalk */ function parseRequest($parts, $controllers) { $c = strtolower(@$parts[0]); $method = "index"; $type = RESPONSE_TYPE_DEFAULT; // If the specified controller doesn't exist, 404. if (!isset($controllers[$c])) { ET::notFound(); } // Make an instance of the controller. $controller = ETFactory::make($controllers[$c]); // Determine the controller method and response type to use. Default to index. $arguments = array_slice($parts, 2); if (!empty($parts[1])) { $method = strtolower($parts[1]); // If there's a period in the method string, use the first half as the method and the second half as the response type. if (strpos($method, ".") !== false) { list($method, $suffix) = explode(".", $method, 2); if (in_array($suffix, array(RESPONSE_TYPE_VIEW, RESPONSE_TYPE_JSON, RESPONSE_TYPE_AJAX, RESPONSE_TYPE_ATOM))) { $type = $suffix; } } // Get all of the action methods in the controller class. $methods = get_class_methods($controller); foreach ($methods as $k => $v) { if (strpos($v = strtolower($v), "action_") !== 0) { unset($methods[$k]); continue; } $methods[$k] = substr($v, 7); } // If the method we want to use doesn't exist in the controller... if (!$method or !in_array($method, $methods)) { // Search for a plugin with this method. If found, use that. $found = false; foreach (ET::$plugins as $plugin) { if (method_exists($plugin, "action_" . $c . "Controller_" . $method)) { $found = true; break; } } // If one wasn't found, default to the "action_index" method. if (!$found) { $method = "index"; $arguments = array_slice($parts, 1); } } } return array($c, $controller, $method, $arguments, $type); }