コード例 #1
0
 /**
  * JSON-RPC entrance.
  *
  * @return Void
  */
 public function apiAction()
 {
     Zend_Registry::set('CMS', true);
     /**
      * Prepare the server. Zend_Json_Server cannot work with batched requests natively,
      * so that's taken care of customly here. Therefore, autoEmitResponse is set to false
      * so the server doesn't print the response directly.
      */
     $server = new Zend_Json_Server();
     $server->setClass('Garp_Content_Manager_Proxy');
     $server->setAutoEmitResponse(false);
     if ($this->getRequest()->isPost()) {
         $post = $this->_getJsonRpcRequest();
         $batch = false;
         $responses = array();
         $requests = Zend_Json::decode($post, Zend_Json::TYPE_ARRAY);
         /**
          * Check if this was a batch request. In that case the array is a plain array of
          * arrays. If not, there will be a 'jsonrpc' key in the root of the array.
          */
         $batch = !array_key_exists('jsonrpc', $requests);
         if (!$batch) {
             $requests = array($requests);
         }
         foreach ($requests as $i => $request) {
             $request = $this->_reformJsonRpcRequest($request);
             $requestJson = Zend_Json::encode($request);
             $requestObj = new Zend_Json_Server_Request();
             $requestObj->loadJson($requestJson);
             $server->setRequest($requestObj);
             /**
              * Note; response gets returned by reference, resulting in a $responses array
              * containing all the same items.
              * That's why clone is used here.
              */
             $response = clone $server->handle();
             $responses[] = $response;
         }
         $response = $batch ? '[' . implode(',', $responses) . ']' : $responses[0];
     } else {
         $response = $server->getServiceMap();
     }
     $this->_helper->layout->setLayout('json');
     //  filter out escaped slashes, because they're annoying and not necessary.
     $response = str_replace('\\/', '/', $response);
     $this->view->response = $response;
 }