11<?php
2-
32/**
43 * Ajax Handler
54 *
65 * @author vesta, http://vestacp.com/
7- * @copyright vesta 2010
6+ * @copyright vesta 2010-2011
87 */
9- class AjaxHandler {
8+ class AjaxHandler
9+ {
1010
1111 static public $ instance = null ;
1212
@@ -16,23 +16,26 @@ class AjaxHandler {
1616 /**
1717 * Grab current instance or create it
1818 *
19- * @return <type>
19+ * @return AjaxHandler
2020 */
21- static function getInstance ($ request =null ) {
21+ static function getInstance ($ request =null )
22+ {
2223 return null == self ::$ instance ? self ::$ instance = new self () : self ::$ instance ;
2324 }
2425
2526 /**
2627 * Called functions should reply in the following way
2728 * return $this->reply($result, $data, $msg, $extra);
2829 *
29- * @param type $request
30- * @return type
30+ * @param Request $request
31+ * @return mixed
3132 */
32- function dispatch ($ request ) {
33+ function dispatch ($ request )
34+ {
3335 $ method = Request::parseAjaxMethod ($ request );
3436 $ inc_file = V_ROOT_DIR . 'api ' . DIRECTORY_SEPARATOR . $ method ['namespace ' ] . '.class.php ' ;
35- if (!is_readable ($ inc_file )) {
37+ if (!is_readable ($ inc_file ))
38+ {
3639 throw new SystemException (Message::INVALID_METHOD );
3740 }
3841
@@ -41,14 +44,24 @@ function dispatch($request) {
4144 $ space = new $ method ['namespace ' ];
4245 $ method = $ method ['function ' ] . 'Execute ' ;
4346
44- if (!method_exists ($ space , $ method )) {
47+ if (!method_exists ($ space , $ method ))
48+ {
4549 throw new SystemException (Message::INVALID_METHOD );
4650 }
4751
4852 return $ space ->$ method ($ request );
4953 }
5054
51- function reply ($ result , $ data , $ message = '' , $ extra = array ()) {
55+ /**
56+ * Prepare the result of method execution into ajax-readable format
57+ *
58+ * @param boolean $result - result of method execution
59+ * @param array $data - data to be replied
60+ * @param string $message - replied message
61+ * @param array $extra - extra data
62+ */
63+ function reply ($ result , $ data , $ message = '' , $ extra = array ())
64+ {
5265 return json_encode (array ('result ' => $ result ,
5366 'data ' => $ data ,
5467 'message ' => $ message ,
@@ -57,30 +70,55 @@ function reply($result, $data, $message = '', $extra = array()) {
5770 ));
5871 }
5972
60- static function makeReply ($ reply ) {
73+ /**
74+ * TODO: delete this method
75+ * @deprecated
76+ */
77+ static function makeReply ($ reply )
78+ {
6179 print $ reply ;
6280 }
6381
6482 //
6583 // Error handlers
6684 //
6785
68- static function generalError ($ error ) {
86+ /**
87+ * Generate general error
88+ * @param Exception $error
89+ */
90+ static function generalError ($ error )
91+ {
6992 self ::renderGlobalError (Message::ERROR , Message::GENERAL_ERROR , $ error );
7093 }
7194
72- static function protectionError ($ error ) {
95+ /**
96+ * Generate protection error
97+ * @param Exception $error
98+ */
99+ static function protectionError ($ error )
100+ {
73101 self ::renderGlobalError (Message::ERROR , Message::PROTECTION_ERROR , $ error );
74102 }
75103
76- static function systemError ($ error ) {
104+ /**
105+ * Generate system error
106+ * @param Exception $error
107+ */
108+ static function systemError ($ error )
109+ {
77110 self ::renderGlobalError (Message::ERROR , Message::SYSTEM_ERROR , $ error );
78111 }
79112
80- static function renderGlobalError ($ type , $ message , $ error ) {
113+ /**
114+ * Prepare and render the error
115+ * @param Exception $error
116+ */
117+ static function renderGlobalError ($ type , $ message , $ error )
118+ {
81119 $ trace = $ error ->getTrace ();
82120 AjaxHandler::makeReply (
83- AjaxHandler::getInstance ()->reply (false , $ type , $ message . ': ' . $ error ->getMessage (), $ trace [0 ]['file ' ] . ' / ' . $ trace [0 ]['line ' ])
121+ AjaxHandler::getInstance ()->reply (false , $ type , $ message . ': ' . $ error ->getMessage (), $ trace [0 ]['file ' ] . ' / ' . $ trace [0 ]['line ' ])
84122 );
85123 }
86124
0 commit comments