Exemplo n.º 1
0
 /**
  * Singleton, returns instance
  *
  * @return Core_API_Proxy
  */
 public static function getInstance()
 {
     if (self::$instance == null) {
         $c = __CLASS__;
         self::$instance = new $c();
     }
     return self::$instance;
 }
Exemplo n.º 2
0
 /**
  * Handles the request to the API.
  * It first checks that the method called (parameter 'method') is available in the module (it means that the method exists and is public)
  * It then reads the parameters from the request string and throws an exception if there are missing parameters.
  * It then calls the API Proxy which will call the requested method.
  * 
  * @return mixed The data resulting from the API call  
  */
 public function process()
 {
     // read the format requested for the output data
     //$outputFormat = strtolower(Core_Common::getRequestVar('format', 'xml', 'string', $this->request));
     $outputFormat = strtolower(Core_Common::getRequestVar('format', 'json', 'string', $this->request));
     // create the response
     $response = new API_ResponseBuilder($this->request, $outputFormat);
     try {
         // read parameters
         $moduleMethod = Core_Common::getRequestVar('method', null, null, $this->request);
         list($module, $method) = $this->extractModuleAndMethod($moduleMethod);
         /* Load the request module */
         $api_file = "Module" . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . "API.php";
         /* Include the module */
         if (!file_exists($api_file)) {
             echo "API not found " . $requested_module;
             return;
         }
         require_once $api_file;
         $api_class = "Module_" . $module . "_API";
         if (!class_exists($api_class)) {
             // Error
             throw new Exception("Error: Unknown class " . $requested_module);
         }
         // Dynamically create the class
         $this->api = new $api_class();
         // Dynamically call the action
         if (!method_exists($this->api, $method)) {
             // Error
             throw new Exception("Error: Unknown method " . $method);
             return;
         }
         // call the method
         $returnedValue = API_Proxy::getInstance()->call($api_class, $method, $this->request);
         $toReturn = $response->getResponse($returnedValue);
     } catch (Exception $e) {
         $toReturn = $response->getResponseException($e);
     }
     return $toReturn;
 }