Esempio n. 1
0
 /**
     Tries to find a response handler for $request->getType().
 
     $request must be one of:
 
     - A JSONRequest object. Its getType() value is used
     as the handler lookup key.
 
     - An array, in which case the JSONRequest($request)
     is called to construct a request.
 
     - A string containing '{', in which case it is assumed
     to be message JSON and is passed on to JSONRequest($request).
 
     - A string containing a lookup key. If it is found, the response
     handler is passed that key rather than a JSONRequest() object.
 
     If $f is a string and does not map to a current entry then
     paths which have been registered in addAutoloadDir() are searched
     for a file names EVENT_TYPE.inc.php. If one is found, it is
     include and the mapping is re-checked (because presumably the
     file will register a handler).
 
     Returns:
 
     If no handler is found, null is returned. If one is found,
     it is handled as described in mapResponder()
     and on success a JSONResponder is returned. If an error is encountered
     in the handling of the request, the contained response will
     be a wrapper for that error.
 */
 public static function getResponder($request)
 {
     $key = null;
     function reportError($r, $msg)
     {
         return new JSONResponder_Error($r, __CLASS__ . '::getResponder(): ' . $msg);
     }
     if (is_string($request)) {
         if (false === strpos($request, '{')) {
             $key = $request;
         } else {
             try {
                 $request = new JSONRequest($request);
                 $key = $request->getType();
             } catch (Exception $e) {
                 if (1) {
                     throw $e;
                 } else {
                     return new JSONResponder_Error(null, "EXCEPTION while creating JSONRequest from request data: " . $e->getMessage());
                 }
             }
         }
     } else {
         if ($request instanceof JSONRequest) {
             $key = $request->getType();
         } else {
             if (@is_array($request)) {
                 $request = new JSONRequest($request);
                 $key = $request->getType();
             } else {
                 // TODO: create an Exception response type to wrap this, and return it:
                 return new JSONResponder_Error(new JSONRequest(), "Illegal arguments to " . __CLASS__ . "::getResponder([unknown])");
             }
         }
     }
     $f = @self::$handlers[$key];
     $request->set('arrivalTime', $request->get('arrivalTime', JSONMessage::timestamp()));
     while (!$f) {
         foreach (self::$DispatcherPath as $dir) {
             $fn = $dir . '/' . $key . '.inc.php';
             if (@file_exists($fn)) {
                 require_once $fn;
                 $f = @self::$handlers[$key];
                 break;
             }
         }
         if (!$f) {
             return null;
         }
         //reportErr($request,"No Responder found for message type '".$key."'.");
         break;
     }
     if (is_string($f)) {
         if (class_exists($f)) {
             $x = new $f($request);
             if ($x instanceof JSONResponder) {
                 return $x;
             } else {
                 if ($x instanceof JSONResponse) {
                     return new JSONResponder_Generic($request, $x);
                 } else {
                     return reportError($r, "class mapped to [" . $key . "] is neither " . "a JSONResponder nor a JSONResponse!");
                 }
             }
         } else {
             if (function_exists($f)) {
                 $x = $f($request);
                 if ($x instanceof JSONResponder) {
                     return $x;
                 } else {
                     if ($x instanceof JSONResponse) {
                         return new JSONResponder_Generic($request, $x);
                     } else {
                         return reportError($request, "JSONMessageDispatcher mapped to [" . $key . "] returned an object of an unknown type!");
                     }
                 }
             } else {
                 return reportError($request, "JSONResponder mapped to [" . $key . "] names neither a class nor a function!");
             }
         }
     } else {
         if (is_callable($f)) {
             $x = $f($request);
             if ($x instanceof JSONResponder) {
                 return $x;
             } else {
                 if ($x instanceof JSONResponse) {
                     return new JSONResponder_Generic($request, $x);
                 } else {
                     return reportError($request, "JSONMessageDispatcher mapped to [" . $key . "] returned an object of an unknown typ!");
                 }
             }
         } else {
             return reportError($request, "JSONMessageDispatcher handler mapped to [" . $key . "] is not callable!");
         }
     }
 }