/** * dispatch * dispatches execution to a handler based on * a request uri * * pattern modifications based on those by Joe Topjian, * in the GluePHP micro-framework: http://gluephp.com/ * * @param \dirp\http\request $request * @param \dirp\http\response * @throws \dirp\exception\routing if no routes are defined * @throws \dirp\exception\routing if a method is assigned but not existant * @return mixed */ public static function dispatch(\dirp\http\request $request, \dirp\http\response $response) { if (!static::$_routes) { return false; // boy i sure hope this is handled somewhere } $routes = static::$_routes; $path = $request->get_uri(); foreach ($routes as $pair) { $pattern = $pair[0]; $handler = $pair[1]; $pattern = str_replace('/', '\\/', $pattern); $pattern = '^' . $pattern . '\\/?$'; if (preg_match("/{$pattern}/i", $path, $matches)) { if (!method_exists($handler[0], $handler[1])) { throw new \dirp\exception\routing('that method (' . $handler[1] . ') doesn\'t exist!'); } // make matches available through the request $request->set_params($matches); // route passing/halting: try { // check for a 'before' handler for this controller. if (method_exists($handler[0], 'before')) { call_user_func(array($handler[0], 'before'), $request, $response); } if ($ret = call_user_func(array($handler[0], $handler[1]), $request, $response)) { return $ret; } return $handler[0]; } catch (\dirp\exception\pass $e) { continue; } catch (\dirp\exception\halt $e) { return $e->getMessage(); } } } throw new \dirp\exception\routing('no matches, four oh four!'); }