Ejemplo n.º 1
0
 /**
  * Handles an error by calling an error handler (yeah I know)
  *
  * @access private
  * @param Interfaces\Request $request The current request being made
  * @param Interfaces\Response $response The current response
  * @return Interfaces\Response The new or modified response
  */
 private function handleError($request, $response)
 {
     $base_urls = array();
     $candidates = array();
     $handler = NULL;
     $error = $response->getStatus();
     $root_hash = md5(NULL . $error);
     //
     // Remove all handlers which don't handle our error while collecting
     // their base URLs
     //
     foreach ($this->handlers as $candidate) {
         $base_urls[] = $candidate['base_url'];
         if ($candidate['error'] == $error) {
             $candidates[] = $candidate;
         }
     }
     //
     // Remove any base URLs that aren't actually at the base of our request path
     //
     $base_urls = array_filter($base_urls, function ($base_url) use($request) {
         return $base_url ? strpos($request->getURL()->getPath(), $base_url) === 0 : FALSE;
     });
     //
     // Sort the base URLs by longest first
     //
     usort($base_urls, function ($a, $b) {
         $a_len = strlen($a);
         $b_len = strlen($b);
         if ($a_len == $b_len) {
             return 0;
         } else {
             return $a_len > $b_len ? 1 : -1;
         }
     });
     //
     // Iterate over each base URL (longest will be first) and each remaining candidate
     // as soon as a candidate's base URL matches ours, that's our best pick
     //
     foreach ($base_urls as $base_url) {
         foreach ($candidates as $candidate) {
             if ($candidate['base_url'] == $base_url) {
                 $handler = $candidate;
                 break 2;
             }
         }
     }
     if (!$handler) {
         if (isset($this->handlers[$root_hash])) {
             $handler = $this->handlers[$root_hash];
         } else {
             //
             // If we get here it means we did all that for nothing and just want to
             // return the original response...
             //
             return $response;
         }
     }
     $action = is_string($handler['action']) ? self::decompile($handler['action'], array()) : $handler['action'];
     try {
         $response = $this->captureResponse($action, $request, $response, FALSE);
     } catch (Flourish\ContinueException $e) {
     }
     return $response;
 }
Ejemplo n.º 2
0
 /**
  * Sets the request for which the response is intended
  *
  * @access public
  * @param Interfaces\Request $request The request object
  * @return void
  */
 public function setRequest(Interfaces\Request $request)
 {
     $this->request = $request;
     $this->url = $request->getURL();
     $this->lang = $request->getBestAcceptLanguage();
     $this->method = $request->getMethod();
 }