addFilter(Amfphp_Core_Gateway::FILTER_DESERIALIZER, $this, 'filterHandler'); $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_DESERIALIZED_REQUEST_HANDLER, $this, 'filterHandler'); $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_EXCEPTION_HANDLER, $this, 'filterHandler'); $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_SERIALIZER, $this, 'filterHandler'); $filterManager->addFilter(Amfphp_Core_Gateway::FILTER_HEADERS, $this, 'filterHeaders'); $this->returnErrorDetails = (isset ($config[Amfphp_Core_Config::CONFIG_RETURN_ERROR_DETAILS]) && $config[Amfphp_Core_Config::CONFIG_RETURN_ERROR_DETAILS]); } /** * If the content type contains the 'json' string, returns this plugin * @param mixed null at call in gateway. * @param String $contentType * @return this or null */ public function filterHandler($handler, $contentType){ if(strpos($contentType, self::CONTENT_TYPE) !== false){ return $this; } } /** * deserialize * @see Amfphp_Core_Common_IDeserializer * @param array $getData * @param array $postData * @param string $rawPostData * @return string */ public function deserialize(array $getData, array $postData, $rawPostData){ return $getData; } /** * Retrieve the serviceName, methodName and parameters from the PHP object * representing the JSON string * call service * @see Amfphp_Core_Common_IDeserializedRequestHandler * @param array $deserializedRequest * @param Amfphp_Core_Common_ServiceRouter $serviceRouter * @return the service call response */ public function handleDeserializedRequest($deserializedRequest, Amfphp_Core_Common_ServiceRouter $serviceRouter){ if(isset ($deserializedRequest['serviceName'])){ $serviceName = $deserializedRequest['serviceName']; }else{ throw new Exception('Service name field missing in call parameters \n' . print_r($deserializedRequest, true)); } if(isset ($deserializedRequest['methodName'])){ $methodName = $deserializedRequest['methodName']; }else{ throw new Exception('MethodName field missing in call parameters \n' . print_r($deserializedRequest, true)); } $parameters = array(); $paramCounter = 1; while(isset ($deserializedRequest["p$paramCounter"])){ $parameters[] = $deserializedRequest["p$paramCounter"]; $paramCounter++; } return $serviceRouter->executeServiceCall($serviceName, $methodName, $parameters); } /** * handle exception * @see Amfphp_Core_Common_IExceptionHandler * @param Exception $exception * @return stdClass */ public function handleException(Exception $exception){ $error = new stdClass(); $error->message = $exception->getMessage(); $error->code = $exception->getCode(); if($this->returnErrorDetails){ $error->file = $exception->getFile(); $error->line = $exception->getLine(); $error->stack = $exception->getTraceAsString(); } return (object)array('error' => $error); } /** * Encode the PHP object returned from the service call into a JSON string * @see Amfphp_Core_Common_ISerializer * @param mixed $data * @return string the encoded JSON string sent to JavaScript */ public function serialize($data){ $encoded = json_encode($data); if(isset ($_GET['callback'])){ return $_GET['callback'] . '(' . $encoded . ');'; }else{ return $encoded; } } /** * sets return content type to json * @param array $headers * @param string $contentType * @return array */ public function filterHeaders($headers, $contentType){ if ($contentType == self::CONTENT_TYPE) { $headers['Content-Type'] = 'application/json'; return $headers; } } } ?>