Ejemplo n.º 1
0
 /**
  * Run the RPC and return its output
  *
  * @param  RPC       $rpc
  * @return array
  * @throws Exception when parameters are not valid
  */
 protected function dispatchRPC(RPC $rpc)
 {
     $response = array('type' => 'rpc', 'tid' => $rpc->getId(), 'action' => $rpc->getAction(), 'method' => $rpc->getMethod(), 'result' => null);
     if (!$this->api->hasAction($rpc->getAction())) {
         throw new Exception('Action ' . $rpc->getAction() . ' does not exist');
     }
     $action = $this->api->getAction($rpc->getAction());
     // Verify the method exists
     if (!$action->hasMethod($rpc->getMethod())) {
         throw new Exception('Method ' . $rpc->getMethod() . ' does not exist');
     }
     // Verify that we received enough parameters to call the method
     if ($action->getMethod($rpc->getMethod())->getNumberOfParameters() > count($rpc->getData())) {
         throw new Exception('Invalid parameter count');
     }
     $object = $this->manager->get($action->getObjectName());
     // Trigger a RPC dispatch event
     $eventVars = array('object' => $object, 'rpc' => $rpc);
     $result = $this->getEventManager()->trigger(DirectEvent::EVENT_DISPATCH_RPC, $this, $eventVars);
     if ($result->stopped()) {
         return $result->last();
     }
     try {
         // Fetch result from the function call
         $response['result'] = call_user_func_array(array($object, $rpc->getMethod()), $rpc->getData());
     } catch (Exception $e) {
         $error = array('type' => 'exception', 'message' => 'An unhandled exception occured', 'where' => '');
         if ($this->isDebugMode()) {
             $error['message'] = $e->getMessage();
             $error['where'] = $e->getTraceAsString();
         }
         $response = $error;
     }
     return $response;
 }
Ejemplo n.º 2
0
 /**
  * @param  array $apiConfig
  * @return Api
  */
 public function buildApi(array $apiConfig)
 {
     $actions = array();
     // legacy code, probably to be removed
     if (isset($apiConfig['modules']) && is_array($apiConfig['modules'])) {
         $actions = ArrayUtils::merge($actions, $this->buildDirectoryApi($apiConfig['modules']));
     }
     if (isset($apiConfig['services']) && is_array($apiConfig['services'])) {
         $actions = ArrayUtils::merge($actions, $this->buildServiceApi($apiConfig['services']));
     }
     $api = new Api();
     /* @var $actions \KJSencha\Direct\Remoting\Api\Object\Action[] */
     foreach ($actions as $name => $action) {
         $api->addAction($name, $action);
     }
     return $api;
 }