/** * Loops through routes trying to find the right one * * @param Eden\Registry\Index $request The request object * * @return array */ public function getRoute($request) { $path = $request->get('path', 'string'); //find the route foreach ($this->routes as $pattern => $meta) { $regex = str_replace('**', '!!', $pattern); $regex = str_replace('*', '([^/]*)', $regex); $regex = str_replace('!!', '(.*)', $regex); $regex = '#^' . $regex . '(.*)#'; if (!preg_match($regex, $path, $matches)) { continue; } //get dynamic variables $variables = $this->getVariables($pattern, $path); return array($pattern, $meta, $variables); } return array(false, false, false); }
/** * Process route middleware * * @param Eden\Registry\Index $request The request object * @param Eden\Registry\Index $response The response object * * @return bool */ protected function processRoutes($request, $response) { $method = strtoupper($request->get('method')); //if no routing on this if (!isset($this->routeMiddleware[$method]) || !is_array($this->routeMiddleware[$method])) { return false; } $args = array($request, $response); $path = $request->get('path', 'string'); //determine the route foreach ($this->routeMiddleware[$method] as $route) { $pattern = $route[0]; $callback = $route[1]; $regex = str_replace('**', '!!', $pattern); $regex = str_replace('*', '([^/]*)', $regex); $regex = str_replace('!!', '(.*)', $regex); $regex = '#^' . $regex . '(.*)#'; if (!preg_match($regex, $path, $matches)) { continue; } //get dynamic variables $variables = $this->getVariables($matches); //and stuff it in the request object $request->set('variables', $variables); //bind callback $callback = $callback->bindTo($this, get_class($this)); //call the callback //and keep going unles they say explicitly to stop if (call_user_func_array($callback, $args) === false) { break; } } return true; }