AjaxHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Ajax Handler
  4. *
  5. * @author vesta, http://vestacp.com/
  6. * @copyright vesta 2010-2011
  7. */
  8. class AjaxHandler
  9. {
  10. static public $instance = null;
  11. public $errors = array();
  12. public $status = TRUE;
  13. /**
  14. * Grab current instance or create it
  15. *
  16. * @return AjaxHandler
  17. */
  18. static function getInstance($request=null)
  19. {
  20. return null == self::$instance ? self::$instance = new self() : self::$instance;
  21. }
  22. /**
  23. * Called functions should reply in the following way
  24. * return $this->reply($result, $data, $msg, $extra);
  25. *
  26. * @param Request $request
  27. * @return mixed
  28. */
  29. function dispatch($request)
  30. {
  31. $method = Request::parseAjaxMethod($request);
  32. $inc_file = V_ROOT_DIR . 'api' . DIRECTORY_SEPARATOR . $method['namespace'] . '.class.php';
  33. if (!is_readable($inc_file))
  34. {
  35. throw new SystemException(Message::INVALID_METHOD);
  36. }
  37. require $inc_file;
  38. $space = new $method['namespace'];
  39. $method = $method['function'] . 'Execute';
  40. if (!method_exists($space, $method))
  41. {
  42. throw new SystemException(Message::INVALID_METHOD);
  43. }
  44. return $space->$method($request);
  45. }
  46. /**
  47. * Prepare the result of method execution into ajax-readable format
  48. *
  49. * @param boolean $result - result of method execution
  50. * @param array $data - data to be replied
  51. * @param string $message - replied message
  52. * @param array $extra - extra data
  53. */
  54. function reply($result, $data, $message = '', $extra = array())
  55. {
  56. return json_encode(array('result' => $result,
  57. 'data' => $data,
  58. 'message' => $message,
  59. 'extra' => $extra,
  60. 'errors' => $this->errors
  61. ));
  62. }
  63. /**
  64. * TODO: delete this method
  65. * @deprecated
  66. */
  67. static function makeReply($reply)
  68. {
  69. print $reply;
  70. }
  71. //
  72. // Error handlers
  73. //
  74. /**
  75. * Generate general error
  76. * @param Exception $error
  77. */
  78. static function generalError($error)
  79. {
  80. self::renderGlobalError(Message::ERROR, Message::GENERAL_ERROR, $error);
  81. }
  82. /**
  83. * Generate protection error
  84. * @param Exception $error
  85. */
  86. static function protectionError($error)
  87. {
  88. self::renderGlobalError(Message::ERROR, Message::PROTECTION_ERROR, $error);
  89. }
  90. /**
  91. * Generate system error
  92. * @param Exception $error
  93. */
  94. static function systemError($error)
  95. {
  96. self::renderGlobalError(Message::ERROR, Message::SYSTEM_ERROR, $error);
  97. }
  98. /**
  99. * Prepare and render the error
  100. * @param Exception $error
  101. */
  102. static function renderGlobalError($type, $message, $error)
  103. {
  104. $trace = $error->getTrace();
  105. AjaxHandler::makeReply(
  106. AjaxHandler::getInstance()->reply(false, $type, $message . ': ' . $error->getMessage(), $trace[0]['file'] . ' / ' . $trace[0]['line'])
  107. );
  108. }
  109. }