/** * Runs the application and routes the request. * * @param object $app Instantiated application object. */ public static function run($app) { $route = Router::process(new Request()); // Route to 404 if controller and/or method cannot be found if (!class_exists($route['controller']) or !method_exists($route['controller'], "{$route['method']}Action")) { if ($app->environment() == 'development') { throw new Exception("Unable to find controller for '" . Request::pathInfo() . "'"); } else { $route = Router::set404(); } } // Get method parameters $r = new ReflectionMethod("{$route['controller']}::{$route['method']}Action"); $params = []; foreach ($r->getParameters() as $param) { if (isset($route['params'][$param->getName()])) { $params[] = $route['params'][$param->getName()]; } } unset($r, $param); static::$controller = new $route['controller'](); // Run before filters $beforeAll = EventDispatcher::dispatch("before." . $route['controller'] . "::*"); $beforeAction = EventDispatcher::dispatch("before." . $route['controller'] . "::{$route['method']}"); if ($beforeAll instanceof Response || $beforeAction instanceof Response) { static::$controller->executeAction = false; $response = $beforeAll ?: $beforeAction; } // Execute action if (static::$controller->executeAction) { $response = call_user_func_array(array(static::$controller, $route['method'] . 'Action'), $params); } // Run after filters $afterAll = EventDispatcher::dispatch("after." . $route['controller'] . "::*"); $afterAction = EventDispatcher::dispatch("after." . $route['controller'] . "::{$route['method']}"); if ($afterAll instanceof Response || $afterAction instanceof Response) { $response = $afterAll instanceof Response ? $afterAll : $afterAction; } // Send response if (!$response instanceof Response) { throw new Exception("The controller [{$route['controller']}::{$route['method']}] returned an invalid response."); } else { $response->send(); } // Shutdown the controller static::$controller->__shutdown(); }
/** * Creates the URI for the specified page. * * @param integer $page * * @return string */ public function createUri($page) { $queryString = $this->query; $queryString[] = "page={$page}"; $queryString = implode('&', $queryString); return Request::pathInfo() . "?{$queryString}"; }
/** * Routes the request to the controller. * * @param Request $request */ public static function process(Request $request) { $uri = "/" . trim($request->pathInfo(), '/'); // Check if this is root page if (isset(static::$routes['root']) and $request->pathInfo() == '/') { return static::setRoute(static::$routes['root']); } // The fun begins foreach (static::$routes as $route) { // Replace tokens $route->route = str_replace(array_keys(static::$tokens), array_values(static::$tokens), $route->route); // Does the route match the request? $pattern = "#^{$route->route}" . '(?<extension>' . implode('|', static::$extensions) . ")?\$#"; if (preg_match($pattern, $uri, $params)) { unset($params[0]); $route->params = array_merge($route->params, $params); $route->destination = preg_replace($pattern, $route->destination, $uri); // Merge params with defaults $route->params = array_merge($route->defaults, $route->params); if (in_array(strtolower($request->method()), $route->method)) { return static::setRoute($route); } } } // No matches, try 404 route if (isset(static::$routes['404'])) { return static::set404(); } else { throw new Exception("No routes found for '{$uri}'"); } }