예제 #1
0
 /**
  * "Runs" the routes collection relative to a provided request
  *
  * This iterates over all available links in the collection and attempts to match the path
  * of the request object against each link's compild pattern.  If a match is found it will
  * attempt to dispatch to the linked action.
  *
  * @access public
  * @param Interfaces\Request $request The request to run against
  * @param Interfaces\Response $response The response to use
  * @return mixed The response of the dispatched action
  */
 public function run(Interfaces\Request $request, Interfaces\Response $response)
 {
     $restless_uri = NULL;
     $request_uri = $request->getURL()->getPath();
     $unused_params = array();
     if (self::$restless) {
         $restless_uri = $request_uri[strlen($request_uri) - 1] == '/' ? substr($request_uri, 0, -1) : $request_uri . '/';
     }
     if ($redirect_type = $this->translateRedirect($request_uri, $restless_uri)) {
         $request->redirect($request_uri, $redirect_type);
     }
     foreach ($this->links as $pattern => $link) {
         try {
             if (preg_match('#^' . $pattern . '$#', $request_uri, $matches)) {
                 array_shift($matches);
                 $route = $link['route'];
                 $action = $link['action'];
                 $params = array_combine($link['params'], $matches);
                 $params = array_map('urldecode', $params);
                 if (is_string($action)) {
                     $action = self::decompile($action, $params, $unused_params);
                     $params = $unused_params;
                 }
                 foreach ($params as $key => $value) {
                     $request->set($key, $value);
                 }
                 $response = $this->captureResponse($action, $request, $response);
                 break;
             } elseif (preg_match('#^' . $pattern . '$#', $restless_uri)) {
                 $request->redirect($restless_uri, HTTP\REDIRECT_PERMANENT);
             }
         } catch (Flourish\ContinueException $e) {
             continue;
         } catch (Flourish\YieldException $e) {
             break;
         }
     }
     return $response->checkCode(400, 599) ? $this->handleError($request, $response) : $response;
 }