[795] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * NanoRequest |
---|
| 5 | * |
---|
| 6 | * @package NanoAjax |
---|
| 7 | * |
---|
| 8 | */ |
---|
| 9 | class NanoRequest |
---|
| 10 | { |
---|
| 11 | /** |
---|
| 12 | * Enter description here... |
---|
| 13 | * |
---|
| 14 | * @var array |
---|
| 15 | */ |
---|
| 16 | private $_mArrRequestData = array(); |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * path to user classes |
---|
| 20 | * |
---|
| 21 | * @var string |
---|
| 22 | */ |
---|
| 23 | private $_mStrClassPath = ''; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * default class suffix |
---|
| 27 | * |
---|
| 28 | * @var string |
---|
| 29 | */ |
---|
| 30 | private $_mStrClassSuffix = '.inc.php'; |
---|
| 31 | private $_mStrClassPreffix = 'class.ajax.'; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Enter description here... |
---|
| 36 | * |
---|
| 37 | * @param unknown_type $request_data |
---|
| 38 | */ |
---|
| 39 | public function __construct( $class_path, $class_suffix, $class_preffix ) |
---|
| 40 | { |
---|
| 41 | $this->_mStrClassPath = $class_path; |
---|
| 42 | $this->_mStrClassSuffix = $class_suffix; |
---|
| 43 | $this->_mStrClassPreffix = $class_preffix; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Enter description here... |
---|
| 48 | * |
---|
| 49 | * @param unknown_type $request_data |
---|
| 50 | */ |
---|
| 51 | public function setRequestData($request_data) |
---|
| 52 | { |
---|
| 53 | if( $this->_isRequestDataValid($request_data) ) |
---|
| 54 | { |
---|
| 55 | $this->_mArrRequestData = $request_data; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Enter description here... |
---|
| 62 | * |
---|
| 63 | * @return unknown |
---|
| 64 | */ |
---|
| 65 | public function executeRequest() |
---|
| 66 | { |
---|
| 67 | $this->_loadUserDefinedClass( $this->_mArrRequestData['parameter']['action'] ); |
---|
| 68 | |
---|
| 69 | if( false !== ($userclass = $this->_initializeUserClass($this->_mArrRequestData['parameter']['action'])) ) |
---|
| 70 | { |
---|
| 71 | return $this->_callUserMethod( $userclass, $this->_mArrRequestData['parameter']['mode'], $this->_mArrRequestData['data'] ); |
---|
| 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | $this->_throwException('user class ['.$this->_mArrRequestData['parameter']['action'].'] not found!!!'); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * Enter description here... |
---|
| 81 | * |
---|
| 82 | * @param unknown_type $data |
---|
| 83 | * @return unknown |
---|
| 84 | */ |
---|
| 85 | private function _isRequestDataValid( $data ) |
---|
| 86 | { |
---|
| 87 | if( is_array($data) && count($data) > 0 ) |
---|
| 88 | { |
---|
| 89 | if( isset($data['parameter']) && !empty($data['parameter']) ) |
---|
| 90 | { |
---|
| 91 | if( isset($data['parameter']['action']) && !empty($data['parameter']['action']) ) |
---|
| 92 | { |
---|
| 93 | if( isset($data['parameter']['mode']) && !empty($data['parameter']['mode']) ) |
---|
| 94 | { |
---|
| 95 | return true; |
---|
| 96 | } |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | $this->_throwException('missing or empty Parameter [mode] in (virtual) request!!!'); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | else |
---|
| 103 | { |
---|
| 104 | $this->_throwException('missing or empty Parameter [action] in (virtual) request!!!'); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | else |
---|
| 108 | { |
---|
| 109 | $this->_throwException('missing all parameter data in (virtual) request!!!'); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | $this->_throwException('invalid formed (virtual) request!!!'); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Enter description here... |
---|
| 120 | * |
---|
| 121 | * @param unknown_type $classname |
---|
| 122 | */ |
---|
| 123 | protected function _loadUserDefinedClass( $classname ) |
---|
| 124 | { |
---|
| 125 | ( NanoUtil::isNotEmptyString($classname) && /*!class_exists($classname) &&*/ file_exists($this->_mStrClassPath.$this->_mStrClassPreffix.$classname.$this->_mStrClassSuffix) && is_readable($this->_mStrClassPath.$this->_mStrClassPreffix.$classname.$this->_mStrClassSuffix) ) |
---|
| 126 | ? require_once( $this->_mStrClassPath.$this->_mStrClassPreffix.$classname.$this->_mStrClassSuffix ) |
---|
| 127 | : $this->_throwException('given class {'.$this->_mStrClassPreffix.$classname.$this->_mStrClassSuffix. |
---|
| 128 | '} does not exists'); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Enter description here... |
---|
| 133 | * |
---|
| 134 | * @param unknown_type $classname |
---|
| 135 | * @return unknown |
---|
| 136 | */ |
---|
| 137 | protected function _initializeUserClass( $classname ) |
---|
| 138 | { |
---|
| 139 | return ( class_exists($classname) ) |
---|
| 140 | ? new $classname() |
---|
| 141 | : false; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * Enter description here... |
---|
| 146 | * |
---|
| 147 | * @param unknown_type $userclass |
---|
| 148 | * @param unknown_type $usermethod |
---|
| 149 | * @param unknown_type $userdata |
---|
| 150 | * @return unknown |
---|
| 151 | */ |
---|
| 152 | protected function _callUserMethod( $userclass, $usermethod, $userdata ) |
---|
| 153 | { |
---|
| 154 | return ( method_exists( $userclass, $usermethod ) ) |
---|
| 155 | ? $userclass->$usermethod($userdata) |
---|
| 156 | : $this->_throwException('given user method {'.$usermethod. |
---|
| 157 | '} does not exists in class ['. |
---|
| 158 | get_class($userclass).']'); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Enter description here... |
---|
| 164 | * |
---|
| 165 | * @param unknown_type $msg |
---|
| 166 | */ |
---|
| 167 | private function _throwException( $msg ) |
---|
| 168 | { |
---|
| 169 | throw new Exception($msg); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | ?> |
---|