Example #1
0
 /**
  * Run the router and serve the current request.
  * 
  * This function is __CALLED INTERNALLY__ and, therefore
  * it __MUST NOT__ be called! 
  * 
  * @param Request $reqestToFulfill the request to be served/fulfilled
  *
  * @return Response $reqestToFulfill the request to be served/fulfilled
  */
 public static function run(Request &$reqestToFulfill)
 {
     //derive the response from the current request
     $response = Response::deriveFromRequest($reqestToFulfill);
     $decodedUri = urldecode($reqestToFulfill->getUri()->getPath());
     $reversedParams = null;
     //this is the route that reference the action to be taken
     $actionRuote = null;
     //test/try matching every route
     foreach (self::$routes as $currentRoute) {
         //build a collection from the current reverser URI (of detect the match failure)
         $reversedParams = $currentRoute->matchURI($decodedUri, $reqestToFulfill->getMethod());
         if (is_object($reversedParams)) {
             //execute the requested action!
             $actionRuote =& $currentRoute;
             //stop searching for a suitable URI to be matched against the current one
             break;
         }
     }
     //oh.... seems like we have a 404 Not Found....
     if (!is_object($actionRuote)) {
         $response = $response->withStatus(404);
         foreach (self::$callbacks as $currentRoute) {
             //check for a valid callback
             if (is_object($currentRoute->matchURI(self::NOT_FOUND, $reqestToFulfill->getMethod()))) {
                 //flag the execution of this failback action!
                 $actionRuote = $currentRoute;
                 //found what I was looking for, break the foreach
                 break;
             }
         }
     }
     //execute the router call
     $request = clone $reqestToFulfill;
     $deductedParams = is_object($reversedParams) ? $reversedParams : new SerializableCollection();
     is_object($actionRuote) ? $actionRuote($request, $response, $deductedParams) : null;
     //this function have to return a response
     return $response;
 }