Exemple #1
0
 protected function message(Exception $exception)
 {
     $this->dispatch('message');
     if (!$exception instanceof RestException) {
         $exception = new RestException(500, $this->productionMode ? null : $exception->getMessage(), array(), $exception);
     }
     $this->exception = $exception;
     $method = 'handle' . $exception->getCode();
     $handled = false;
     foreach ($this->errorClasses as $className) {
         if (method_exists($className, $method)) {
             $obj = Scope::get($className);
             if ($obj->{$method}()) {
                 $handled = true;
             }
         }
     }
     if ($handled) {
         return;
     }
     if (!isset($this->responseFormat)) {
         $this->responseFormat = Scope::get('JsonFormat');
     }
     $this->composeHeaders($exception);
     /**
      * @var iCompose Default Composer
      */
     $compose = Scope::get(Defaults::$composeClass);
     $this->responseData = $this->responseFormat->encode($compose->message($exception), !$this->productionMode);
     $this->respond();
 }
 /**
  * Encodes the response in the preferred format and sends back.
  *
  * @param mixed       $data array or scalar value or iValueObject or null
  * @param int         $statusCode
  * @param string|null $statusMessage
  */
 public function sendData($data, $statusCode = 0, $statusMessage = null)
 {
     //$this->log []= ob_get_clean ();
     //only GET method should be cached if allowed by API developer
     $expires = $this->requestMethod == 'GET' ? Defaults::$headerExpires : 0;
     $cacheControl = Defaults::$headerCacheControl[0];
     if ($expires > 0) {
         $cacheControl = $this->apiMethodInfo->accessLevel ? 'private, ' : 'public, ';
         $cacheControl .= end(Defaults::$headerCacheControl);
         $cacheControl = str_replace('{expires}', $expires, $cacheControl);
         $expires = gmdate('D, d M Y H:i:s \\G\\M\\T', time() + $expires);
     }
     @header('Cache-Control: ' . $cacheControl);
     @header('Expires: ' . $expires);
     @header('X-Powered-By: Luracast Restler v' . Restler::VERSION);
     if (Defaults::$crossOriginResourceSharing && isset($_SERVER['HTTP_ORIGIN'])) {
         header('Access-Control-Allow-Origin: ' . (Defaults::$accessControlAllowOrigin == '*' ? $_SERVER['HTTP_ORIGIN'] : Defaults::$accessControlAllowOrigin));
         header('Access-Control-Allow-Credentials: true');
         header('Access-Control-Max-Age: 86400');
     }
     if (isset($this->apiMethodInfo->metadata['header'])) {
         foreach ($this->apiMethodInfo->metadata['header'] as $header) {
             @header($header, true);
         }
     }
     /**
      *
      * @var iRespond DefaultResponder
      */
     $responder = Util::setProperties(Defaults::$responderClass, isset($this->apiMethodInfo->metadata) ? $this->apiMethodInfo->metadata : null);
     if ($statusCode == 0) {
         if (isset($this->apiMethodInfo->metadata['status'])) {
             $this->setStatus($this->apiMethodInfo->metadata['status']);
         }
         $data = $responder->formatResponse($data);
         $data = $this->responseFormat->encode($data, !$this->productionMode);
         $postProcess = '_' . $this->apiMethod . '_' . $this->responseFormat->getExtension();
         if (isset($this->apiClassInstance) && method_exists($this->apiClassInstance, $postProcess)) {
             $data = call_user_func(array($this->apiClassInstance, $postProcess), $data);
         }
     } else {
         $message = RestException::$codes[$statusCode] . (empty($statusMessage) ? '' : ': ' . $statusMessage);
         $this->setStatus($statusCode);
         $data = $this->responseFormat->encode($responder->formatError($statusCode, $message), !$this->productionMode);
     }
     $this->responseFormat->setCharset(Defaults::$charset);
     $charset = $this->responseFormat->getCharset() ?: Defaults::$charset;
     @header('Content-Type: ' . (Defaults::$useVendorMIMEVersioning ? 'application/vnd.' . Defaults::$apiVendor . "-v{$this->requestedApiVersion}" . '+' . $this->responseFormat->getExtension() : $this->responseFormat->getMIME()) . '; charset=' . $charset);
     @header('Content-Language: ' . Defaults::$language);
     //handle throttling
     if (Defaults::$throttle) {
         $elapsed = time() - $this->startTime;
         if (Defaults::$throttle / 1000.0 > $elapsed) {
             usleep(1000000.0 * (Defaults::$throttle / 1000.0 - $elapsed));
         }
     }
     die($data);
 }