1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * NanoController |
---|
5 | * |
---|
6 | * @package NanoAjax |
---|
7 | * |
---|
8 | */ |
---|
9 | class NanoController |
---|
10 | { |
---|
11 | /** |
---|
12 | * holds NanoJsonConverter object |
---|
13 | * |
---|
14 | * @var NanoJsonConverter |
---|
15 | */ |
---|
16 | protected $_mObjJsonConverter; |
---|
17 | |
---|
18 | /** |
---|
19 | * holds JSON object |
---|
20 | * |
---|
21 | * @var JSON |
---|
22 | */ |
---|
23 | protected $_mObjJson; |
---|
24 | |
---|
25 | /** |
---|
26 | * path to user classes |
---|
27 | * |
---|
28 | * @var string |
---|
29 | */ |
---|
30 | protected $_mStrClassPath = ''; |
---|
31 | |
---|
32 | /** |
---|
33 | * default class suffix |
---|
34 | * |
---|
35 | * @var string |
---|
36 | */ |
---|
37 | protected $_mStrClassSuffix = '.inc.php'; |
---|
38 | protected $_mStrClassPreffix = 'class.ajax.'; |
---|
39 | |
---|
40 | /** |
---|
41 | * holds NanoRequest object for executing request |
---|
42 | * |
---|
43 | * @var NanoRequest |
---|
44 | */ |
---|
45 | protected $_mObjNanoRequest; |
---|
46 | |
---|
47 | /** |
---|
48 | * holds final JSON encoded result of virtual requests |
---|
49 | * |
---|
50 | * @var string |
---|
51 | */ |
---|
52 | protected $_mStrVirtualRequestsResult = ''; |
---|
53 | |
---|
54 | |
---|
55 | /** |
---|
56 | * Constructor |
---|
57 | * |
---|
58 | */ |
---|
59 | function __construct() |
---|
60 | { |
---|
61 | $this->_mObjJsonConverter = &Factory::newInstance('NanoJsonConverter'); |
---|
62 | $this->_mObjJson = &Factory::newInstance('Services_JSON'); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | /** |
---|
67 | * Enter description here... |
---|
68 | * |
---|
69 | * @param string $path |
---|
70 | */ |
---|
71 | public function setClassPath( $path ) |
---|
72 | { |
---|
73 | if( is_string($path) && !empty($path) && is_dir($path) && is_readable($path) ) |
---|
74 | { |
---|
75 | $this->_mStrClassPath = $path . (($path[strlen($path)-1] !== '/') ? '/' : ''); |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | $this->_throw(__METHOD__,'php class path ['.$path.'] not valid!!!'); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | /** |
---|
85 | * Enter description here... |
---|
86 | * |
---|
87 | * @param string $suffix |
---|
88 | */ |
---|
89 | public function setClassSuffix( $suffix ) |
---|
90 | { |
---|
91 | if( is_string($suffix) && !empty($suffix) ) |
---|
92 | { |
---|
93 | $this->_mStrClassSuffix = $suffix; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | $this->_throw(__METHOD__,'php class suffix not valid!!!'); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | public function setClassPreffix( $preffix ) |
---|
102 | { |
---|
103 | if( is_string($preffix) && !empty($preffix) ) |
---|
104 | { |
---|
105 | $this->_mStrClassPreffix = $preffix; |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | $this->_throw(__METHOD__,'php class preffix not valid!!!'); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Enter description here... |
---|
115 | * |
---|
116 | * @param string $json_data |
---|
117 | */ |
---|
118 | public function setJsonData( $json_data ) |
---|
119 | { |
---|
120 | $this->_mObjJsonConverter->setJsonString($json_data); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * Enter description here... |
---|
126 | * |
---|
127 | * @param boolean $print_data |
---|
128 | * @return unknown |
---|
129 | */ |
---|
130 | public function iterateOverVirtualRequests() |
---|
131 | { |
---|
132 | $this->_mObjNanoRequest = &Factory::newInstance('NanoRequest', $this->_mStrClassPath, $this->_mStrClassSuffix, $this->_mStrClassPreffix ); |
---|
133 | $return_data = array(); |
---|
134 | |
---|
135 | foreach( $this->_mObjJsonConverter->getJsonDecodedAsArray() as $request_identifier => $request_data) |
---|
136 | { |
---|
137 | $output = array(); |
---|
138 | |
---|
139 | try |
---|
140 | { |
---|
141 | $this->_mObjNanoRequest->setRequestData($request_data); |
---|
142 | |
---|
143 | $output['data'] = $this->_mObjNanoRequest->executeRequest(); |
---|
144 | } |
---|
145 | catch (Exception $exception) |
---|
146 | { |
---|
147 | $output['exception'] = $exception->getMessage(); |
---|
148 | } |
---|
149 | |
---|
150 | $return_data[$request_identifier] = $output; |
---|
151 | } |
---|
152 | |
---|
153 | $this->_mStrVirtualRequestsResult = $this->_mObjJson->encode( $return_data ); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** |
---|
158 | * returns result of virtual request(s) as encoded JSON string |
---|
159 | * |
---|
160 | * @return string |
---|
161 | */ |
---|
162 | public function getResultData() |
---|
163 | { |
---|
164 | return $this->_mStrVirtualRequestsResult; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | * outputs result of virtual request(s) as encoded JSON string |
---|
170 | * |
---|
171 | */ |
---|
172 | public function outputResultData() |
---|
173 | { |
---|
174 | echo $this->_mStrVirtualRequestsResult; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * generates an (json) exception and output it |
---|
180 | * |
---|
181 | * @param string $method |
---|
182 | * @param string $msg |
---|
183 | */ |
---|
184 | protected function _throw( $method, $msg ) |
---|
185 | { |
---|
186 | die( $this->_mObjJson->encode(array('__NANOAJAX_SYSTEM_EXCEPTION__' => array('exception' => $msg))) ); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Throw error on all virtual requests |
---|
191 | * |
---|
192 | * @param string $message The message of the exception |
---|
193 | */ |
---|
194 | public function throwErrorOnAllVirtualRequests($message) |
---|
195 | { |
---|
196 | $output = array(); |
---|
197 | foreach ($this->_mObjJsonConverter->getJsonDecodedAsArray() as $requestIdentifier => $requestData) |
---|
198 | $output[$requestIdentifier] = array('exception' => $message); |
---|
199 | die($this->_mObjJson->encode($output)); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | ?> |
---|