Example #1
0
 public function translate()
 {
     // Initialize the response with the current state
     $request = new self(array(), $this->url, $this->status);
     // Attempt to translate the url until no more translations are found
     do {
         // If this flag is set to true, then translation will continue another round
         $translated = false;
         // Routes is formatted as `array(url-pattern-to-match => rewrite-instructions, ...)`.
         // Step through each route and try to match the base URL pattern with the current URL
         foreach ($this->routes as $route) {
             // A match with the current URL was found
             if (preg_match($route->pattern, $request->url)) {
                 // Rewrite the URL
                 $request->setUrl(preg_replace($route->pattern, $route->rewrite, $request->url));
                 $translated = true;
                 // Optional: If a new status was provided, set it
                 if ($route->status !== false) {
                     $request->setStatus($route->status);
                 }
                 // If the `last` flag is set or a redirect is required, then stop all translations
                 if (!empty($route->is_last) || $request->isStatusRedirect()) {
                     $translated = false;
                     break;
                 }
             }
         }
     } while ($translated);
     // Make sure that the URL is set strictly as a path
     $url_parts = parse_url($request->getUrl(false));
     $request->setUrl($url_parts['path']);
     // Inject rewritten URL params into _GET
     if (!empty($url_parts['query'])) {
         $params = array();
         parse_str($url_parts['query'], $params);
         $_GET = array_merge($_GET, $params);
     }
     // If the new status is a redirect
     if ((int) ($request->status / 100) == 3) {
         // Clean up the query string before creeating the new request
         unset($_GET[GOBE_QUERY_PATH]);
         unset($_GET[GOBE_QUERY_STATUS]);
         header("Location: " . $url_parts['path'] . '?' . http_build_query($_GET), true, $request->status);
         die;
     }
     // Set the new status in the HTTP header
     //		header("gobe-status", true, $request->status);
     return $request;
 }