<?php defined('SYSPATH') or die('No direct script access.'); /** * @author: Vad Skakov <*****@*****.**> */ defined('EXT') or define('EXT', '.php'); RestfulAPI::initialize();
private function _response() { if (!isset(Response::$messages[$this->_response_httpCode])) { $this->_response_httpCode = 400; } if ($this->responseAsJSON) { if (!$this->isError()) { // SUCCESS $jsonData = $this->_response_data; if (NULL !== $this->responseLocation) { //$this->response->headers('Location', $this->responseLocation); } } else { // ERROR if ($this->_response_data instanceof Exception) { $exception = $this->_response_data; $message = $exception->getMessage(); if (!$exception instanceof RestfulAPI_Exception) { $this->_response_httpCode = $exception->getCode(); } } else { $message = $this->_response_data; } if (!is_string($message)) { $message = isset($exception) ? get_class($exception) : 'Undefined error'; } $jsonData = $this->getErrorResponse($message, $this->_response_operationCode); if (isset($this->_response_errors)) { $errors = []; foreach ((array) $this->_response_errors as $field => $error) { $errors[] = ['key' => $field, 'description' => $error]; } $jsonData['parameters'] = $errors; } } $this->response->headers('cache-control', 'no-cache, no-store, max-age=0, must-revalidate'); $this->response->headers('content-type', 'application/json; charset=utf-8'); try { $this->response->status($this->_response_httpCode); } catch (Exception $e) { $this->response->status(500); $this->_response_operationCode = $this->_response_httpCode; } if (isset($exception)) { $_traceType = RestfulAPI::config('onerror.debug.exception', 'string'); if ($_traceType) { $this->setDebugData(['exception' => ['source' => self::exceptionString($exception), 'trace' => $_traceType !== 'array' ? $exception->getTraceAsString() : $exception->getTrace()]]); } } try { $this->setResponseBody($jsonData); } catch (Exception $e) { $exception = !isset($exception) ? $e : new Exception($e->getMessage(), $e->getCode(), $exception); $this->response->status(500); $this->setResponseBody(['message' => strtr('Error while formatting response:message', [':message' => !Helpers_Core::isProduction() ? ': ' . $e->getMessage() : '']), 'code' => 0]); } // LOGGING EXCEPTION if (isset($exception)) { $_logCodes = RestfulAPI::config('onerror.log.http_codes', []); if (TRUE === $_logCodes || Helpers_Arr::inArray($this->response->status(), $_logCodes)) { Kohana::$log->add($this->response->status() == 500 ? Log::ERROR : Log::INFO, self::exceptionString($exception), NULL, ['exception' => $exception]); } } $this->response->send_headers(TRUE); exit($this->response); } else { if ($this->isError()) { if ($this->_response_data instanceof Exception) { Kohana_Exception::response($this->_response_data); } throw HTTP_Exception::factory($this->_response_httpCode, '[ :errno ] :error', [':errno' => $this->_response_operationCode, ':error' => $this->_response_data]); } else { $this->response->body($this->_response_data); } } }