@@ -19,260 +19,260 @@ |
||
19 | 19 | */ |
20 | 20 | class JsonFormat extends Format |
21 | 21 | { |
22 | - /** |
|
23 | - * @var boolean|null shim for json_encode option JSON_PRETTY_PRINT set |
|
24 | - * it to null to use smart defaults |
|
25 | - */ |
|
26 | - public static $prettyPrint = null; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var boolean|null shim for json_encode option JSON_UNESCAPED_SLASHES |
|
30 | - * set it to null to use smart defaults |
|
31 | - */ |
|
32 | - public static $unEscapedSlashes = null; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var boolean|null shim for json_encode JSON_UNESCAPED_UNICODE set it |
|
36 | - * to null to use smart defaults |
|
37 | - */ |
|
38 | - public static $unEscapedUnicode = null; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var boolean|null shim for json_decode JSON_BIGINT_AS_STRING set it to |
|
42 | - * null to |
|
43 | - * use smart defaults |
|
44 | - */ |
|
45 | - public static $bigIntAsString = null; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var boolean|null shim for json_decode JSON_NUMERIC_CHECK set it to |
|
49 | - * null to |
|
50 | - * use smart defaults |
|
51 | - */ |
|
52 | - public static $numbersAsNumbers = null; |
|
53 | - |
|
54 | - const MIME = 'application/json'; |
|
55 | - const EXTENSION = 'json'; |
|
56 | - |
|
57 | - public function encode($data, $humanReadable = false) |
|
58 | - { |
|
59 | - if (!is_null(self::$prettyPrint)) { |
|
60 | - $humanReadable = self::$prettyPrint; |
|
61 | - } |
|
62 | - if (is_null(self::$unEscapedSlashes)) { |
|
63 | - self::$unEscapedSlashes = $humanReadable; |
|
64 | - } |
|
65 | - if (is_null(self::$unEscapedUnicode)) { |
|
66 | - self::$unEscapedUnicode = $this->charset == 'utf-8'; |
|
67 | - } |
|
68 | - |
|
69 | - $options = 0; |
|
70 | - |
|
71 | - if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
72 | - || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
73 | - ) { |
|
74 | - |
|
75 | - if ($humanReadable) { |
|
76 | - $options |= JSON_PRETTY_PRINT; |
|
77 | - } |
|
78 | - |
|
79 | - if (self::$unEscapedSlashes) { |
|
80 | - $options |= JSON_UNESCAPED_SLASHES; |
|
81 | - } |
|
82 | - |
|
83 | - if (self::$bigIntAsString) { |
|
84 | - $options |= JSON_BIGINT_AS_STRING; |
|
85 | - } |
|
86 | - |
|
87 | - if (self::$unEscapedUnicode) { |
|
88 | - $options |= JSON_UNESCAPED_UNICODE; |
|
89 | - } |
|
90 | - |
|
91 | - if (self::$numbersAsNumbers) { |
|
92 | - $options |= JSON_NUMERIC_CHECK; |
|
93 | - } |
|
94 | - |
|
95 | - $result = json_encode(Obj::toArray($data, true), $options); |
|
96 | - $this->handleJsonError(); |
|
97 | - |
|
98 | - return $result; |
|
99 | - } |
|
100 | - |
|
101 | - $result = json_encode(Obj::toArray($data, true)); |
|
102 | - $this->handleJsonError(); |
|
103 | - |
|
104 | - if ($humanReadable) { |
|
105 | - $result = $this->formatJson($result); |
|
106 | - } |
|
107 | - |
|
108 | - if (self::$unEscapedUnicode) { |
|
109 | - $result = preg_replace_callback( |
|
110 | - '/\\\u(\w\w\w\w)/', |
|
111 | - function ($matches) { |
|
112 | - if (function_exists('mb_convert_encoding')) { |
|
113 | - return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
|
114 | - } else { |
|
115 | - return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1])); |
|
116 | - } |
|
117 | - }, |
|
118 | - $result |
|
119 | - ); |
|
120 | - } |
|
121 | - |
|
122 | - if (self::$unEscapedSlashes) { |
|
123 | - $result = str_replace('\/', '/', $result); |
|
124 | - } |
|
125 | - |
|
126 | - return $result; |
|
127 | - } |
|
128 | - |
|
129 | - public function decode($data) |
|
130 | - { |
|
131 | - if (empty($data)) { |
|
132 | - return null; |
|
133 | - } |
|
134 | - |
|
135 | - $options = 0; |
|
136 | - if (self::$bigIntAsString) { |
|
137 | - if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
138 | - || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
139 | - ) { |
|
140 | - $options |= JSON_BIGINT_AS_STRING; |
|
141 | - } else { |
|
142 | - $data = preg_replace( |
|
143 | - '/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', |
|
144 | - ': "$1"', |
|
145 | - $data |
|
146 | - ); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - try { |
|
151 | - $decoded = json_decode($data, true, 512, $options); |
|
152 | - $this->handleJsonError(); |
|
153 | - } catch (\RuntimeException $e) { |
|
154 | - throw new RestException(400, $e->getMessage()); |
|
155 | - } |
|
156 | - |
|
157 | - if (strlen($data) && $decoded === null || $decoded === $data) { |
|
158 | - throw new RestException(400, 'Error parsing JSON'); |
|
159 | - } |
|
160 | - |
|
161 | - return $decoded; //Obj::toArray($decoded); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Pretty print JSON string |
|
166 | - * |
|
167 | - * @param string $json |
|
168 | - * |
|
169 | - * @return string formatted json |
|
170 | - */ |
|
171 | - private function formatJson($json) |
|
172 | - { |
|
173 | - $tab = ' '; |
|
174 | - $newJson = ''; |
|
175 | - $indentLevel = 0; |
|
176 | - $inString = false; |
|
177 | - $len = strlen($json); |
|
178 | - for ($c = 0; $c < $len; $c++) { |
|
179 | - $char = $json [$c]; |
|
180 | - switch ($char) { |
|
181 | - case '{' : |
|
182 | - case '[' : |
|
183 | - if (!$inString) { |
|
184 | - $newJson .= $char . "\n" . |
|
185 | - str_repeat($tab, $indentLevel + 1); |
|
186 | - $indentLevel++; |
|
187 | - } else { |
|
188 | - $newJson .= $char; |
|
189 | - } |
|
190 | - break; |
|
191 | - case '}' : |
|
192 | - case ']' : |
|
193 | - if (!$inString) { |
|
194 | - $indentLevel--; |
|
195 | - $newJson .= "\n" . |
|
196 | - str_repeat($tab, $indentLevel) . $char; |
|
197 | - } else { |
|
198 | - $newJson .= $char; |
|
199 | - } |
|
200 | - break; |
|
201 | - case ',' : |
|
202 | - if (!$inString) { |
|
203 | - $newJson .= ",\n" . |
|
204 | - str_repeat($tab, $indentLevel); |
|
205 | - } else { |
|
206 | - $newJson .= $char; |
|
207 | - } |
|
208 | - break; |
|
209 | - case ':' : |
|
210 | - if (!$inString) { |
|
211 | - $newJson .= ': '; |
|
212 | - } else { |
|
213 | - $newJson .= $char; |
|
214 | - } |
|
215 | - break; |
|
216 | - case '"' : |
|
217 | - if ($c == 0) { |
|
218 | - $inString = true; |
|
219 | - } elseif ($c > 0 && $json [$c - 1] != '\\') { |
|
220 | - $inString = !$inString; |
|
221 | - } |
|
222 | - default : |
|
223 | - $newJson .= $char; |
|
224 | - break; |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - return $newJson; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Throws an exception if an error occurred during the last JSON encoding/decoding |
|
233 | - * |
|
234 | - * @return void |
|
235 | - * @throws \RuntimeException |
|
236 | - */ |
|
237 | - protected function handleJsonError() |
|
238 | - { |
|
239 | - if (function_exists('json_last_error_msg') && json_last_error() !== JSON_ERROR_NONE) { |
|
240 | - |
|
241 | - // PHP >= 5.5.0 |
|
242 | - |
|
243 | - $message = json_last_error_msg(); |
|
244 | - |
|
245 | - } elseif (function_exists('json_last_error')) { |
|
246 | - |
|
247 | - // PHP >= 5.3.0 |
|
248 | - |
|
249 | - switch (json_last_error()) { |
|
250 | - case JSON_ERROR_NONE: |
|
251 | - break; |
|
252 | - case JSON_ERROR_DEPTH: |
|
253 | - $message = 'maximum stack depth exceeded'; |
|
254 | - break; |
|
255 | - case JSON_ERROR_STATE_MISMATCH: |
|
256 | - $message = 'underflow or the modes mismatch'; |
|
257 | - break; |
|
258 | - case JSON_ERROR_CTRL_CHAR: |
|
259 | - $message = 'unexpected control character found'; |
|
260 | - break; |
|
261 | - case JSON_ERROR_SYNTAX: |
|
262 | - $message = 'malformed JSON'; |
|
263 | - break; |
|
264 | - case JSON_ERROR_UTF8: |
|
265 | - $message = 'malformed UTF-8 characters, possibly ' . |
|
266 | - 'incorrectly encoded'; |
|
267 | - break; |
|
268 | - default: |
|
269 | - $message = 'unknown error'; |
|
270 | - break; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - if (isset($message)) { |
|
275 | - throw new \RuntimeException('Error encoding/decoding JSON: ' . $message); |
|
276 | - } |
|
277 | - } |
|
22 | + /** |
|
23 | + * @var boolean|null shim for json_encode option JSON_PRETTY_PRINT set |
|
24 | + * it to null to use smart defaults |
|
25 | + */ |
|
26 | + public static $prettyPrint = null; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var boolean|null shim for json_encode option JSON_UNESCAPED_SLASHES |
|
30 | + * set it to null to use smart defaults |
|
31 | + */ |
|
32 | + public static $unEscapedSlashes = null; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var boolean|null shim for json_encode JSON_UNESCAPED_UNICODE set it |
|
36 | + * to null to use smart defaults |
|
37 | + */ |
|
38 | + public static $unEscapedUnicode = null; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var boolean|null shim for json_decode JSON_BIGINT_AS_STRING set it to |
|
42 | + * null to |
|
43 | + * use smart defaults |
|
44 | + */ |
|
45 | + public static $bigIntAsString = null; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var boolean|null shim for json_decode JSON_NUMERIC_CHECK set it to |
|
49 | + * null to |
|
50 | + * use smart defaults |
|
51 | + */ |
|
52 | + public static $numbersAsNumbers = null; |
|
53 | + |
|
54 | + const MIME = 'application/json'; |
|
55 | + const EXTENSION = 'json'; |
|
56 | + |
|
57 | + public function encode($data, $humanReadable = false) |
|
58 | + { |
|
59 | + if (!is_null(self::$prettyPrint)) { |
|
60 | + $humanReadable = self::$prettyPrint; |
|
61 | + } |
|
62 | + if (is_null(self::$unEscapedSlashes)) { |
|
63 | + self::$unEscapedSlashes = $humanReadable; |
|
64 | + } |
|
65 | + if (is_null(self::$unEscapedUnicode)) { |
|
66 | + self::$unEscapedUnicode = $this->charset == 'utf-8'; |
|
67 | + } |
|
68 | + |
|
69 | + $options = 0; |
|
70 | + |
|
71 | + if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
72 | + || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
73 | + ) { |
|
74 | + |
|
75 | + if ($humanReadable) { |
|
76 | + $options |= JSON_PRETTY_PRINT; |
|
77 | + } |
|
78 | + |
|
79 | + if (self::$unEscapedSlashes) { |
|
80 | + $options |= JSON_UNESCAPED_SLASHES; |
|
81 | + } |
|
82 | + |
|
83 | + if (self::$bigIntAsString) { |
|
84 | + $options |= JSON_BIGINT_AS_STRING; |
|
85 | + } |
|
86 | + |
|
87 | + if (self::$unEscapedUnicode) { |
|
88 | + $options |= JSON_UNESCAPED_UNICODE; |
|
89 | + } |
|
90 | + |
|
91 | + if (self::$numbersAsNumbers) { |
|
92 | + $options |= JSON_NUMERIC_CHECK; |
|
93 | + } |
|
94 | + |
|
95 | + $result = json_encode(Obj::toArray($data, true), $options); |
|
96 | + $this->handleJsonError(); |
|
97 | + |
|
98 | + return $result; |
|
99 | + } |
|
100 | + |
|
101 | + $result = json_encode(Obj::toArray($data, true)); |
|
102 | + $this->handleJsonError(); |
|
103 | + |
|
104 | + if ($humanReadable) { |
|
105 | + $result = $this->formatJson($result); |
|
106 | + } |
|
107 | + |
|
108 | + if (self::$unEscapedUnicode) { |
|
109 | + $result = preg_replace_callback( |
|
110 | + '/\\\u(\w\w\w\w)/', |
|
111 | + function ($matches) { |
|
112 | + if (function_exists('mb_convert_encoding')) { |
|
113 | + return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
|
114 | + } else { |
|
115 | + return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1])); |
|
116 | + } |
|
117 | + }, |
|
118 | + $result |
|
119 | + ); |
|
120 | + } |
|
121 | + |
|
122 | + if (self::$unEscapedSlashes) { |
|
123 | + $result = str_replace('\/', '/', $result); |
|
124 | + } |
|
125 | + |
|
126 | + return $result; |
|
127 | + } |
|
128 | + |
|
129 | + public function decode($data) |
|
130 | + { |
|
131 | + if (empty($data)) { |
|
132 | + return null; |
|
133 | + } |
|
134 | + |
|
135 | + $options = 0; |
|
136 | + if (self::$bigIntAsString) { |
|
137 | + if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
138 | + || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
139 | + ) { |
|
140 | + $options |= JSON_BIGINT_AS_STRING; |
|
141 | + } else { |
|
142 | + $data = preg_replace( |
|
143 | + '/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', |
|
144 | + ': "$1"', |
|
145 | + $data |
|
146 | + ); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + try { |
|
151 | + $decoded = json_decode($data, true, 512, $options); |
|
152 | + $this->handleJsonError(); |
|
153 | + } catch (\RuntimeException $e) { |
|
154 | + throw new RestException(400, $e->getMessage()); |
|
155 | + } |
|
156 | + |
|
157 | + if (strlen($data) && $decoded === null || $decoded === $data) { |
|
158 | + throw new RestException(400, 'Error parsing JSON'); |
|
159 | + } |
|
160 | + |
|
161 | + return $decoded; //Obj::toArray($decoded); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Pretty print JSON string |
|
166 | + * |
|
167 | + * @param string $json |
|
168 | + * |
|
169 | + * @return string formatted json |
|
170 | + */ |
|
171 | + private function formatJson($json) |
|
172 | + { |
|
173 | + $tab = ' '; |
|
174 | + $newJson = ''; |
|
175 | + $indentLevel = 0; |
|
176 | + $inString = false; |
|
177 | + $len = strlen($json); |
|
178 | + for ($c = 0; $c < $len; $c++) { |
|
179 | + $char = $json [$c]; |
|
180 | + switch ($char) { |
|
181 | + case '{' : |
|
182 | + case '[' : |
|
183 | + if (!$inString) { |
|
184 | + $newJson .= $char . "\n" . |
|
185 | + str_repeat($tab, $indentLevel + 1); |
|
186 | + $indentLevel++; |
|
187 | + } else { |
|
188 | + $newJson .= $char; |
|
189 | + } |
|
190 | + break; |
|
191 | + case '}' : |
|
192 | + case ']' : |
|
193 | + if (!$inString) { |
|
194 | + $indentLevel--; |
|
195 | + $newJson .= "\n" . |
|
196 | + str_repeat($tab, $indentLevel) . $char; |
|
197 | + } else { |
|
198 | + $newJson .= $char; |
|
199 | + } |
|
200 | + break; |
|
201 | + case ',' : |
|
202 | + if (!$inString) { |
|
203 | + $newJson .= ",\n" . |
|
204 | + str_repeat($tab, $indentLevel); |
|
205 | + } else { |
|
206 | + $newJson .= $char; |
|
207 | + } |
|
208 | + break; |
|
209 | + case ':' : |
|
210 | + if (!$inString) { |
|
211 | + $newJson .= ': '; |
|
212 | + } else { |
|
213 | + $newJson .= $char; |
|
214 | + } |
|
215 | + break; |
|
216 | + case '"' : |
|
217 | + if ($c == 0) { |
|
218 | + $inString = true; |
|
219 | + } elseif ($c > 0 && $json [$c - 1] != '\\') { |
|
220 | + $inString = !$inString; |
|
221 | + } |
|
222 | + default : |
|
223 | + $newJson .= $char; |
|
224 | + break; |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + return $newJson; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Throws an exception if an error occurred during the last JSON encoding/decoding |
|
233 | + * |
|
234 | + * @return void |
|
235 | + * @throws \RuntimeException |
|
236 | + */ |
|
237 | + protected function handleJsonError() |
|
238 | + { |
|
239 | + if (function_exists('json_last_error_msg') && json_last_error() !== JSON_ERROR_NONE) { |
|
240 | + |
|
241 | + // PHP >= 5.5.0 |
|
242 | + |
|
243 | + $message = json_last_error_msg(); |
|
244 | + |
|
245 | + } elseif (function_exists('json_last_error')) { |
|
246 | + |
|
247 | + // PHP >= 5.3.0 |
|
248 | + |
|
249 | + switch (json_last_error()) { |
|
250 | + case JSON_ERROR_NONE: |
|
251 | + break; |
|
252 | + case JSON_ERROR_DEPTH: |
|
253 | + $message = 'maximum stack depth exceeded'; |
|
254 | + break; |
|
255 | + case JSON_ERROR_STATE_MISMATCH: |
|
256 | + $message = 'underflow or the modes mismatch'; |
|
257 | + break; |
|
258 | + case JSON_ERROR_CTRL_CHAR: |
|
259 | + $message = 'unexpected control character found'; |
|
260 | + break; |
|
261 | + case JSON_ERROR_SYNTAX: |
|
262 | + $message = 'malformed JSON'; |
|
263 | + break; |
|
264 | + case JSON_ERROR_UTF8: |
|
265 | + $message = 'malformed UTF-8 characters, possibly ' . |
|
266 | + 'incorrectly encoded'; |
|
267 | + break; |
|
268 | + default: |
|
269 | + $message = 'unknown error'; |
|
270 | + break; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + if (isset($message)) { |
|
275 | + throw new \RuntimeException('Error encoding/decoding JSON: ' . $message); |
|
276 | + } |
|
277 | + } |
|
278 | 278 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | if (self::$unEscapedUnicode) { |
109 | 109 | $result = preg_replace_callback( |
110 | 110 | '/\\\u(\w\w\w\w)/', |
111 | - function ($matches) { |
|
111 | + function($matches) { |
|
112 | 112 | if (function_exists('mb_convert_encoding')) { |
113 | 113 | return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
114 | 114 | } else { |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | case '{' : |
182 | 182 | case '[' : |
183 | 183 | if (!$inString) { |
184 | - $newJson .= $char . "\n" . |
|
184 | + $newJson .= $char."\n". |
|
185 | 185 | str_repeat($tab, $indentLevel + 1); |
186 | 186 | $indentLevel++; |
187 | 187 | } else { |
@@ -192,15 +192,15 @@ discard block |
||
192 | 192 | case ']' : |
193 | 193 | if (!$inString) { |
194 | 194 | $indentLevel--; |
195 | - $newJson .= "\n" . |
|
196 | - str_repeat($tab, $indentLevel) . $char; |
|
195 | + $newJson .= "\n". |
|
196 | + str_repeat($tab, $indentLevel).$char; |
|
197 | 197 | } else { |
198 | 198 | $newJson .= $char; |
199 | 199 | } |
200 | 200 | break; |
201 | 201 | case ',' : |
202 | 202 | if (!$inString) { |
203 | - $newJson .= ",\n" . |
|
203 | + $newJson .= ",\n". |
|
204 | 204 | str_repeat($tab, $indentLevel); |
205 | 205 | } else { |
206 | 206 | $newJson .= $char; |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | $message = 'malformed JSON'; |
263 | 263 | break; |
264 | 264 | case JSON_ERROR_UTF8: |
265 | - $message = 'malformed UTF-8 characters, possibly ' . |
|
265 | + $message = 'malformed UTF-8 characters, possibly '. |
|
266 | 266 | 'incorrectly encoded'; |
267 | 267 | break; |
268 | 268 | default: |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | } |
273 | 273 | |
274 | 274 | if (isset($message)) { |
275 | - throw new \RuntimeException('Error encoding/decoding JSON: ' . $message); |
|
275 | + throw new \RuntimeException('Error encoding/decoding JSON: '.$message); |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 | } |
@@ -15,44 +15,44 @@ |
||
15 | 15 | */ |
16 | 16 | class UrlEncodedFormat extends Format |
17 | 17 | { |
18 | - const MIME = 'application/x-www-form-urlencoded'; |
|
19 | - const EXTENSION = 'post'; |
|
18 | + const MIME = 'application/x-www-form-urlencoded'; |
|
19 | + const EXTENSION = 'post'; |
|
20 | 20 | |
21 | - public function encode($data, $humanReadable = false) |
|
22 | - { |
|
23 | - return http_build_query(static::encoderTypeFix($data)); |
|
24 | - } |
|
21 | + public function encode($data, $humanReadable = false) |
|
22 | + { |
|
23 | + return http_build_query(static::encoderTypeFix($data)); |
|
24 | + } |
|
25 | 25 | |
26 | - public function decode($data) |
|
27 | - { |
|
28 | - parse_str($data, $r); |
|
29 | - return self::decoderTypeFix($r); |
|
30 | - } |
|
26 | + public function decode($data) |
|
27 | + { |
|
28 | + parse_str($data, $r); |
|
29 | + return self::decoderTypeFix($r); |
|
30 | + } |
|
31 | 31 | |
32 | - public static function encoderTypeFix(array $data) |
|
33 | - { |
|
34 | - foreach ($data as $k => $v) { |
|
35 | - if (is_bool($v)) { |
|
36 | - $data[$k] = $v = $v ? 'true' : 'false'; |
|
37 | - } elseif (is_array($v)) { |
|
38 | - $data[$k] = $v = static::decoderTypeFix($v); |
|
39 | - } |
|
40 | - } |
|
41 | - return $data; |
|
42 | - } |
|
32 | + public static function encoderTypeFix(array $data) |
|
33 | + { |
|
34 | + foreach ($data as $k => $v) { |
|
35 | + if (is_bool($v)) { |
|
36 | + $data[$k] = $v = $v ? 'true' : 'false'; |
|
37 | + } elseif (is_array($v)) { |
|
38 | + $data[$k] = $v = static::decoderTypeFix($v); |
|
39 | + } |
|
40 | + } |
|
41 | + return $data; |
|
42 | + } |
|
43 | 43 | |
44 | - public static function decoderTypeFix(array $data) |
|
45 | - { |
|
46 | - foreach ($data as $k => $v) { |
|
47 | - if ($v === 'true' || $v === 'false') { |
|
48 | - $data[$k] = $v = $v === 'true'; |
|
49 | - } elseif (is_array($v)) { |
|
50 | - $data[$k] = $v = static::decoderTypeFix($v); |
|
51 | - } elseif (empty($v) && $v != 0) { |
|
52 | - unset($data[$k]); |
|
53 | - } |
|
54 | - } |
|
55 | - return $data; |
|
56 | - } |
|
44 | + public static function decoderTypeFix(array $data) |
|
45 | + { |
|
46 | + foreach ($data as $k => $v) { |
|
47 | + if ($v === 'true' || $v === 'false') { |
|
48 | + $data[$k] = $v = $v === 'true'; |
|
49 | + } elseif (is_array($v)) { |
|
50 | + $data[$k] = $v = static::decoderTypeFix($v); |
|
51 | + } elseif (empty($v) && $v != 0) { |
|
52 | + unset($data[$k]); |
|
53 | + } |
|
54 | + } |
|
55 | + return $data; |
|
56 | + } |
|
57 | 57 | } |
58 | 58 |
@@ -22,83 +22,83 @@ |
||
22 | 22 | */ |
23 | 23 | class PlistFormat extends DependentMultiFormat |
24 | 24 | { |
25 | - /** |
|
26 | - * @var boolean set it to true binary plist is preferred |
|
27 | - */ |
|
28 | - public static $compact = null; |
|
29 | - const MIME = 'application/xml,application/x-plist'; |
|
30 | - const EXTENSION = 'plist'; |
|
25 | + /** |
|
26 | + * @var boolean set it to true binary plist is preferred |
|
27 | + */ |
|
28 | + public static $compact = null; |
|
29 | + const MIME = 'application/xml,application/x-plist'; |
|
30 | + const EXTENSION = 'plist'; |
|
31 | 31 | |
32 | - public function setMIME($mime) |
|
33 | - { |
|
34 | - static::$mime = $mime; |
|
35 | - static::$compact = $mime == 'application/x-plist'; |
|
36 | - } |
|
32 | + public function setMIME($mime) |
|
33 | + { |
|
34 | + static::$mime = $mime; |
|
35 | + static::$compact = $mime == 'application/x-plist'; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Encode the given data in plist format |
|
40 | - * |
|
41 | - * @param array $data |
|
42 | - * resulting data that needs to |
|
43 | - * be encoded in plist format |
|
44 | - * @param boolean $humanReadable |
|
45 | - * set to true when restler |
|
46 | - * is not running in production mode. Formatter has to |
|
47 | - * make the encoded output more human readable |
|
48 | - * |
|
49 | - * @return string encoded string |
|
50 | - */ |
|
51 | - public function encode($data, $humanReadable = false) |
|
52 | - { |
|
53 | - //require_once 'CFPropertyList.php'; |
|
54 | - if (!isset(self::$compact)) { |
|
55 | - self::$compact = !$humanReadable; |
|
56 | - } |
|
57 | - /** |
|
58 | - * |
|
59 | - * @var CFPropertyList |
|
60 | - */ |
|
61 | - $plist = new CFPropertyList (); |
|
62 | - $td = new CFTypeDetector (); |
|
63 | - $guessedStructure = $td->toCFType( |
|
64 | - Obj::toArray($data) |
|
65 | - ); |
|
66 | - $plist->add($guessedStructure); |
|
38 | + /** |
|
39 | + * Encode the given data in plist format |
|
40 | + * |
|
41 | + * @param array $data |
|
42 | + * resulting data that needs to |
|
43 | + * be encoded in plist format |
|
44 | + * @param boolean $humanReadable |
|
45 | + * set to true when restler |
|
46 | + * is not running in production mode. Formatter has to |
|
47 | + * make the encoded output more human readable |
|
48 | + * |
|
49 | + * @return string encoded string |
|
50 | + */ |
|
51 | + public function encode($data, $humanReadable = false) |
|
52 | + { |
|
53 | + //require_once 'CFPropertyList.php'; |
|
54 | + if (!isset(self::$compact)) { |
|
55 | + self::$compact = !$humanReadable; |
|
56 | + } |
|
57 | + /** |
|
58 | + * |
|
59 | + * @var CFPropertyList |
|
60 | + */ |
|
61 | + $plist = new CFPropertyList (); |
|
62 | + $td = new CFTypeDetector (); |
|
63 | + $guessedStructure = $td->toCFType( |
|
64 | + Obj::toArray($data) |
|
65 | + ); |
|
66 | + $plist->add($guessedStructure); |
|
67 | 67 | |
68 | - return self::$compact |
|
69 | - ? $plist->toBinary() |
|
70 | - : $plist->toXML(true); |
|
71 | - } |
|
68 | + return self::$compact |
|
69 | + ? $plist->toBinary() |
|
70 | + : $plist->toXML(true); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Decode the given data from plist format |
|
75 | - * |
|
76 | - * @param string $data |
|
77 | - * data sent from client to |
|
78 | - * the api in the given format. |
|
79 | - * |
|
80 | - * @return array associative array of the parsed data |
|
81 | - */ |
|
82 | - public function decode($data) |
|
83 | - { |
|
84 | - $plist = new CFPropertyList (); |
|
85 | - $plist->parse($data); |
|
73 | + /** |
|
74 | + * Decode the given data from plist format |
|
75 | + * |
|
76 | + * @param string $data |
|
77 | + * data sent from client to |
|
78 | + * the api in the given format. |
|
79 | + * |
|
80 | + * @return array associative array of the parsed data |
|
81 | + */ |
|
82 | + public function decode($data) |
|
83 | + { |
|
84 | + $plist = new CFPropertyList (); |
|
85 | + $plist->parse($data); |
|
86 | 86 | |
87 | - return $plist->toArray(); |
|
88 | - } |
|
87 | + return $plist->toArray(); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Get external class => packagist package name as an associative array |
|
92 | - * |
|
93 | - * @return array list of dependencies for the format |
|
94 | - * |
|
95 | - * @example return ['Illuminate\\View\\View' => 'illuminate/view:4.2.*'] |
|
96 | - */ |
|
97 | - public function getDependencyMap() |
|
98 | - { |
|
99 | - return array( |
|
100 | - 'CFPropertyList\CFPropertyList' => 'rodneyrehm/plist:dev-master' |
|
101 | - ); |
|
102 | - } |
|
90 | + /** |
|
91 | + * Get external class => packagist package name as an associative array |
|
92 | + * |
|
93 | + * @return array list of dependencies for the format |
|
94 | + * |
|
95 | + * @example return ['Illuminate\\View\\View' => 'illuminate/view:4.2.*'] |
|
96 | + */ |
|
97 | + public function getDependencyMap() |
|
98 | + { |
|
99 | + return array( |
|
100 | + 'CFPropertyList\CFPropertyList' => 'rodneyrehm/plist:dev-master' |
|
101 | + ); |
|
102 | + } |
|
103 | 103 | } |
104 | 104 |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @var CFPropertyList |
60 | 60 | */ |
61 | - $plist = new CFPropertyList (); |
|
62 | - $td = new CFTypeDetector (); |
|
61 | + $plist = new CFPropertyList(); |
|
62 | + $td = new CFTypeDetector(); |
|
63 | 63 | $guessedStructure = $td->toCFType( |
64 | 64 | Obj::toArray($data) |
65 | 65 | ); |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | public function decode($data) |
83 | 83 | { |
84 | - $plist = new CFPropertyList (); |
|
84 | + $plist = new CFPropertyList(); |
|
85 | 85 | $plist->parse($data); |
86 | 86 | |
87 | 87 | return $plist->toArray(); |
@@ -16,15 +16,15 @@ |
||
16 | 16 | interface iDecodeStream |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Decode the given data stream |
|
21 | - * |
|
22 | - * @param string $stream A stream resource with data |
|
23 | - * sent from client to the api |
|
24 | - * in the given format. |
|
25 | - * |
|
26 | - * @return array associative array of the parsed data |
|
27 | - */ |
|
28 | - public function decodeStream($stream); |
|
19 | + /** |
|
20 | + * Decode the given data stream |
|
21 | + * |
|
22 | + * @param string $stream A stream resource with data |
|
23 | + * sent from client to the api |
|
24 | + * in the given format. |
|
25 | + * |
|
26 | + * @return array associative array of the parsed data |
|
27 | + */ |
|
28 | + public function decodeStream($stream); |
|
29 | 29 | |
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -16,53 +16,53 @@ |
||
16 | 16 | */ |
17 | 17 | class Redirect |
18 | 18 | { |
19 | - /** |
|
20 | - * Redirect to given url |
|
21 | - * |
|
22 | - * @param string $url relative path or full url |
|
23 | - * @param array $params associative array of query parameters |
|
24 | - * @param array $flashData associative array of properties to be set in $_SESSION for one time use |
|
25 | - * @param int $status http status code to send the response with ideally 301 or 302 |
|
26 | - * |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public static function to($url, array $params = array(), array $flashData = array(), $status = 302) |
|
30 | - { |
|
31 | - $url = ltrim($url, '/'); |
|
32 | - /** @var $r Restler */ |
|
33 | - $r = Scope::get('Restler'); |
|
34 | - $base = $r->getBaseUrl() . '/'; |
|
35 | - if (0 !== strpos($url, 'http')) { |
|
36 | - $url = $base . $url; |
|
37 | - } |
|
38 | - if (!empty($flashData) || $base . $r->url !== $url || Util::getRequestMethod() != 'GET') { |
|
39 | - if ($r->responseFormat instanceof JsonFormat) { |
|
40 | - return array('redirect' => $url); |
|
41 | - } |
|
42 | - if (!empty($params)) { |
|
43 | - $url .= '?' . http_build_query($params); |
|
44 | - } |
|
45 | - Flash::set($flashData); |
|
46 | - header( |
|
47 | - "{$_SERVER['SERVER_PROTOCOL']} $status " . |
|
48 | - (isset(RestException::$codes[$status]) ? RestException::$codes[$status] : '') |
|
49 | - ); |
|
50 | - header("Location: $url"); |
|
51 | - die(''); |
|
52 | - } |
|
19 | + /** |
|
20 | + * Redirect to given url |
|
21 | + * |
|
22 | + * @param string $url relative path or full url |
|
23 | + * @param array $params associative array of query parameters |
|
24 | + * @param array $flashData associative array of properties to be set in $_SESSION for one time use |
|
25 | + * @param int $status http status code to send the response with ideally 301 or 302 |
|
26 | + * |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public static function to($url, array $params = array(), array $flashData = array(), $status = 302) |
|
30 | + { |
|
31 | + $url = ltrim($url, '/'); |
|
32 | + /** @var $r Restler */ |
|
33 | + $r = Scope::get('Restler'); |
|
34 | + $base = $r->getBaseUrl() . '/'; |
|
35 | + if (0 !== strpos($url, 'http')) { |
|
36 | + $url = $base . $url; |
|
37 | + } |
|
38 | + if (!empty($flashData) || $base . $r->url !== $url || Util::getRequestMethod() != 'GET') { |
|
39 | + if ($r->responseFormat instanceof JsonFormat) { |
|
40 | + return array('redirect' => $url); |
|
41 | + } |
|
42 | + if (!empty($params)) { |
|
43 | + $url .= '?' . http_build_query($params); |
|
44 | + } |
|
45 | + Flash::set($flashData); |
|
46 | + header( |
|
47 | + "{$_SERVER['SERVER_PROTOCOL']} $status " . |
|
48 | + (isset(RestException::$codes[$status]) ? RestException::$codes[$status] : '') |
|
49 | + ); |
|
50 | + header("Location: $url"); |
|
51 | + die(''); |
|
52 | + } |
|
53 | 53 | |
54 | - return array(); |
|
55 | - } |
|
54 | + return array(); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Redirect back to the previous page |
|
59 | - * |
|
60 | - * Makes use of http referrer for redirection |
|
61 | - * |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - public static function back() |
|
65 | - { |
|
66 | - return static::to($_SERVER['HTTP_REFERER']); |
|
67 | - } |
|
57 | + /** |
|
58 | + * Redirect back to the previous page |
|
59 | + * |
|
60 | + * Makes use of http referrer for redirection |
|
61 | + * |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + public static function back() |
|
65 | + { |
|
66 | + return static::to($_SERVER['HTTP_REFERER']); |
|
67 | + } |
|
68 | 68 | } |
69 | 69 | \ No newline at end of file |
@@ -31,20 +31,20 @@ |
||
31 | 31 | $url = ltrim($url, '/'); |
32 | 32 | /** @var $r Restler */ |
33 | 33 | $r = Scope::get('Restler'); |
34 | - $base = $r->getBaseUrl() . '/'; |
|
34 | + $base = $r->getBaseUrl().'/'; |
|
35 | 35 | if (0 !== strpos($url, 'http')) { |
36 | - $url = $base . $url; |
|
36 | + $url = $base.$url; |
|
37 | 37 | } |
38 | - if (!empty($flashData) || $base . $r->url !== $url || Util::getRequestMethod() != 'GET') { |
|
38 | + if (!empty($flashData) || $base.$r->url !== $url || Util::getRequestMethod() != 'GET') { |
|
39 | 39 | if ($r->responseFormat instanceof JsonFormat) { |
40 | 40 | return array('redirect' => $url); |
41 | 41 | } |
42 | 42 | if (!empty($params)) { |
43 | - $url .= '?' . http_build_query($params); |
|
43 | + $url .= '?'.http_build_query($params); |
|
44 | 44 | } |
45 | 45 | Flash::set($flashData); |
46 | 46 | header( |
47 | - "{$_SERVER['SERVER_PROTOCOL']} $status " . |
|
47 | + "{$_SERVER['SERVER_PROTOCOL']} $status ". |
|
48 | 48 | (isset(RestException::$codes[$status]) ? RestException::$codes[$status] : '') |
49 | 49 | ); |
50 | 50 | header("Location: $url"); |
@@ -14,116 +14,116 @@ |
||
14 | 14 | */ |
15 | 15 | class HumanReadableCache implements iCache |
16 | 16 | { |
17 | - /** |
|
18 | - * @var string path of the folder to hold cache files |
|
19 | - */ |
|
20 | - public static $cacheDir; |
|
17 | + /** |
|
18 | + * @var string path of the folder to hold cache files |
|
19 | + */ |
|
20 | + public static $cacheDir; |
|
21 | 21 | |
22 | - public function __construct() |
|
23 | - { |
|
24 | - if (is_null(self::$cacheDir)) { |
|
25 | - self::$cacheDir = Defaults::$cacheDirectory; |
|
26 | - } |
|
27 | - } |
|
22 | + public function __construct() |
|
23 | + { |
|
24 | + if (is_null(self::$cacheDir)) { |
|
25 | + self::$cacheDir = Defaults::$cacheDirectory; |
|
26 | + } |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * store data in the cache |
|
31 | - * |
|
32 | - * @param string $name |
|
33 | - * @param mixed $data |
|
34 | - * |
|
35 | - * @throws \Exception |
|
36 | - * @return boolean true if successful |
|
37 | - */ |
|
38 | - public function set($name, $data) |
|
39 | - { |
|
40 | - if (is_array($data)) { |
|
41 | - $s = '$o = array();' . PHP_EOL . PHP_EOL; |
|
42 | - $s .= '// ** THIS IS AN AUTO GENERATED FILE.' |
|
43 | - . ' DO NOT EDIT MANUALLY ** '; |
|
44 | - foreach ($data as $key => $value) { |
|
45 | - $s .= PHP_EOL . PHP_EOL . |
|
46 | - "//==================== $key ====================" |
|
47 | - . PHP_EOL . PHP_EOL; |
|
48 | - if (is_array($value)) { |
|
49 | - $s .= '$o[\'' . $key . '\'] = array();'; |
|
50 | - foreach ($value as $ke => $va) { |
|
51 | - $s .= PHP_EOL . PHP_EOL . "//==== $key $ke ====" |
|
52 | - . PHP_EOL . PHP_EOL; |
|
53 | - $s .= '$o[\'' . $key . '\'][\'' . $ke . '\'] = ' . |
|
54 | - str_replace(' ', ' ', |
|
55 | - var_export($va, true)) . ';'; |
|
56 | - } |
|
57 | - } else { |
|
58 | - $s .= '$o[\'' . $key . '\'] = ' |
|
59 | - . var_export($value, true) . ';'; |
|
60 | - } |
|
61 | - } |
|
62 | - $s .= PHP_EOL . 'return $o;'; |
|
63 | - } else { |
|
64 | - $s = 'return ' . var_export($data, true) . ';'; |
|
65 | - } |
|
66 | - $file = $this->_file($name); |
|
67 | - $r = @file_put_contents($file, "<?php $s"); |
|
68 | - @chmod($file, 0777); |
|
69 | - if ($r === false) { |
|
70 | - $this->throwException(); |
|
71 | - } |
|
72 | - return $r; |
|
73 | - } |
|
29 | + /** |
|
30 | + * store data in the cache |
|
31 | + * |
|
32 | + * @param string $name |
|
33 | + * @param mixed $data |
|
34 | + * |
|
35 | + * @throws \Exception |
|
36 | + * @return boolean true if successful |
|
37 | + */ |
|
38 | + public function set($name, $data) |
|
39 | + { |
|
40 | + if (is_array($data)) { |
|
41 | + $s = '$o = array();' . PHP_EOL . PHP_EOL; |
|
42 | + $s .= '// ** THIS IS AN AUTO GENERATED FILE.' |
|
43 | + . ' DO NOT EDIT MANUALLY ** '; |
|
44 | + foreach ($data as $key => $value) { |
|
45 | + $s .= PHP_EOL . PHP_EOL . |
|
46 | + "//==================== $key ====================" |
|
47 | + . PHP_EOL . PHP_EOL; |
|
48 | + if (is_array($value)) { |
|
49 | + $s .= '$o[\'' . $key . '\'] = array();'; |
|
50 | + foreach ($value as $ke => $va) { |
|
51 | + $s .= PHP_EOL . PHP_EOL . "//==== $key $ke ====" |
|
52 | + . PHP_EOL . PHP_EOL; |
|
53 | + $s .= '$o[\'' . $key . '\'][\'' . $ke . '\'] = ' . |
|
54 | + str_replace(' ', ' ', |
|
55 | + var_export($va, true)) . ';'; |
|
56 | + } |
|
57 | + } else { |
|
58 | + $s .= '$o[\'' . $key . '\'] = ' |
|
59 | + . var_export($value, true) . ';'; |
|
60 | + } |
|
61 | + } |
|
62 | + $s .= PHP_EOL . 'return $o;'; |
|
63 | + } else { |
|
64 | + $s = 'return ' . var_export($data, true) . ';'; |
|
65 | + } |
|
66 | + $file = $this->_file($name); |
|
67 | + $r = @file_put_contents($file, "<?php $s"); |
|
68 | + @chmod($file, 0777); |
|
69 | + if ($r === false) { |
|
70 | + $this->throwException(); |
|
71 | + } |
|
72 | + return $r; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * retrieve data from the cache |
|
77 | - * |
|
78 | - * @param string $name |
|
79 | - * @param bool $ignoreErrors |
|
80 | - * |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public function get($name, $ignoreErrors = false) |
|
84 | - { |
|
85 | - $file = $this->_file($name); |
|
86 | - if (file_exists($file)) { |
|
87 | - return include($file); |
|
88 | - } |
|
89 | - } |
|
75 | + /** |
|
76 | + * retrieve data from the cache |
|
77 | + * |
|
78 | + * @param string $name |
|
79 | + * @param bool $ignoreErrors |
|
80 | + * |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public function get($name, $ignoreErrors = false) |
|
84 | + { |
|
85 | + $file = $this->_file($name); |
|
86 | + if (file_exists($file)) { |
|
87 | + return include($file); |
|
88 | + } |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * delete data from the cache |
|
93 | - * |
|
94 | - * @param string $name |
|
95 | - * @param bool $ignoreErrors |
|
96 | - * |
|
97 | - * @return boolean true if successful |
|
98 | - */ |
|
99 | - public function clear($name, $ignoreErrors = false) |
|
100 | - { |
|
101 | - return @unlink($this->_file($name)); |
|
102 | - } |
|
91 | + /** |
|
92 | + * delete data from the cache |
|
93 | + * |
|
94 | + * @param string $name |
|
95 | + * @param bool $ignoreErrors |
|
96 | + * |
|
97 | + * @return boolean true if successful |
|
98 | + */ |
|
99 | + public function clear($name, $ignoreErrors = false) |
|
100 | + { |
|
101 | + return @unlink($this->_file($name)); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * check if the given name is cached |
|
106 | - * |
|
107 | - * @param string $name |
|
108 | - * |
|
109 | - * @return boolean true if cached |
|
110 | - */ |
|
111 | - public function isCached($name) |
|
112 | - { |
|
113 | - return file_exists($this->_file($name)); |
|
114 | - } |
|
104 | + /** |
|
105 | + * check if the given name is cached |
|
106 | + * |
|
107 | + * @param string $name |
|
108 | + * |
|
109 | + * @return boolean true if cached |
|
110 | + */ |
|
111 | + public function isCached($name) |
|
112 | + { |
|
113 | + return file_exists($this->_file($name)); |
|
114 | + } |
|
115 | 115 | |
116 | - private function _file($name) |
|
117 | - { |
|
118 | - return self::$cacheDir . '/' . $name . '.php'; |
|
119 | - } |
|
116 | + private function _file($name) |
|
117 | + { |
|
118 | + return self::$cacheDir . '/' . $name . '.php'; |
|
119 | + } |
|
120 | 120 | |
121 | - private function throwException() |
|
122 | - { |
|
123 | - throw new \Exception( |
|
124 | - 'The cache directory `' |
|
125 | - . self::$cacheDir . '` should exist with write permission.' |
|
126 | - ); |
|
127 | - } |
|
121 | + private function throwException() |
|
122 | + { |
|
123 | + throw new \Exception( |
|
124 | + 'The cache directory `' |
|
125 | + . self::$cacheDir . '` should exist with write permission.' |
|
126 | + ); |
|
127 | + } |
|
128 | 128 | } |
129 | 129 |
@@ -38,30 +38,30 @@ discard block |
||
38 | 38 | public function set($name, $data) |
39 | 39 | { |
40 | 40 | if (is_array($data)) { |
41 | - $s = '$o = array();' . PHP_EOL . PHP_EOL; |
|
41 | + $s = '$o = array();'.PHP_EOL.PHP_EOL; |
|
42 | 42 | $s .= '// ** THIS IS AN AUTO GENERATED FILE.' |
43 | 43 | . ' DO NOT EDIT MANUALLY ** '; |
44 | 44 | foreach ($data as $key => $value) { |
45 | - $s .= PHP_EOL . PHP_EOL . |
|
45 | + $s .= PHP_EOL.PHP_EOL. |
|
46 | 46 | "//==================== $key ====================" |
47 | - . PHP_EOL . PHP_EOL; |
|
47 | + . PHP_EOL.PHP_EOL; |
|
48 | 48 | if (is_array($value)) { |
49 | - $s .= '$o[\'' . $key . '\'] = array();'; |
|
49 | + $s .= '$o[\''.$key.'\'] = array();'; |
|
50 | 50 | foreach ($value as $ke => $va) { |
51 | - $s .= PHP_EOL . PHP_EOL . "//==== $key $ke ====" |
|
52 | - . PHP_EOL . PHP_EOL; |
|
53 | - $s .= '$o[\'' . $key . '\'][\'' . $ke . '\'] = ' . |
|
51 | + $s .= PHP_EOL.PHP_EOL."//==== $key $ke ====" |
|
52 | + . PHP_EOL.PHP_EOL; |
|
53 | + $s .= '$o[\''.$key.'\'][\''.$ke.'\'] = '. |
|
54 | 54 | str_replace(' ', ' ', |
55 | - var_export($va, true)) . ';'; |
|
55 | + var_export($va, true)).';'; |
|
56 | 56 | } |
57 | 57 | } else { |
58 | - $s .= '$o[\'' . $key . '\'] = ' |
|
59 | - . var_export($value, true) . ';'; |
|
58 | + $s .= '$o[\''.$key.'\'] = ' |
|
59 | + . var_export($value, true).';'; |
|
60 | 60 | } |
61 | 61 | } |
62 | - $s .= PHP_EOL . 'return $o;'; |
|
62 | + $s .= PHP_EOL.'return $o;'; |
|
63 | 63 | } else { |
64 | - $s = 'return ' . var_export($data, true) . ';'; |
|
64 | + $s = 'return '.var_export($data, true).';'; |
|
65 | 65 | } |
66 | 66 | $file = $this->_file($name); |
67 | 67 | $r = @file_put_contents($file, "<?php $s"); |
@@ -115,14 +115,14 @@ discard block |
||
115 | 115 | |
116 | 116 | private function _file($name) |
117 | 117 | { |
118 | - return self::$cacheDir . '/' . $name . '.php'; |
|
118 | + return self::$cacheDir.'/'.$name.'.php'; |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | private function throwException() |
122 | 122 | { |
123 | 123 | throw new \Exception( |
124 | 124 | 'The cache directory `' |
125 | - . self::$cacheDir . '` should exist with write permission.' |
|
125 | + . self::$cacheDir.'` should exist with write permission.' |
|
126 | 126 | ); |
127 | 127 | } |
128 | 128 | } |
@@ -21,45 +21,45 @@ discard block |
||
21 | 21 | */ |
22 | 22 | class Routes |
23 | 23 | { |
24 | - public static $prefixingParameterNames = array( |
|
25 | - 'id' |
|
26 | - ); |
|
24 | + public static $prefixingParameterNames = array( |
|
25 | + 'id' |
|
26 | + ); |
|
27 | 27 | |
28 | - public static $fieldTypesByName = array( |
|
29 | - 'email' => 'email', |
|
30 | - 'password' => 'password', |
|
31 | - 'phone' => 'tel', |
|
32 | - 'mobile' => 'tel', |
|
33 | - 'tel' => 'tel', |
|
34 | - 'search' => 'search', |
|
35 | - 'date' => 'date', |
|
36 | - 'created_at' => 'datetime', |
|
37 | - 'modified_at' => 'datetime', |
|
38 | - 'url' => 'url', |
|
39 | - 'link' => 'url', |
|
40 | - 'href' => 'url', |
|
41 | - 'website' => 'url', |
|
42 | - 'color' => 'color', |
|
43 | - 'colour' => 'color', |
|
44 | - ); |
|
28 | + public static $fieldTypesByName = array( |
|
29 | + 'email' => 'email', |
|
30 | + 'password' => 'password', |
|
31 | + 'phone' => 'tel', |
|
32 | + 'mobile' => 'tel', |
|
33 | + 'tel' => 'tel', |
|
34 | + 'search' => 'search', |
|
35 | + 'date' => 'date', |
|
36 | + 'created_at' => 'datetime', |
|
37 | + 'modified_at' => 'datetime', |
|
38 | + 'url' => 'url', |
|
39 | + 'link' => 'url', |
|
40 | + 'href' => 'url', |
|
41 | + 'website' => 'url', |
|
42 | + 'color' => 'color', |
|
43 | + 'colour' => 'color', |
|
44 | + ); |
|
45 | 45 | |
46 | - protected static $routes = array(); |
|
46 | + protected static $routes = array(); |
|
47 | 47 | |
48 | - protected static $models = array(); |
|
48 | + protected static $models = array(); |
|
49 | 49 | |
50 | - /** |
|
51 | - * Route the public and protected methods of an Api class |
|
52 | - * |
|
53 | - * @param string $className |
|
54 | - * @param string $resourcePath |
|
55 | - * @param int $version |
|
56 | - * |
|
57 | - * @throws RestException |
|
58 | - */ |
|
59 | - public static function addAPIClass($className, $resourcePath = '', $version = 1) |
|
60 | - { |
|
50 | + /** |
|
51 | + * Route the public and protected methods of an Api class |
|
52 | + * |
|
53 | + * @param string $className |
|
54 | + * @param string $resourcePath |
|
55 | + * @param int $version |
|
56 | + * |
|
57 | + * @throws RestException |
|
58 | + */ |
|
59 | + public static function addAPIClass($className, $resourcePath = '', $version = 1) |
|
60 | + { |
|
61 | 61 | |
62 | - /* |
|
62 | + /* |
|
63 | 63 | * Mapping Rules |
64 | 64 | * ============= |
65 | 65 | * |
@@ -74,749 +74,749 @@ discard block |
||
74 | 74 | * - If a required parameter is not primitive type |
75 | 75 | * - Do not include it in URL |
76 | 76 | */ |
77 | - $class = new ReflectionClass($className); |
|
78 | - $dataName = CommentParser::$embeddedDataName; |
|
79 | - try { |
|
80 | - $classMetadata = CommentParser::parse($class->getDocComment()); |
|
81 | - } catch (Exception $e) { |
|
82 | - throw new RestException(500, "Error while parsing comments of `$className` class. " . $e->getMessage()); |
|
83 | - } |
|
84 | - $classMetadata['scope'] = $scope = static::scope($class); |
|
85 | - $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC + |
|
86 | - ReflectionMethod::IS_PROTECTED); |
|
87 | - foreach ($methods as $method) { |
|
88 | - $methodUrl = strtolower($method->getName()); |
|
89 | - //method name should not begin with _ |
|
90 | - if ($methodUrl[0] == '_') { |
|
91 | - continue; |
|
92 | - } |
|
93 | - $doc = $method->getDocComment(); |
|
77 | + $class = new ReflectionClass($className); |
|
78 | + $dataName = CommentParser::$embeddedDataName; |
|
79 | + try { |
|
80 | + $classMetadata = CommentParser::parse($class->getDocComment()); |
|
81 | + } catch (Exception $e) { |
|
82 | + throw new RestException(500, "Error while parsing comments of `$className` class. " . $e->getMessage()); |
|
83 | + } |
|
84 | + $classMetadata['scope'] = $scope = static::scope($class); |
|
85 | + $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC + |
|
86 | + ReflectionMethod::IS_PROTECTED); |
|
87 | + foreach ($methods as $method) { |
|
88 | + $methodUrl = strtolower($method->getName()); |
|
89 | + //method name should not begin with _ |
|
90 | + if ($methodUrl[0] == '_') { |
|
91 | + continue; |
|
92 | + } |
|
93 | + $doc = $method->getDocComment(); |
|
94 | 94 | |
95 | - try { |
|
96 | - $metadata = CommentParser::parse($doc) + $classMetadata; |
|
97 | - } catch (Exception $e) { |
|
98 | - throw new RestException(500, "Error while parsing comments of `{$className}::{$method->getName()}` method. " . $e->getMessage()); |
|
99 | - } |
|
100 | - //@access should not be private |
|
101 | - if (isset($metadata['access']) |
|
102 | - && $metadata['access'] == 'private' |
|
103 | - ) { |
|
104 | - continue; |
|
105 | - } |
|
106 | - $arguments = array(); |
|
107 | - $defaults = array(); |
|
108 | - $params = $method->getParameters(); |
|
109 | - $position = 0; |
|
110 | - $pathParams = array(); |
|
111 | - $allowAmbiguity |
|
112 | - = (isset($metadata['smart-auto-routing']) |
|
113 | - && $metadata['smart-auto-routing'] != 'true') |
|
114 | - || !Defaults::$smartAutoRouting; |
|
115 | - $metadata['resourcePath'] = trim($resourcePath, '/'); |
|
116 | - if (isset($classMetadata['description'])) { |
|
117 | - $metadata['classDescription'] = $classMetadata['description']; |
|
118 | - } |
|
119 | - if (isset($classMetadata['classLongDescription'])) { |
|
120 | - $metadata['classLongDescription'] |
|
121 | - = $classMetadata['longDescription']; |
|
122 | - } |
|
123 | - if (!isset($metadata['param'])) { |
|
124 | - $metadata['param'] = array(); |
|
125 | - } |
|
126 | - if (isset($metadata['return']['type'])) { |
|
127 | - if ($qualified = Scope::resolve($metadata['return']['type'], $scope)) |
|
128 | - list($metadata['return']['type'], $metadata['return']['children']) = |
|
129 | - static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
|
130 | - } else { |
|
131 | - //assume return type is array |
|
132 | - $metadata['return']['type'] = 'array'; |
|
133 | - } |
|
134 | - foreach ($params as $param) { |
|
135 | - $children = array(); |
|
136 | - $type = |
|
137 | - $param->isArray() ? 'array' : $param->getClass(); |
|
138 | - $arguments[$param->getName()] = $position; |
|
139 | - $defaults[$position] = $param->isDefaultValueAvailable() ? |
|
140 | - $param->getDefaultValue() : null; |
|
141 | - if (!isset($metadata['param'][$position])) { |
|
142 | - $metadata['param'][$position] = array(); |
|
143 | - } |
|
144 | - $m = & $metadata ['param'] [$position]; |
|
145 | - $m ['name'] = $param->getName(); |
|
146 | - if (!isset($m[$dataName])) { |
|
147 | - $m[$dataName] = array(); |
|
148 | - } |
|
149 | - $p = &$m[$dataName]; |
|
150 | - if (empty($m['label'])) |
|
151 | - $m['label'] = Text::title($m['name']); |
|
152 | - if (is_null($type) && isset($m['type'])) { |
|
153 | - $type = $m['type']; |
|
154 | - } |
|
155 | - if (isset(static::$fieldTypesByName[$m['name']]) && empty($p['type']) && $type == 'string') { |
|
156 | - $p['type'] = static::$fieldTypesByName[$m['name']]; |
|
157 | - } |
|
158 | - $m ['default'] = $defaults [$position]; |
|
159 | - $m ['required'] = !$param->isOptional(); |
|
160 | - $contentType = Util::nestedValue($p,'type'); |
|
161 | - if ($type == 'array' && $contentType && $qualified = Scope::resolve($contentType, $scope)) { |
|
162 | - list($p['type'], $children, $modelName) = static::getTypeAndModel( |
|
163 | - new ReflectionClass($qualified), $scope, |
|
164 | - $className . Text::title($methodUrl), $p |
|
165 | - ); |
|
166 | - } |
|
167 | - if ($type instanceof ReflectionClass) { |
|
168 | - list($type, $children, $modelName) = static::getTypeAndModel($type, $scope, |
|
169 | - $className . Text::title($methodUrl), $p); |
|
170 | - } elseif ($type && is_string($type) && $qualified = Scope::resolve($type, $scope)) { |
|
171 | - list($type, $children, $modelName) |
|
172 | - = static::getTypeAndModel(new ReflectionClass($qualified), $scope, |
|
173 | - $className . Text::title($methodUrl), $p); |
|
174 | - } |
|
175 | - if (isset($type)) { |
|
176 | - $m['type'] = $type; |
|
177 | - } |
|
95 | + try { |
|
96 | + $metadata = CommentParser::parse($doc) + $classMetadata; |
|
97 | + } catch (Exception $e) { |
|
98 | + throw new RestException(500, "Error while parsing comments of `{$className}::{$method->getName()}` method. " . $e->getMessage()); |
|
99 | + } |
|
100 | + //@access should not be private |
|
101 | + if (isset($metadata['access']) |
|
102 | + && $metadata['access'] == 'private' |
|
103 | + ) { |
|
104 | + continue; |
|
105 | + } |
|
106 | + $arguments = array(); |
|
107 | + $defaults = array(); |
|
108 | + $params = $method->getParameters(); |
|
109 | + $position = 0; |
|
110 | + $pathParams = array(); |
|
111 | + $allowAmbiguity |
|
112 | + = (isset($metadata['smart-auto-routing']) |
|
113 | + && $metadata['smart-auto-routing'] != 'true') |
|
114 | + || !Defaults::$smartAutoRouting; |
|
115 | + $metadata['resourcePath'] = trim($resourcePath, '/'); |
|
116 | + if (isset($classMetadata['description'])) { |
|
117 | + $metadata['classDescription'] = $classMetadata['description']; |
|
118 | + } |
|
119 | + if (isset($classMetadata['classLongDescription'])) { |
|
120 | + $metadata['classLongDescription'] |
|
121 | + = $classMetadata['longDescription']; |
|
122 | + } |
|
123 | + if (!isset($metadata['param'])) { |
|
124 | + $metadata['param'] = array(); |
|
125 | + } |
|
126 | + if (isset($metadata['return']['type'])) { |
|
127 | + if ($qualified = Scope::resolve($metadata['return']['type'], $scope)) |
|
128 | + list($metadata['return']['type'], $metadata['return']['children']) = |
|
129 | + static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
|
130 | + } else { |
|
131 | + //assume return type is array |
|
132 | + $metadata['return']['type'] = 'array'; |
|
133 | + } |
|
134 | + foreach ($params as $param) { |
|
135 | + $children = array(); |
|
136 | + $type = |
|
137 | + $param->isArray() ? 'array' : $param->getClass(); |
|
138 | + $arguments[$param->getName()] = $position; |
|
139 | + $defaults[$position] = $param->isDefaultValueAvailable() ? |
|
140 | + $param->getDefaultValue() : null; |
|
141 | + if (!isset($metadata['param'][$position])) { |
|
142 | + $metadata['param'][$position] = array(); |
|
143 | + } |
|
144 | + $m = & $metadata ['param'] [$position]; |
|
145 | + $m ['name'] = $param->getName(); |
|
146 | + if (!isset($m[$dataName])) { |
|
147 | + $m[$dataName] = array(); |
|
148 | + } |
|
149 | + $p = &$m[$dataName]; |
|
150 | + if (empty($m['label'])) |
|
151 | + $m['label'] = Text::title($m['name']); |
|
152 | + if (is_null($type) && isset($m['type'])) { |
|
153 | + $type = $m['type']; |
|
154 | + } |
|
155 | + if (isset(static::$fieldTypesByName[$m['name']]) && empty($p['type']) && $type == 'string') { |
|
156 | + $p['type'] = static::$fieldTypesByName[$m['name']]; |
|
157 | + } |
|
158 | + $m ['default'] = $defaults [$position]; |
|
159 | + $m ['required'] = !$param->isOptional(); |
|
160 | + $contentType = Util::nestedValue($p,'type'); |
|
161 | + if ($type == 'array' && $contentType && $qualified = Scope::resolve($contentType, $scope)) { |
|
162 | + list($p['type'], $children, $modelName) = static::getTypeAndModel( |
|
163 | + new ReflectionClass($qualified), $scope, |
|
164 | + $className . Text::title($methodUrl), $p |
|
165 | + ); |
|
166 | + } |
|
167 | + if ($type instanceof ReflectionClass) { |
|
168 | + list($type, $children, $modelName) = static::getTypeAndModel($type, $scope, |
|
169 | + $className . Text::title($methodUrl), $p); |
|
170 | + } elseif ($type && is_string($type) && $qualified = Scope::resolve($type, $scope)) { |
|
171 | + list($type, $children, $modelName) |
|
172 | + = static::getTypeAndModel(new ReflectionClass($qualified), $scope, |
|
173 | + $className . Text::title($methodUrl), $p); |
|
174 | + } |
|
175 | + if (isset($type)) { |
|
176 | + $m['type'] = $type; |
|
177 | + } |
|
178 | 178 | |
179 | - $m['children'] = $children; |
|
180 | - if (isset($modelName)) { |
|
181 | - $m['model'] = $modelName; |
|
182 | - } |
|
183 | - if ($m['name'] == Defaults::$fullRequestDataName) { |
|
184 | - $from = 'body'; |
|
185 | - if (!isset($m['type'])) { |
|
186 | - $type = $m['type'] = 'array'; |
|
187 | - } |
|
179 | + $m['children'] = $children; |
|
180 | + if (isset($modelName)) { |
|
181 | + $m['model'] = $modelName; |
|
182 | + } |
|
183 | + if ($m['name'] == Defaults::$fullRequestDataName) { |
|
184 | + $from = 'body'; |
|
185 | + if (!isset($m['type'])) { |
|
186 | + $type = $m['type'] = 'array'; |
|
187 | + } |
|
188 | 188 | |
189 | - } elseif (isset($p['from'])) { |
|
190 | - $from = $p['from']; |
|
191 | - } else { |
|
192 | - if ((isset($type) && Util::isObjectOrArray($type)) |
|
193 | - ) { |
|
194 | - $from = 'body'; |
|
195 | - if (!isset($type)) { |
|
196 | - $type = $m['type'] = 'array'; |
|
197 | - } |
|
198 | - } elseif ($m['required'] && in_array($m['name'], static::$prefixingParameterNames)) { |
|
199 | - $from = 'path'; |
|
200 | - } else { |
|
201 | - $from = 'body'; |
|
202 | - } |
|
203 | - } |
|
204 | - $p['from'] = $from; |
|
205 | - if (!isset($m['type'])) { |
|
206 | - $type = $m['type'] = static::type($defaults[$position]); |
|
207 | - } |
|
189 | + } elseif (isset($p['from'])) { |
|
190 | + $from = $p['from']; |
|
191 | + } else { |
|
192 | + if ((isset($type) && Util::isObjectOrArray($type)) |
|
193 | + ) { |
|
194 | + $from = 'body'; |
|
195 | + if (!isset($type)) { |
|
196 | + $type = $m['type'] = 'array'; |
|
197 | + } |
|
198 | + } elseif ($m['required'] && in_array($m['name'], static::$prefixingParameterNames)) { |
|
199 | + $from = 'path'; |
|
200 | + } else { |
|
201 | + $from = 'body'; |
|
202 | + } |
|
203 | + } |
|
204 | + $p['from'] = $from; |
|
205 | + if (!isset($m['type'])) { |
|
206 | + $type = $m['type'] = static::type($defaults[$position]); |
|
207 | + } |
|
208 | 208 | |
209 | - if ($allowAmbiguity || $from == 'path') { |
|
210 | - $pathParams [] = $position; |
|
211 | - } |
|
212 | - $position++; |
|
213 | - } |
|
214 | - $accessLevel = 0; |
|
215 | - if ($method->isProtected()) { |
|
216 | - $accessLevel = 3; |
|
217 | - } elseif (isset($metadata['access'])) { |
|
218 | - if ($metadata['access'] == 'protected') { |
|
219 | - $accessLevel = 2; |
|
220 | - } elseif ($metadata['access'] == 'hybrid') { |
|
221 | - $accessLevel = 1; |
|
222 | - } |
|
223 | - } elseif (isset($metadata['protected'])) { |
|
224 | - $accessLevel = 2; |
|
225 | - } |
|
226 | - /* |
|
209 | + if ($allowAmbiguity || $from == 'path') { |
|
210 | + $pathParams [] = $position; |
|
211 | + } |
|
212 | + $position++; |
|
213 | + } |
|
214 | + $accessLevel = 0; |
|
215 | + if ($method->isProtected()) { |
|
216 | + $accessLevel = 3; |
|
217 | + } elseif (isset($metadata['access'])) { |
|
218 | + if ($metadata['access'] == 'protected') { |
|
219 | + $accessLevel = 2; |
|
220 | + } elseif ($metadata['access'] == 'hybrid') { |
|
221 | + $accessLevel = 1; |
|
222 | + } |
|
223 | + } elseif (isset($metadata['protected'])) { |
|
224 | + $accessLevel = 2; |
|
225 | + } |
|
226 | + /* |
|
227 | 227 | echo " access level $accessLevel for $className::" |
228 | 228 | .$method->getName().$method->isProtected().PHP_EOL; |
229 | 229 | */ |
230 | 230 | |
231 | - // take note of the order |
|
232 | - $call = array( |
|
233 | - 'url' => null, |
|
234 | - 'className' => $className, |
|
235 | - 'path' => rtrim($resourcePath, '/'), |
|
236 | - 'methodName' => $method->getName(), |
|
237 | - 'arguments' => $arguments, |
|
238 | - 'defaults' => $defaults, |
|
239 | - 'metadata' => $metadata, |
|
240 | - 'accessLevel' => $accessLevel, |
|
241 | - ); |
|
242 | - // if manual route |
|
243 | - if (preg_match_all( |
|
244 | - '/@url\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)' |
|
245 | - . '[ \t]*\/?(\S*)/s', |
|
246 | - $doc, $matches, PREG_SET_ORDER |
|
247 | - ) |
|
248 | - ) { |
|
249 | - foreach ($matches as $match) { |
|
250 | - $httpMethod = $match[1]; |
|
251 | - $url = rtrim($resourcePath . $match[2], '/'); |
|
252 | - //deep copy the call, as it may change for each @url |
|
253 | - $copy = unserialize(serialize($call)); |
|
254 | - foreach ($copy['metadata']['param'] as $i => $p) { |
|
255 | - $inPath = |
|
256 | - strpos($url, '{' . $p['name'] . '}') || |
|
257 | - strpos($url, ':' . $p['name']); |
|
258 | - if ($inPath) { |
|
259 | - $copy['metadata']['param'][$i][$dataName]['from'] = 'path'; |
|
260 | - } elseif ($httpMethod == 'GET' || $httpMethod == 'DELETE') { |
|
261 | - $copy['metadata']['param'][$i][$dataName]['from'] = 'query'; |
|
262 | - } elseif (empty($p[$dataName]['from']) || $p[$dataName]['from'] == 'path') { |
|
263 | - $copy['metadata']['param'][$i][$dataName]['from'] = 'body'; |
|
264 | - } |
|
265 | - } |
|
266 | - $url = preg_replace_callback('/{[^}]+}|:[^\/]+/', |
|
267 | - function ($matches) use ($copy) { |
|
268 | - $match = trim($matches[0], '{}:'); |
|
269 | - $index = $copy['arguments'][$match]; |
|
270 | - return '{' . |
|
271 | - Routes::typeChar(isset( |
|
272 | - $copy['metadata']['param'][$index]['type']) |
|
273 | - ? $copy['metadata']['param'][$index]['type'] |
|
274 | - : null) |
|
275 | - . $index . '}'; |
|
276 | - }, $url); |
|
277 | - static::addPath($url, $copy, $httpMethod, $version); |
|
278 | - } |
|
279 | - //if auto route enabled, do so |
|
280 | - } elseif (Defaults::$autoRoutingEnabled) { |
|
281 | - // no configuration found so use convention |
|
282 | - if (preg_match_all( |
|
283 | - '/^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)/i', |
|
284 | - $methodUrl, $matches) |
|
285 | - ) { |
|
286 | - $httpMethod = strtoupper($matches[0][0]); |
|
287 | - $methodUrl = substr($methodUrl, strlen($httpMethod)); |
|
288 | - } else { |
|
289 | - $httpMethod = 'GET'; |
|
290 | - } |
|
291 | - if ($methodUrl == 'index') { |
|
292 | - $methodUrl = ''; |
|
293 | - } |
|
294 | - $url = empty($methodUrl) ? rtrim($resourcePath, '/') |
|
295 | - : $resourcePath . $methodUrl; |
|
296 | - for ($position = 0; $position < count($params); $position++) { |
|
297 | - $from = $metadata['param'][$position][$dataName]['from']; |
|
298 | - if ($from == 'body' && ($httpMethod == 'GET' || |
|
299 | - $httpMethod == 'DELETE') |
|
300 | - ) { |
|
301 | - $call['metadata']['param'][$position][$dataName]['from'] |
|
302 | - = 'query'; |
|
303 | - } |
|
304 | - } |
|
305 | - if (empty($pathParams) || $allowAmbiguity) { |
|
306 | - static::addPath($url, $call, $httpMethod, $version); |
|
307 | - } |
|
308 | - $lastPathParam = end($pathParams); |
|
309 | - foreach ($pathParams as $position) { |
|
310 | - if (!empty($url)) |
|
311 | - $url .= '/'; |
|
312 | - $url .= '{' . |
|
313 | - static::typeChar(isset($call['metadata']['param'][$position]['type']) |
|
314 | - ? $call['metadata']['param'][$position]['type'] |
|
315 | - : null) |
|
316 | - . $position . '}'; |
|
317 | - if ($allowAmbiguity || $position == $lastPathParam) { |
|
318 | - static::addPath($url, $call, $httpMethod, $version); |
|
319 | - } |
|
320 | - } |
|
321 | - } |
|
322 | - } |
|
323 | - } |
|
231 | + // take note of the order |
|
232 | + $call = array( |
|
233 | + 'url' => null, |
|
234 | + 'className' => $className, |
|
235 | + 'path' => rtrim($resourcePath, '/'), |
|
236 | + 'methodName' => $method->getName(), |
|
237 | + 'arguments' => $arguments, |
|
238 | + 'defaults' => $defaults, |
|
239 | + 'metadata' => $metadata, |
|
240 | + 'accessLevel' => $accessLevel, |
|
241 | + ); |
|
242 | + // if manual route |
|
243 | + if (preg_match_all( |
|
244 | + '/@url\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)' |
|
245 | + . '[ \t]*\/?(\S*)/s', |
|
246 | + $doc, $matches, PREG_SET_ORDER |
|
247 | + ) |
|
248 | + ) { |
|
249 | + foreach ($matches as $match) { |
|
250 | + $httpMethod = $match[1]; |
|
251 | + $url = rtrim($resourcePath . $match[2], '/'); |
|
252 | + //deep copy the call, as it may change for each @url |
|
253 | + $copy = unserialize(serialize($call)); |
|
254 | + foreach ($copy['metadata']['param'] as $i => $p) { |
|
255 | + $inPath = |
|
256 | + strpos($url, '{' . $p['name'] . '}') || |
|
257 | + strpos($url, ':' . $p['name']); |
|
258 | + if ($inPath) { |
|
259 | + $copy['metadata']['param'][$i][$dataName]['from'] = 'path'; |
|
260 | + } elseif ($httpMethod == 'GET' || $httpMethod == 'DELETE') { |
|
261 | + $copy['metadata']['param'][$i][$dataName]['from'] = 'query'; |
|
262 | + } elseif (empty($p[$dataName]['from']) || $p[$dataName]['from'] == 'path') { |
|
263 | + $copy['metadata']['param'][$i][$dataName]['from'] = 'body'; |
|
264 | + } |
|
265 | + } |
|
266 | + $url = preg_replace_callback('/{[^}]+}|:[^\/]+/', |
|
267 | + function ($matches) use ($copy) { |
|
268 | + $match = trim($matches[0], '{}:'); |
|
269 | + $index = $copy['arguments'][$match]; |
|
270 | + return '{' . |
|
271 | + Routes::typeChar(isset( |
|
272 | + $copy['metadata']['param'][$index]['type']) |
|
273 | + ? $copy['metadata']['param'][$index]['type'] |
|
274 | + : null) |
|
275 | + . $index . '}'; |
|
276 | + }, $url); |
|
277 | + static::addPath($url, $copy, $httpMethod, $version); |
|
278 | + } |
|
279 | + //if auto route enabled, do so |
|
280 | + } elseif (Defaults::$autoRoutingEnabled) { |
|
281 | + // no configuration found so use convention |
|
282 | + if (preg_match_all( |
|
283 | + '/^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)/i', |
|
284 | + $methodUrl, $matches) |
|
285 | + ) { |
|
286 | + $httpMethod = strtoupper($matches[0][0]); |
|
287 | + $methodUrl = substr($methodUrl, strlen($httpMethod)); |
|
288 | + } else { |
|
289 | + $httpMethod = 'GET'; |
|
290 | + } |
|
291 | + if ($methodUrl == 'index') { |
|
292 | + $methodUrl = ''; |
|
293 | + } |
|
294 | + $url = empty($methodUrl) ? rtrim($resourcePath, '/') |
|
295 | + : $resourcePath . $methodUrl; |
|
296 | + for ($position = 0; $position < count($params); $position++) { |
|
297 | + $from = $metadata['param'][$position][$dataName]['from']; |
|
298 | + if ($from == 'body' && ($httpMethod == 'GET' || |
|
299 | + $httpMethod == 'DELETE') |
|
300 | + ) { |
|
301 | + $call['metadata']['param'][$position][$dataName]['from'] |
|
302 | + = 'query'; |
|
303 | + } |
|
304 | + } |
|
305 | + if (empty($pathParams) || $allowAmbiguity) { |
|
306 | + static::addPath($url, $call, $httpMethod, $version); |
|
307 | + } |
|
308 | + $lastPathParam = end($pathParams); |
|
309 | + foreach ($pathParams as $position) { |
|
310 | + if (!empty($url)) |
|
311 | + $url .= '/'; |
|
312 | + $url .= '{' . |
|
313 | + static::typeChar(isset($call['metadata']['param'][$position]['type']) |
|
314 | + ? $call['metadata']['param'][$position]['type'] |
|
315 | + : null) |
|
316 | + . $position . '}'; |
|
317 | + if ($allowAmbiguity || $position == $lastPathParam) { |
|
318 | + static::addPath($url, $call, $httpMethod, $version); |
|
319 | + } |
|
320 | + } |
|
321 | + } |
|
322 | + } |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * @access private |
|
327 | - */ |
|
328 | - public static function typeChar($type = null) |
|
329 | - { |
|
330 | - if (!$type) { |
|
331 | - return 's'; |
|
332 | - } |
|
333 | - switch ($type[0]) { |
|
334 | - case 'i': |
|
335 | - case 'f': |
|
336 | - return 'n'; |
|
337 | - } |
|
338 | - return 's'; |
|
339 | - } |
|
325 | + /** |
|
326 | + * @access private |
|
327 | + */ |
|
328 | + public static function typeChar($type = null) |
|
329 | + { |
|
330 | + if (!$type) { |
|
331 | + return 's'; |
|
332 | + } |
|
333 | + switch ($type[0]) { |
|
334 | + case 'i': |
|
335 | + case 'f': |
|
336 | + return 'n'; |
|
337 | + } |
|
338 | + return 's'; |
|
339 | + } |
|
340 | 340 | |
341 | - protected static function addPath($path, array $call, |
|
342 | - $httpMethod = 'GET', $version = 1) |
|
343 | - { |
|
344 | - $call['url'] = preg_replace_callback( |
|
345 | - "/\{\S(\d+)\}/", |
|
346 | - function ($matches) use ($call) { |
|
347 | - return '{' . |
|
348 | - $call['metadata']['param'][$matches[1]]['name'] . '}'; |
|
349 | - }, |
|
350 | - $path |
|
351 | - ); |
|
352 | - //check for wildcard routes |
|
353 | - if (substr($path, -1, 1) == '*') { |
|
354 | - $path = rtrim($path, '/*'); |
|
355 | - static::$routes["v$version"]['*'][$path][$httpMethod] = $call; |
|
356 | - } else { |
|
357 | - static::$routes["v$version"][$path][$httpMethod] = $call; |
|
358 | - //create an alias with index if the method name is index |
|
359 | - if ($call['methodName'] == 'index') |
|
360 | - static::$routes["v$version"][ltrim("$path/index", '/')][$httpMethod] = $call; |
|
361 | - } |
|
362 | - } |
|
341 | + protected static function addPath($path, array $call, |
|
342 | + $httpMethod = 'GET', $version = 1) |
|
343 | + { |
|
344 | + $call['url'] = preg_replace_callback( |
|
345 | + "/\{\S(\d+)\}/", |
|
346 | + function ($matches) use ($call) { |
|
347 | + return '{' . |
|
348 | + $call['metadata']['param'][$matches[1]]['name'] . '}'; |
|
349 | + }, |
|
350 | + $path |
|
351 | + ); |
|
352 | + //check for wildcard routes |
|
353 | + if (substr($path, -1, 1) == '*') { |
|
354 | + $path = rtrim($path, '/*'); |
|
355 | + static::$routes["v$version"]['*'][$path][$httpMethod] = $call; |
|
356 | + } else { |
|
357 | + static::$routes["v$version"][$path][$httpMethod] = $call; |
|
358 | + //create an alias with index if the method name is index |
|
359 | + if ($call['methodName'] == 'index') |
|
360 | + static::$routes["v$version"][ltrim("$path/index", '/')][$httpMethod] = $call; |
|
361 | + } |
|
362 | + } |
|
363 | 363 | |
364 | - /** |
|
365 | - * Find the api method for the given url and http method |
|
366 | - * |
|
367 | - * @param string $path Requested url path |
|
368 | - * @param string $httpMethod GET|POST|PUT|PATCH|DELETE etc |
|
369 | - * @param int $version Api Version number |
|
370 | - * @param array $data Data collected from the request |
|
371 | - * |
|
372 | - * @throws RestException |
|
373 | - * @return ApiMethodInfo |
|
374 | - */ |
|
375 | - public static function find($path, $httpMethod, |
|
376 | - $version = 1, array $data = array()) |
|
377 | - { |
|
378 | - $p = Util::nestedValue(static::$routes, "v$version"); |
|
379 | - if (!$p) { |
|
380 | - throw new RestException( |
|
381 | - 404, |
|
382 | - $version == 1 ? '' : "Version $version is not supported" |
|
383 | - ); |
|
384 | - } |
|
385 | - $status = 404; |
|
386 | - $message = null; |
|
387 | - $methods = array(); |
|
388 | - if (isset($p[$path][$httpMethod])) { |
|
389 | - //================== static routes ========================== |
|
390 | - return static::populate($p[$path][$httpMethod], $data); |
|
391 | - } elseif (isset($p['*'])) { |
|
392 | - //================== wildcard routes ======================== |
|
393 | - uksort($p['*'], function ($a, $b) { |
|
394 | - return strlen($b) - strlen($a); |
|
395 | - }); |
|
396 | - foreach ($p['*'] as $key => $value) { |
|
397 | - if (strpos($path, $key) === 0 && isset($value[$httpMethod])) { |
|
398 | - //path found, convert rest of the path to parameters |
|
399 | - $path = substr($path, strlen($key) + 1); |
|
400 | - $call = ApiMethodInfo::__set_state($value[$httpMethod]); |
|
401 | - $call->parameters = empty($path) |
|
402 | - ? array() |
|
403 | - : explode('/', $path); |
|
404 | - return $call; |
|
405 | - } |
|
406 | - } |
|
407 | - } |
|
408 | - //================== dynamic routes ============================= |
|
409 | - //add newline char if trailing slash is found |
|
410 | - if (substr($path, -1) == '/') |
|
411 | - $path .= PHP_EOL; |
|
412 | - //if double slash is found fill in newline char; |
|
413 | - $path = str_replace('//', '/' . PHP_EOL . '/', $path); |
|
414 | - ksort($p); |
|
415 | - foreach ($p as $key => $value) { |
|
416 | - if (!isset($value[$httpMethod])) { |
|
417 | - continue; |
|
418 | - } |
|
419 | - $regex = str_replace(array('{', '}'), |
|
420 | - array('(?P<', '>[^/]+)'), $key); |
|
421 | - if (preg_match_all(":^$regex$:i", $path, $matches, PREG_SET_ORDER)) { |
|
422 | - $matches = $matches[0]; |
|
423 | - $found = true; |
|
424 | - foreach ($matches as $k => $v) { |
|
425 | - if (is_numeric($k)) { |
|
426 | - unset($matches[$k]); |
|
427 | - continue; |
|
428 | - } |
|
429 | - $index = intval(substr($k, 1)); |
|
430 | - $details = $value[$httpMethod]['metadata']['param'][$index]; |
|
431 | - if ($k[0] == 's' || strpos($k, static::pathVarTypeOf($v)) === 0) { |
|
432 | - //remove the newlines |
|
433 | - $data[$details['name']] = trim($v, PHP_EOL); |
|
434 | - } else { |
|
435 | - $status = 400; |
|
436 | - $message = 'invalid value specified for `' |
|
437 | - . $details['name'] . '`'; |
|
438 | - $found = false; |
|
439 | - break; |
|
440 | - } |
|
441 | - } |
|
442 | - if ($found) { |
|
443 | - return static::populate($value[$httpMethod], $data); |
|
444 | - } |
|
445 | - } |
|
446 | - } |
|
447 | - if ($status == 404) { |
|
448 | - //check if other methods are allowed |
|
449 | - if (isset($p[$path])) { |
|
450 | - $status = 405; |
|
451 | - $methods = array_keys($p[$path]); |
|
452 | - } |
|
453 | - } |
|
454 | - if ($status == 405) { |
|
455 | - header('Allow: ' . implode(', ', $methods)); |
|
456 | - } |
|
457 | - throw new RestException($status, $message); |
|
458 | - } |
|
364 | + /** |
|
365 | + * Find the api method for the given url and http method |
|
366 | + * |
|
367 | + * @param string $path Requested url path |
|
368 | + * @param string $httpMethod GET|POST|PUT|PATCH|DELETE etc |
|
369 | + * @param int $version Api Version number |
|
370 | + * @param array $data Data collected from the request |
|
371 | + * |
|
372 | + * @throws RestException |
|
373 | + * @return ApiMethodInfo |
|
374 | + */ |
|
375 | + public static function find($path, $httpMethod, |
|
376 | + $version = 1, array $data = array()) |
|
377 | + { |
|
378 | + $p = Util::nestedValue(static::$routes, "v$version"); |
|
379 | + if (!$p) { |
|
380 | + throw new RestException( |
|
381 | + 404, |
|
382 | + $version == 1 ? '' : "Version $version is not supported" |
|
383 | + ); |
|
384 | + } |
|
385 | + $status = 404; |
|
386 | + $message = null; |
|
387 | + $methods = array(); |
|
388 | + if (isset($p[$path][$httpMethod])) { |
|
389 | + //================== static routes ========================== |
|
390 | + return static::populate($p[$path][$httpMethod], $data); |
|
391 | + } elseif (isset($p['*'])) { |
|
392 | + //================== wildcard routes ======================== |
|
393 | + uksort($p['*'], function ($a, $b) { |
|
394 | + return strlen($b) - strlen($a); |
|
395 | + }); |
|
396 | + foreach ($p['*'] as $key => $value) { |
|
397 | + if (strpos($path, $key) === 0 && isset($value[$httpMethod])) { |
|
398 | + //path found, convert rest of the path to parameters |
|
399 | + $path = substr($path, strlen($key) + 1); |
|
400 | + $call = ApiMethodInfo::__set_state($value[$httpMethod]); |
|
401 | + $call->parameters = empty($path) |
|
402 | + ? array() |
|
403 | + : explode('/', $path); |
|
404 | + return $call; |
|
405 | + } |
|
406 | + } |
|
407 | + } |
|
408 | + //================== dynamic routes ============================= |
|
409 | + //add newline char if trailing slash is found |
|
410 | + if (substr($path, -1) == '/') |
|
411 | + $path .= PHP_EOL; |
|
412 | + //if double slash is found fill in newline char; |
|
413 | + $path = str_replace('//', '/' . PHP_EOL . '/', $path); |
|
414 | + ksort($p); |
|
415 | + foreach ($p as $key => $value) { |
|
416 | + if (!isset($value[$httpMethod])) { |
|
417 | + continue; |
|
418 | + } |
|
419 | + $regex = str_replace(array('{', '}'), |
|
420 | + array('(?P<', '>[^/]+)'), $key); |
|
421 | + if (preg_match_all(":^$regex$:i", $path, $matches, PREG_SET_ORDER)) { |
|
422 | + $matches = $matches[0]; |
|
423 | + $found = true; |
|
424 | + foreach ($matches as $k => $v) { |
|
425 | + if (is_numeric($k)) { |
|
426 | + unset($matches[$k]); |
|
427 | + continue; |
|
428 | + } |
|
429 | + $index = intval(substr($k, 1)); |
|
430 | + $details = $value[$httpMethod]['metadata']['param'][$index]; |
|
431 | + if ($k[0] == 's' || strpos($k, static::pathVarTypeOf($v)) === 0) { |
|
432 | + //remove the newlines |
|
433 | + $data[$details['name']] = trim($v, PHP_EOL); |
|
434 | + } else { |
|
435 | + $status = 400; |
|
436 | + $message = 'invalid value specified for `' |
|
437 | + . $details['name'] . '`'; |
|
438 | + $found = false; |
|
439 | + break; |
|
440 | + } |
|
441 | + } |
|
442 | + if ($found) { |
|
443 | + return static::populate($value[$httpMethod], $data); |
|
444 | + } |
|
445 | + } |
|
446 | + } |
|
447 | + if ($status == 404) { |
|
448 | + //check if other methods are allowed |
|
449 | + if (isset($p[$path])) { |
|
450 | + $status = 405; |
|
451 | + $methods = array_keys($p[$path]); |
|
452 | + } |
|
453 | + } |
|
454 | + if ($status == 405) { |
|
455 | + header('Allow: ' . implode(', ', $methods)); |
|
456 | + } |
|
457 | + throw new RestException($status, $message); |
|
458 | + } |
|
459 | 459 | |
460 | - public static function findAll(array $excludedPaths = array(), array $excludedHttpMethods = array(), $version = 1) |
|
461 | - { |
|
462 | - $map = array(); |
|
463 | - $all = Util::nestedValue(self::$routes, "v$version"); |
|
464 | - $filter = array(); |
|
465 | - if (isset($all['*'])) { |
|
466 | - $all = $all['*'] + $all; |
|
467 | - unset($all['*']); |
|
468 | - } |
|
469 | - if(is_array($all)){ |
|
470 | - foreach ($all as $fullPath => $routes) { |
|
471 | - foreach ($routes as $httpMethod => $route) { |
|
472 | - if (in_array($httpMethod, $excludedHttpMethods)) { |
|
473 | - continue; |
|
474 | - } |
|
475 | - foreach ($excludedPaths as $exclude) { |
|
476 | - if (empty($exclude)) { |
|
477 | - if ($fullPath == $exclude || $fullPath == 'index') |
|
478 | - continue 2; |
|
479 | - } elseif (Text::beginsWith($fullPath, $exclude)) { |
|
480 | - continue 2; |
|
481 | - } |
|
482 | - } |
|
483 | - $hash = "$httpMethod " . $route['url']; |
|
484 | - if (!isset($filter[$hash])) { |
|
485 | - $route['httpMethod'] = $httpMethod; |
|
486 | - $map[$route['metadata']['resourcePath']][] |
|
487 | - = array('access' => static::verifyAccess($route), 'route' => $route, 'hash' => $hash); |
|
488 | - $filter[$hash] = true; |
|
489 | - } |
|
490 | - } |
|
491 | - } |
|
492 | - } |
|
493 | - return $map; |
|
494 | - } |
|
460 | + public static function findAll(array $excludedPaths = array(), array $excludedHttpMethods = array(), $version = 1) |
|
461 | + { |
|
462 | + $map = array(); |
|
463 | + $all = Util::nestedValue(self::$routes, "v$version"); |
|
464 | + $filter = array(); |
|
465 | + if (isset($all['*'])) { |
|
466 | + $all = $all['*'] + $all; |
|
467 | + unset($all['*']); |
|
468 | + } |
|
469 | + if(is_array($all)){ |
|
470 | + foreach ($all as $fullPath => $routes) { |
|
471 | + foreach ($routes as $httpMethod => $route) { |
|
472 | + if (in_array($httpMethod, $excludedHttpMethods)) { |
|
473 | + continue; |
|
474 | + } |
|
475 | + foreach ($excludedPaths as $exclude) { |
|
476 | + if (empty($exclude)) { |
|
477 | + if ($fullPath == $exclude || $fullPath == 'index') |
|
478 | + continue 2; |
|
479 | + } elseif (Text::beginsWith($fullPath, $exclude)) { |
|
480 | + continue 2; |
|
481 | + } |
|
482 | + } |
|
483 | + $hash = "$httpMethod " . $route['url']; |
|
484 | + if (!isset($filter[$hash])) { |
|
485 | + $route['httpMethod'] = $httpMethod; |
|
486 | + $map[$route['metadata']['resourcePath']][] |
|
487 | + = array('access' => static::verifyAccess($route), 'route' => $route, 'hash' => $hash); |
|
488 | + $filter[$hash] = true; |
|
489 | + } |
|
490 | + } |
|
491 | + } |
|
492 | + } |
|
493 | + return $map; |
|
494 | + } |
|
495 | 495 | |
496 | - public static function verifyAccess($route) |
|
497 | - { |
|
498 | - if ($route['accessLevel'] < 2) |
|
499 | - return true; |
|
500 | - /** @var Restler $r */ |
|
501 | - $r = Scope::get('Restler'); |
|
502 | - $authenticated = $r->_authenticated; |
|
503 | - if (!$authenticated && $route['accessLevel'] > 1) |
|
504 | - return false; |
|
505 | - if ( |
|
506 | - $authenticated && |
|
507 | - Defaults::$accessControlFunction && |
|
508 | - (!call_user_func(Defaults::$accessControlFunction, $route['metadata'])) |
|
509 | - ) { |
|
510 | - return false; |
|
511 | - } |
|
512 | - return true; |
|
513 | - } |
|
496 | + public static function verifyAccess($route) |
|
497 | + { |
|
498 | + if ($route['accessLevel'] < 2) |
|
499 | + return true; |
|
500 | + /** @var Restler $r */ |
|
501 | + $r = Scope::get('Restler'); |
|
502 | + $authenticated = $r->_authenticated; |
|
503 | + if (!$authenticated && $route['accessLevel'] > 1) |
|
504 | + return false; |
|
505 | + if ( |
|
506 | + $authenticated && |
|
507 | + Defaults::$accessControlFunction && |
|
508 | + (!call_user_func(Defaults::$accessControlFunction, $route['metadata'])) |
|
509 | + ) { |
|
510 | + return false; |
|
511 | + } |
|
512 | + return true; |
|
513 | + } |
|
514 | 514 | |
515 | 515 | |
516 | - /** |
|
517 | - * Populates the parameter values |
|
518 | - * |
|
519 | - * @param array $call |
|
520 | - * @param $data |
|
521 | - * |
|
522 | - * @return ApiMethodInfo |
|
523 | - * |
|
524 | - * @access private |
|
525 | - */ |
|
526 | - protected static function populate(array $call, $data) |
|
527 | - { |
|
528 | - $call['parameters'] = $call['defaults']; |
|
529 | - $p = & $call['parameters']; |
|
530 | - $dataName = CommentParser::$embeddedDataName; |
|
531 | - foreach ($data as $key => $value) { |
|
532 | - if (isset($call['arguments'][$key])) { |
|
533 | - $p[$call['arguments'][$key]] = $value; |
|
534 | - } |
|
535 | - } |
|
536 | - if (Defaults::$smartParameterParsing) { |
|
537 | - if ( |
|
538 | - ($m = Util::nestedValue($call, 'metadata', 'param', 0)) && |
|
539 | - !array_key_exists($m['name'], $data) && |
|
540 | - array_key_exists(Defaults::$fullRequestDataName, $data) && |
|
541 | - !is_null($d = $data[Defaults::$fullRequestDataName]) && |
|
542 | - isset($m['type']) && |
|
543 | - static::typeMatch($m['type'], $d) |
|
544 | - ) { |
|
545 | - $p[0] = $d; |
|
546 | - } else { |
|
547 | - $bodyParamCount = 0; |
|
548 | - $lastBodyParamIndex = -1; |
|
549 | - $lastM = null; |
|
550 | - foreach ($call['metadata']['param'] as $k => $m) { |
|
551 | - if ($m[$dataName]['from'] == 'body') { |
|
552 | - $bodyParamCount++; |
|
553 | - $lastBodyParamIndex = $k; |
|
554 | - $lastM = $m; |
|
555 | - } |
|
556 | - } |
|
557 | - if ( |
|
558 | - $bodyParamCount == 1 && |
|
559 | - !array_key_exists($lastM['name'], $data) && |
|
560 | - array_key_exists(Defaults::$fullRequestDataName, $data) && |
|
561 | - !is_null($d = $data[Defaults::$fullRequestDataName]) |
|
562 | - ) { |
|
563 | - $p[$lastBodyParamIndex] = $d; |
|
564 | - } |
|
565 | - } |
|
566 | - } |
|
567 | - $r = ApiMethodInfo::__set_state($call); |
|
568 | - $modifier = "_modify_{$r->methodName}_api"; |
|
569 | - if (method_exists($r->className, $modifier)) { |
|
570 | - $stage = end(Scope::get('Restler')->getEvents()); |
|
571 | - if (empty($stage)) |
|
572 | - $stage = 'setup'; |
|
573 | - $r = Scope::get($r->className)->$modifier($r, $stage) ? : $r; |
|
574 | - } |
|
575 | - return $r; |
|
576 | - } |
|
516 | + /** |
|
517 | + * Populates the parameter values |
|
518 | + * |
|
519 | + * @param array $call |
|
520 | + * @param $data |
|
521 | + * |
|
522 | + * @return ApiMethodInfo |
|
523 | + * |
|
524 | + * @access private |
|
525 | + */ |
|
526 | + protected static function populate(array $call, $data) |
|
527 | + { |
|
528 | + $call['parameters'] = $call['defaults']; |
|
529 | + $p = & $call['parameters']; |
|
530 | + $dataName = CommentParser::$embeddedDataName; |
|
531 | + foreach ($data as $key => $value) { |
|
532 | + if (isset($call['arguments'][$key])) { |
|
533 | + $p[$call['arguments'][$key]] = $value; |
|
534 | + } |
|
535 | + } |
|
536 | + if (Defaults::$smartParameterParsing) { |
|
537 | + if ( |
|
538 | + ($m = Util::nestedValue($call, 'metadata', 'param', 0)) && |
|
539 | + !array_key_exists($m['name'], $data) && |
|
540 | + array_key_exists(Defaults::$fullRequestDataName, $data) && |
|
541 | + !is_null($d = $data[Defaults::$fullRequestDataName]) && |
|
542 | + isset($m['type']) && |
|
543 | + static::typeMatch($m['type'], $d) |
|
544 | + ) { |
|
545 | + $p[0] = $d; |
|
546 | + } else { |
|
547 | + $bodyParamCount = 0; |
|
548 | + $lastBodyParamIndex = -1; |
|
549 | + $lastM = null; |
|
550 | + foreach ($call['metadata']['param'] as $k => $m) { |
|
551 | + if ($m[$dataName]['from'] == 'body') { |
|
552 | + $bodyParamCount++; |
|
553 | + $lastBodyParamIndex = $k; |
|
554 | + $lastM = $m; |
|
555 | + } |
|
556 | + } |
|
557 | + if ( |
|
558 | + $bodyParamCount == 1 && |
|
559 | + !array_key_exists($lastM['name'], $data) && |
|
560 | + array_key_exists(Defaults::$fullRequestDataName, $data) && |
|
561 | + !is_null($d = $data[Defaults::$fullRequestDataName]) |
|
562 | + ) { |
|
563 | + $p[$lastBodyParamIndex] = $d; |
|
564 | + } |
|
565 | + } |
|
566 | + } |
|
567 | + $r = ApiMethodInfo::__set_state($call); |
|
568 | + $modifier = "_modify_{$r->methodName}_api"; |
|
569 | + if (method_exists($r->className, $modifier)) { |
|
570 | + $stage = end(Scope::get('Restler')->getEvents()); |
|
571 | + if (empty($stage)) |
|
572 | + $stage = 'setup'; |
|
573 | + $r = Scope::get($r->className)->$modifier($r, $stage) ? : $r; |
|
574 | + } |
|
575 | + return $r; |
|
576 | + } |
|
577 | 577 | |
578 | - /** |
|
579 | - * @access private |
|
580 | - */ |
|
581 | - protected static function pathVarTypeOf($var) |
|
582 | - { |
|
583 | - if (is_numeric($var)) { |
|
584 | - return 'n'; |
|
585 | - } |
|
586 | - if ($var === 'true' || $var === 'false') { |
|
587 | - return 'b'; |
|
588 | - } |
|
589 | - return 's'; |
|
590 | - } |
|
578 | + /** |
|
579 | + * @access private |
|
580 | + */ |
|
581 | + protected static function pathVarTypeOf($var) |
|
582 | + { |
|
583 | + if (is_numeric($var)) { |
|
584 | + return 'n'; |
|
585 | + } |
|
586 | + if ($var === 'true' || $var === 'false') { |
|
587 | + return 'b'; |
|
588 | + } |
|
589 | + return 's'; |
|
590 | + } |
|
591 | 591 | |
592 | - protected static function typeMatch($type, $var) |
|
593 | - { |
|
594 | - switch ($type) { |
|
595 | - case 'boolean': |
|
596 | - case 'bool': |
|
597 | - return is_bool($var); |
|
598 | - case 'array': |
|
599 | - case 'object': |
|
600 | - return is_array($var); |
|
601 | - case 'string': |
|
602 | - case 'int': |
|
603 | - case 'integer': |
|
604 | - case 'float': |
|
605 | - case 'number': |
|
606 | - return is_scalar($var); |
|
607 | - } |
|
608 | - return true; |
|
609 | - } |
|
592 | + protected static function typeMatch($type, $var) |
|
593 | + { |
|
594 | + switch ($type) { |
|
595 | + case 'boolean': |
|
596 | + case 'bool': |
|
597 | + return is_bool($var); |
|
598 | + case 'array': |
|
599 | + case 'object': |
|
600 | + return is_array($var); |
|
601 | + case 'string': |
|
602 | + case 'int': |
|
603 | + case 'integer': |
|
604 | + case 'float': |
|
605 | + case 'number': |
|
606 | + return is_scalar($var); |
|
607 | + } |
|
608 | + return true; |
|
609 | + } |
|
610 | 610 | |
611 | - protected static function parseMagic(ReflectionClass $class, $forResponse = true) |
|
612 | - { |
|
613 | - if (!$c = CommentParser::parse($class->getDocComment())) { |
|
614 | - return false; |
|
615 | - } |
|
616 | - $p = 'property'; |
|
617 | - $r = empty($c[$p]) ? array() : $c[$p]; |
|
618 | - $p .= '-' . ($forResponse ? 'read' : 'write'); |
|
619 | - if (!empty($c[$p])) { |
|
620 | - $r = array_merge($r, $c[$p]); |
|
621 | - } |
|
611 | + protected static function parseMagic(ReflectionClass $class, $forResponse = true) |
|
612 | + { |
|
613 | + if (!$c = CommentParser::parse($class->getDocComment())) { |
|
614 | + return false; |
|
615 | + } |
|
616 | + $p = 'property'; |
|
617 | + $r = empty($c[$p]) ? array() : $c[$p]; |
|
618 | + $p .= '-' . ($forResponse ? 'read' : 'write'); |
|
619 | + if (!empty($c[$p])) { |
|
620 | + $r = array_merge($r, $c[$p]); |
|
621 | + } |
|
622 | 622 | |
623 | - return $r; |
|
624 | - } |
|
623 | + return $r; |
|
624 | + } |
|
625 | 625 | |
626 | - /** |
|
627 | - * Get the type and associated model |
|
628 | - * |
|
629 | - * @param ReflectionClass $class |
|
630 | - * @param array $scope |
|
631 | - * |
|
632 | - * @throws RestException |
|
633 | - * @throws \Exception |
|
634 | - * @return array |
|
635 | - * |
|
636 | - * @access protected |
|
637 | - */ |
|
638 | - protected static function getTypeAndModel(ReflectionClass $class, array $scope, $prefix='', array $rules=array()) |
|
639 | - { |
|
640 | - $className = $class->getName(); |
|
641 | - $dataName = CommentParser::$embeddedDataName; |
|
642 | - if (isset(static::$models[$prefix.$className])) { |
|
643 | - return static::$models[$prefix.$className]; |
|
644 | - } |
|
645 | - $children = array(); |
|
646 | - try { |
|
647 | - if ($magic_properties = static::parseMagic($class, empty($prefix))) { |
|
648 | - foreach ($magic_properties as $prop) { |
|
649 | - if (!isset($prop['name'])) { |
|
650 | - throw new Exception('@property comment is not properly defined in ' . $className . ' class'); |
|
651 | - } |
|
652 | - if (!isset($prop[$dataName]['label'])) { |
|
653 | - $prop[$dataName]['label'] = Text::title($prop['name']); |
|
654 | - } |
|
655 | - if (isset(static::$fieldTypesByName[$prop['name']]) && $prop['type'] == 'string' && !isset($prop[$dataName]['type'])) { |
|
656 | - $prop[$dataName]['type'] = static::$fieldTypesByName[$prop['name']]; |
|
657 | - } |
|
658 | - $children[$prop['name']] = $prop; |
|
659 | - } |
|
660 | - } else { |
|
661 | - $props = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
|
662 | - foreach ($props as $prop) { |
|
663 | - $name = $prop->getName(); |
|
664 | - $child = array('name' => $name); |
|
665 | - if ($c = $prop->getDocComment()) { |
|
666 | - $child += Util::nestedValue(CommentParser::parse($c), 'var') ?: array(); |
|
667 | - } else { |
|
668 | - $o = $class->newInstance(); |
|
669 | - $p = $prop->getValue($o); |
|
670 | - if (is_object($p)) { |
|
671 | - $child['type'] = get_class($p); |
|
672 | - } elseif (is_array($p)) { |
|
673 | - $child['type'] = 'array'; |
|
674 | - if (count($p)) { |
|
675 | - $pc = reset($p); |
|
676 | - if (is_object($pc)) { |
|
677 | - $child['contentType'] = get_class($pc); |
|
678 | - } |
|
679 | - } |
|
680 | - } |
|
681 | - } |
|
682 | - $child += array( |
|
683 | - 'type' => isset(static::$fieldTypesByName[$child['name']]) |
|
684 | - ? static::$fieldTypesByName[$child['name']] |
|
685 | - : 'string', |
|
686 | - 'label' => Text::title($child['name']) |
|
687 | - ); |
|
688 | - isset($child[$dataName]) |
|
689 | - ? $child[$dataName] += array('required' => true) |
|
690 | - : $child[$dataName]['required'] = true; |
|
691 | - if ($prop->class != $className && $qualified = Scope::resolve($child['type'], $scope)) { |
|
692 | - list($child['type'], $child['children']) |
|
693 | - = static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
|
694 | - } elseif ( |
|
695 | - ($contentType = Util::nestedValue($child, $dataName, 'type')) && |
|
696 | - ($qualified = Scope::resolve($contentType, $scope)) |
|
697 | - ) { |
|
698 | - list($child['contentType'], $child['children']) |
|
699 | - = static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
|
700 | - } |
|
701 | - $children[$name] = $child; |
|
702 | - } |
|
703 | - } |
|
704 | - } catch (Exception $e) { |
|
705 | - if (Text::endsWith($e->getFile(), 'CommentParser.php')) { |
|
706 | - throw new RestException(500, "Error while parsing comments of `$className` class. " . $e->getMessage()); |
|
707 | - } |
|
708 | - throw $e; |
|
709 | - } |
|
710 | - if ($properties = Util::nestedValue($rules, 'properties')) { |
|
711 | - if (is_string($properties)) { |
|
712 | - $properties = array($properties); |
|
713 | - } |
|
714 | - $c = array(); |
|
715 | - foreach ($properties as $property) { |
|
716 | - if (isset($children[$property])) { |
|
717 | - $c[$property] = $children[$property]; |
|
718 | - } |
|
719 | - } |
|
720 | - $children = $c; |
|
721 | - } |
|
722 | - if ($required = Util::nestedValue($rules, 'required')) { |
|
723 | - //override required on children |
|
724 | - if (is_bool($required)) { |
|
725 | - // true means all are required false means none are required |
|
726 | - $required = $required ? array_keys($children) : array(); |
|
727 | - } elseif (is_string($required)) { |
|
728 | - $required = array($required); |
|
729 | - } |
|
730 | - $required = array_fill_keys($required, true); |
|
731 | - foreach ($children as $name => $child) { |
|
732 | - $children[$name][$dataName]['required'] = isset($required[$name]); |
|
733 | - } |
|
734 | - } |
|
735 | - static::$models[$prefix.$className] = array($className, $children, $prefix.$className); |
|
736 | - return static::$models[$prefix.$className]; |
|
737 | - } |
|
626 | + /** |
|
627 | + * Get the type and associated model |
|
628 | + * |
|
629 | + * @param ReflectionClass $class |
|
630 | + * @param array $scope |
|
631 | + * |
|
632 | + * @throws RestException |
|
633 | + * @throws \Exception |
|
634 | + * @return array |
|
635 | + * |
|
636 | + * @access protected |
|
637 | + */ |
|
638 | + protected static function getTypeAndModel(ReflectionClass $class, array $scope, $prefix='', array $rules=array()) |
|
639 | + { |
|
640 | + $className = $class->getName(); |
|
641 | + $dataName = CommentParser::$embeddedDataName; |
|
642 | + if (isset(static::$models[$prefix.$className])) { |
|
643 | + return static::$models[$prefix.$className]; |
|
644 | + } |
|
645 | + $children = array(); |
|
646 | + try { |
|
647 | + if ($magic_properties = static::parseMagic($class, empty($prefix))) { |
|
648 | + foreach ($magic_properties as $prop) { |
|
649 | + if (!isset($prop['name'])) { |
|
650 | + throw new Exception('@property comment is not properly defined in ' . $className . ' class'); |
|
651 | + } |
|
652 | + if (!isset($prop[$dataName]['label'])) { |
|
653 | + $prop[$dataName]['label'] = Text::title($prop['name']); |
|
654 | + } |
|
655 | + if (isset(static::$fieldTypesByName[$prop['name']]) && $prop['type'] == 'string' && !isset($prop[$dataName]['type'])) { |
|
656 | + $prop[$dataName]['type'] = static::$fieldTypesByName[$prop['name']]; |
|
657 | + } |
|
658 | + $children[$prop['name']] = $prop; |
|
659 | + } |
|
660 | + } else { |
|
661 | + $props = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
|
662 | + foreach ($props as $prop) { |
|
663 | + $name = $prop->getName(); |
|
664 | + $child = array('name' => $name); |
|
665 | + if ($c = $prop->getDocComment()) { |
|
666 | + $child += Util::nestedValue(CommentParser::parse($c), 'var') ?: array(); |
|
667 | + } else { |
|
668 | + $o = $class->newInstance(); |
|
669 | + $p = $prop->getValue($o); |
|
670 | + if (is_object($p)) { |
|
671 | + $child['type'] = get_class($p); |
|
672 | + } elseif (is_array($p)) { |
|
673 | + $child['type'] = 'array'; |
|
674 | + if (count($p)) { |
|
675 | + $pc = reset($p); |
|
676 | + if (is_object($pc)) { |
|
677 | + $child['contentType'] = get_class($pc); |
|
678 | + } |
|
679 | + } |
|
680 | + } |
|
681 | + } |
|
682 | + $child += array( |
|
683 | + 'type' => isset(static::$fieldTypesByName[$child['name']]) |
|
684 | + ? static::$fieldTypesByName[$child['name']] |
|
685 | + : 'string', |
|
686 | + 'label' => Text::title($child['name']) |
|
687 | + ); |
|
688 | + isset($child[$dataName]) |
|
689 | + ? $child[$dataName] += array('required' => true) |
|
690 | + : $child[$dataName]['required'] = true; |
|
691 | + if ($prop->class != $className && $qualified = Scope::resolve($child['type'], $scope)) { |
|
692 | + list($child['type'], $child['children']) |
|
693 | + = static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
|
694 | + } elseif ( |
|
695 | + ($contentType = Util::nestedValue($child, $dataName, 'type')) && |
|
696 | + ($qualified = Scope::resolve($contentType, $scope)) |
|
697 | + ) { |
|
698 | + list($child['contentType'], $child['children']) |
|
699 | + = static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
|
700 | + } |
|
701 | + $children[$name] = $child; |
|
702 | + } |
|
703 | + } |
|
704 | + } catch (Exception $e) { |
|
705 | + if (Text::endsWith($e->getFile(), 'CommentParser.php')) { |
|
706 | + throw new RestException(500, "Error while parsing comments of `$className` class. " . $e->getMessage()); |
|
707 | + } |
|
708 | + throw $e; |
|
709 | + } |
|
710 | + if ($properties = Util::nestedValue($rules, 'properties')) { |
|
711 | + if (is_string($properties)) { |
|
712 | + $properties = array($properties); |
|
713 | + } |
|
714 | + $c = array(); |
|
715 | + foreach ($properties as $property) { |
|
716 | + if (isset($children[$property])) { |
|
717 | + $c[$property] = $children[$property]; |
|
718 | + } |
|
719 | + } |
|
720 | + $children = $c; |
|
721 | + } |
|
722 | + if ($required = Util::nestedValue($rules, 'required')) { |
|
723 | + //override required on children |
|
724 | + if (is_bool($required)) { |
|
725 | + // true means all are required false means none are required |
|
726 | + $required = $required ? array_keys($children) : array(); |
|
727 | + } elseif (is_string($required)) { |
|
728 | + $required = array($required); |
|
729 | + } |
|
730 | + $required = array_fill_keys($required, true); |
|
731 | + foreach ($children as $name => $child) { |
|
732 | + $children[$name][$dataName]['required'] = isset($required[$name]); |
|
733 | + } |
|
734 | + } |
|
735 | + static::$models[$prefix.$className] = array($className, $children, $prefix.$className); |
|
736 | + return static::$models[$prefix.$className]; |
|
737 | + } |
|
738 | 738 | |
739 | - /** |
|
740 | - * Import previously created routes from cache |
|
741 | - * |
|
742 | - * @param array $routes |
|
743 | - */ |
|
744 | - public static function fromArray(array $routes) |
|
745 | - { |
|
746 | - static::$routes = $routes; |
|
747 | - } |
|
739 | + /** |
|
740 | + * Import previously created routes from cache |
|
741 | + * |
|
742 | + * @param array $routes |
|
743 | + */ |
|
744 | + public static function fromArray(array $routes) |
|
745 | + { |
|
746 | + static::$routes = $routes; |
|
747 | + } |
|
748 | 748 | |
749 | - /** |
|
750 | - * Export current routes for cache |
|
751 | - * |
|
752 | - * @return array |
|
753 | - */ |
|
754 | - public static function toArray() |
|
755 | - { |
|
756 | - return static::$routes; |
|
757 | - } |
|
749 | + /** |
|
750 | + * Export current routes for cache |
|
751 | + * |
|
752 | + * @return array |
|
753 | + */ |
|
754 | + public static function toArray() |
|
755 | + { |
|
756 | + return static::$routes; |
|
757 | + } |
|
758 | 758 | |
759 | - public static function type($var) |
|
760 | - { |
|
761 | - if (is_object($var)) return get_class($var); |
|
762 | - if (is_array($var)) return 'array'; |
|
763 | - if (is_bool($var)) return 'boolean'; |
|
764 | - if (is_numeric($var)) return is_float($var) ? 'float' : 'int'; |
|
765 | - return 'string'; |
|
766 | - } |
|
759 | + public static function type($var) |
|
760 | + { |
|
761 | + if (is_object($var)) return get_class($var); |
|
762 | + if (is_array($var)) return 'array'; |
|
763 | + if (is_bool($var)) return 'boolean'; |
|
764 | + if (is_numeric($var)) return is_float($var) ? 'float' : 'int'; |
|
765 | + return 'string'; |
|
766 | + } |
|
767 | 767 | |
768 | - public static function scope(ReflectionClass $class) |
|
769 | - { |
|
770 | - $namespace = $class->getNamespaceName(); |
|
771 | - $imports = array( |
|
772 | - '*' => empty($namespace) ? '' : $namespace . '\\' |
|
773 | - ); |
|
774 | - $file = file_get_contents($class->getFileName()); |
|
775 | - $tokens = token_get_all($file); |
|
776 | - $namespace = ''; |
|
777 | - $alias = ''; |
|
778 | - $reading = false; |
|
779 | - $last = 0; |
|
780 | - foreach ($tokens as $token) { |
|
781 | - if (is_string($token)) { |
|
782 | - if ($reading && ',' == $token) { |
|
783 | - //===== STOP =====// |
|
784 | - $reading = false; |
|
785 | - if (!empty($namespace)) |
|
786 | - $imports[$alias] = trim($namespace, '\\'); |
|
787 | - //===== START =====// |
|
788 | - $reading = true; |
|
789 | - $namespace = ''; |
|
790 | - $alias = ''; |
|
791 | - } else { |
|
792 | - //===== STOP =====// |
|
793 | - $reading = false; |
|
794 | - if (!empty($namespace)) |
|
795 | - $imports[$alias] = trim($namespace, '\\'); |
|
796 | - } |
|
797 | - } elseif (T_USE == $token[0]) { |
|
798 | - //===== START =====// |
|
799 | - $reading = true; |
|
800 | - $namespace = ''; |
|
801 | - $alias = ''; |
|
802 | - } elseif ($reading) { |
|
803 | - //echo token_name($token[0]) . ' ' . $token[1] . PHP_EOL; |
|
804 | - switch ($token[0]) { |
|
805 | - case T_WHITESPACE: |
|
806 | - continue 2; |
|
807 | - case T_STRING: |
|
808 | - $alias = $token[1]; |
|
809 | - if (T_AS == $last) { |
|
810 | - break; |
|
811 | - } |
|
812 | - //don't break; |
|
813 | - case T_NS_SEPARATOR: |
|
814 | - $namespace .= $token[1]; |
|
815 | - break; |
|
816 | - } |
|
817 | - $last = $token[0]; |
|
818 | - } |
|
819 | - } |
|
820 | - return $imports; |
|
821 | - } |
|
768 | + public static function scope(ReflectionClass $class) |
|
769 | + { |
|
770 | + $namespace = $class->getNamespaceName(); |
|
771 | + $imports = array( |
|
772 | + '*' => empty($namespace) ? '' : $namespace . '\\' |
|
773 | + ); |
|
774 | + $file = file_get_contents($class->getFileName()); |
|
775 | + $tokens = token_get_all($file); |
|
776 | + $namespace = ''; |
|
777 | + $alias = ''; |
|
778 | + $reading = false; |
|
779 | + $last = 0; |
|
780 | + foreach ($tokens as $token) { |
|
781 | + if (is_string($token)) { |
|
782 | + if ($reading && ',' == $token) { |
|
783 | + //===== STOP =====// |
|
784 | + $reading = false; |
|
785 | + if (!empty($namespace)) |
|
786 | + $imports[$alias] = trim($namespace, '\\'); |
|
787 | + //===== START =====// |
|
788 | + $reading = true; |
|
789 | + $namespace = ''; |
|
790 | + $alias = ''; |
|
791 | + } else { |
|
792 | + //===== STOP =====// |
|
793 | + $reading = false; |
|
794 | + if (!empty($namespace)) |
|
795 | + $imports[$alias] = trim($namespace, '\\'); |
|
796 | + } |
|
797 | + } elseif (T_USE == $token[0]) { |
|
798 | + //===== START =====// |
|
799 | + $reading = true; |
|
800 | + $namespace = ''; |
|
801 | + $alias = ''; |
|
802 | + } elseif ($reading) { |
|
803 | + //echo token_name($token[0]) . ' ' . $token[1] . PHP_EOL; |
|
804 | + switch ($token[0]) { |
|
805 | + case T_WHITESPACE: |
|
806 | + continue 2; |
|
807 | + case T_STRING: |
|
808 | + $alias = $token[1]; |
|
809 | + if (T_AS == $last) { |
|
810 | + break; |
|
811 | + } |
|
812 | + //don't break; |
|
813 | + case T_NS_SEPARATOR: |
|
814 | + $namespace .= $token[1]; |
|
815 | + break; |
|
816 | + } |
|
817 | + $last = $token[0]; |
|
818 | + } |
|
819 | + } |
|
820 | + return $imports; |
|
821 | + } |
|
822 | 822 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | try { |
80 | 80 | $classMetadata = CommentParser::parse($class->getDocComment()); |
81 | 81 | } catch (Exception $e) { |
82 | - throw new RestException(500, "Error while parsing comments of `$className` class. " . $e->getMessage()); |
|
82 | + throw new RestException(500, "Error while parsing comments of `$className` class. ".$e->getMessage()); |
|
83 | 83 | } |
84 | 84 | $classMetadata['scope'] = $scope = static::scope($class); |
85 | 85 | $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC + |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | try { |
96 | 96 | $metadata = CommentParser::parse($doc) + $classMetadata; |
97 | 97 | } catch (Exception $e) { |
98 | - throw new RestException(500, "Error while parsing comments of `{$className}::{$method->getName()}` method. " . $e->getMessage()); |
|
98 | + throw new RestException(500, "Error while parsing comments of `{$className}::{$method->getName()}` method. ".$e->getMessage()); |
|
99 | 99 | } |
100 | 100 | //@access should not be private |
101 | 101 | if (isset($metadata['access']) |
@@ -157,20 +157,20 @@ discard block |
||
157 | 157 | } |
158 | 158 | $m ['default'] = $defaults [$position]; |
159 | 159 | $m ['required'] = !$param->isOptional(); |
160 | - $contentType = Util::nestedValue($p,'type'); |
|
160 | + $contentType = Util::nestedValue($p, 'type'); |
|
161 | 161 | if ($type == 'array' && $contentType && $qualified = Scope::resolve($contentType, $scope)) { |
162 | 162 | list($p['type'], $children, $modelName) = static::getTypeAndModel( |
163 | 163 | new ReflectionClass($qualified), $scope, |
164 | - $className . Text::title($methodUrl), $p |
|
164 | + $className.Text::title($methodUrl), $p |
|
165 | 165 | ); |
166 | 166 | } |
167 | 167 | if ($type instanceof ReflectionClass) { |
168 | 168 | list($type, $children, $modelName) = static::getTypeAndModel($type, $scope, |
169 | - $className . Text::title($methodUrl), $p); |
|
169 | + $className.Text::title($methodUrl), $p); |
|
170 | 170 | } elseif ($type && is_string($type) && $qualified = Scope::resolve($type, $scope)) { |
171 | 171 | list($type, $children, $modelName) |
172 | 172 | = static::getTypeAndModel(new ReflectionClass($qualified), $scope, |
173 | - $className . Text::title($methodUrl), $p); |
|
173 | + $className.Text::title($methodUrl), $p); |
|
174 | 174 | } |
175 | 175 | if (isset($type)) { |
176 | 176 | $m['type'] = $type; |
@@ -248,13 +248,13 @@ discard block |
||
248 | 248 | ) { |
249 | 249 | foreach ($matches as $match) { |
250 | 250 | $httpMethod = $match[1]; |
251 | - $url = rtrim($resourcePath . $match[2], '/'); |
|
251 | + $url = rtrim($resourcePath.$match[2], '/'); |
|
252 | 252 | //deep copy the call, as it may change for each @url |
253 | 253 | $copy = unserialize(serialize($call)); |
254 | 254 | foreach ($copy['metadata']['param'] as $i => $p) { |
255 | 255 | $inPath = |
256 | - strpos($url, '{' . $p['name'] . '}') || |
|
257 | - strpos($url, ':' . $p['name']); |
|
256 | + strpos($url, '{'.$p['name'].'}') || |
|
257 | + strpos($url, ':'.$p['name']); |
|
258 | 258 | if ($inPath) { |
259 | 259 | $copy['metadata']['param'][$i][$dataName]['from'] = 'path'; |
260 | 260 | } elseif ($httpMethod == 'GET' || $httpMethod == 'DELETE') { |
@@ -264,15 +264,15 @@ discard block |
||
264 | 264 | } |
265 | 265 | } |
266 | 266 | $url = preg_replace_callback('/{[^}]+}|:[^\/]+/', |
267 | - function ($matches) use ($copy) { |
|
267 | + function($matches) use ($copy) { |
|
268 | 268 | $match = trim($matches[0], '{}:'); |
269 | 269 | $index = $copy['arguments'][$match]; |
270 | - return '{' . |
|
270 | + return '{'. |
|
271 | 271 | Routes::typeChar(isset( |
272 | 272 | $copy['metadata']['param'][$index]['type']) |
273 | 273 | ? $copy['metadata']['param'][$index]['type'] |
274 | 274 | : null) |
275 | - . $index . '}'; |
|
275 | + . $index.'}'; |
|
276 | 276 | }, $url); |
277 | 277 | static::addPath($url, $copy, $httpMethod, $version); |
278 | 278 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | $methodUrl = ''; |
293 | 293 | } |
294 | 294 | $url = empty($methodUrl) ? rtrim($resourcePath, '/') |
295 | - : $resourcePath . $methodUrl; |
|
295 | + : $resourcePath.$methodUrl; |
|
296 | 296 | for ($position = 0; $position < count($params); $position++) { |
297 | 297 | $from = $metadata['param'][$position][$dataName]['from']; |
298 | 298 | if ($from == 'body' && ($httpMethod == 'GET' || |
@@ -309,11 +309,11 @@ discard block |
||
309 | 309 | foreach ($pathParams as $position) { |
310 | 310 | if (!empty($url)) |
311 | 311 | $url .= '/'; |
312 | - $url .= '{' . |
|
312 | + $url .= '{'. |
|
313 | 313 | static::typeChar(isset($call['metadata']['param'][$position]['type']) |
314 | 314 | ? $call['metadata']['param'][$position]['type'] |
315 | 315 | : null) |
316 | - . $position . '}'; |
|
316 | + . $position.'}'; |
|
317 | 317 | if ($allowAmbiguity || $position == $lastPathParam) { |
318 | 318 | static::addPath($url, $call, $httpMethod, $version); |
319 | 319 | } |
@@ -343,9 +343,9 @@ discard block |
||
343 | 343 | { |
344 | 344 | $call['url'] = preg_replace_callback( |
345 | 345 | "/\{\S(\d+)\}/", |
346 | - function ($matches) use ($call) { |
|
347 | - return '{' . |
|
348 | - $call['metadata']['param'][$matches[1]]['name'] . '}'; |
|
346 | + function($matches) use ($call) { |
|
347 | + return '{'. |
|
348 | + $call['metadata']['param'][$matches[1]]['name'].'}'; |
|
349 | 349 | }, |
350 | 350 | $path |
351 | 351 | ); |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | return static::populate($p[$path][$httpMethod], $data); |
391 | 391 | } elseif (isset($p['*'])) { |
392 | 392 | //================== wildcard routes ======================== |
393 | - uksort($p['*'], function ($a, $b) { |
|
393 | + uksort($p['*'], function($a, $b) { |
|
394 | 394 | return strlen($b) - strlen($a); |
395 | 395 | }); |
396 | 396 | foreach ($p['*'] as $key => $value) { |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | if (substr($path, -1) == '/') |
411 | 411 | $path .= PHP_EOL; |
412 | 412 | //if double slash is found fill in newline char; |
413 | - $path = str_replace('//', '/' . PHP_EOL . '/', $path); |
|
413 | + $path = str_replace('//', '/'.PHP_EOL.'/', $path); |
|
414 | 414 | ksort($p); |
415 | 415 | foreach ($p as $key => $value) { |
416 | 416 | if (!isset($value[$httpMethod])) { |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | } else { |
435 | 435 | $status = 400; |
436 | 436 | $message = 'invalid value specified for `' |
437 | - . $details['name'] . '`'; |
|
437 | + . $details['name'].'`'; |
|
438 | 438 | $found = false; |
439 | 439 | break; |
440 | 440 | } |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | } |
453 | 453 | } |
454 | 454 | if ($status == 405) { |
455 | - header('Allow: ' . implode(', ', $methods)); |
|
455 | + header('Allow: '.implode(', ', $methods)); |
|
456 | 456 | } |
457 | 457 | throw new RestException($status, $message); |
458 | 458 | } |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | $all = $all['*'] + $all; |
467 | 467 | unset($all['*']); |
468 | 468 | } |
469 | - if(is_array($all)){ |
|
469 | + if (is_array($all)) { |
|
470 | 470 | foreach ($all as $fullPath => $routes) { |
471 | 471 | foreach ($routes as $httpMethod => $route) { |
472 | 472 | if (in_array($httpMethod, $excludedHttpMethods)) { |
@@ -480,7 +480,7 @@ discard block |
||
480 | 480 | continue 2; |
481 | 481 | } |
482 | 482 | } |
483 | - $hash = "$httpMethod " . $route['url']; |
|
483 | + $hash = "$httpMethod ".$route['url']; |
|
484 | 484 | if (!isset($filter[$hash])) { |
485 | 485 | $route['httpMethod'] = $httpMethod; |
486 | 486 | $map[$route['metadata']['resourcePath']][] |
@@ -570,7 +570,7 @@ discard block |
||
570 | 570 | $stage = end(Scope::get('Restler')->getEvents()); |
571 | 571 | if (empty($stage)) |
572 | 572 | $stage = 'setup'; |
573 | - $r = Scope::get($r->className)->$modifier($r, $stage) ? : $r; |
|
573 | + $r = Scope::get($r->className)->$modifier($r, $stage) ?: $r; |
|
574 | 574 | } |
575 | 575 | return $r; |
576 | 576 | } |
@@ -615,7 +615,7 @@ discard block |
||
615 | 615 | } |
616 | 616 | $p = 'property'; |
617 | 617 | $r = empty($c[$p]) ? array() : $c[$p]; |
618 | - $p .= '-' . ($forResponse ? 'read' : 'write'); |
|
618 | + $p .= '-'.($forResponse ? 'read' : 'write'); |
|
619 | 619 | if (!empty($c[$p])) { |
620 | 620 | $r = array_merge($r, $c[$p]); |
621 | 621 | } |
@@ -635,7 +635,7 @@ discard block |
||
635 | 635 | * |
636 | 636 | * @access protected |
637 | 637 | */ |
638 | - protected static function getTypeAndModel(ReflectionClass $class, array $scope, $prefix='', array $rules=array()) |
|
638 | + protected static function getTypeAndModel(ReflectionClass $class, array $scope, $prefix = '', array $rules = array()) |
|
639 | 639 | { |
640 | 640 | $className = $class->getName(); |
641 | 641 | $dataName = CommentParser::$embeddedDataName; |
@@ -647,7 +647,7 @@ discard block |
||
647 | 647 | if ($magic_properties = static::parseMagic($class, empty($prefix))) { |
648 | 648 | foreach ($magic_properties as $prop) { |
649 | 649 | if (!isset($prop['name'])) { |
650 | - throw new Exception('@property comment is not properly defined in ' . $className . ' class'); |
|
650 | + throw new Exception('@property comment is not properly defined in '.$className.' class'); |
|
651 | 651 | } |
652 | 652 | if (!isset($prop[$dataName]['label'])) { |
653 | 653 | $prop[$dataName]['label'] = Text::title($prop['name']); |
@@ -703,7 +703,7 @@ discard block |
||
703 | 703 | } |
704 | 704 | } catch (Exception $e) { |
705 | 705 | if (Text::endsWith($e->getFile(), 'CommentParser.php')) { |
706 | - throw new RestException(500, "Error while parsing comments of `$className` class. " . $e->getMessage()); |
|
706 | + throw new RestException(500, "Error while parsing comments of `$className` class. ".$e->getMessage()); |
|
707 | 707 | } |
708 | 708 | throw $e; |
709 | 709 | } |
@@ -769,7 +769,7 @@ discard block |
||
769 | 769 | { |
770 | 770 | $namespace = $class->getNamespaceName(); |
771 | 771 | $imports = array( |
772 | - '*' => empty($namespace) ? '' : $namespace . '\\' |
|
772 | + '*' => empty($namespace) ? '' : $namespace.'\\' |
|
773 | 773 | ); |
774 | 774 | $file = file_get_contents($class->getFileName()); |
775 | 775 | $tokens = token_get_all($file); |
@@ -124,9 +124,10 @@ discard block |
||
124 | 124 | $metadata['param'] = array(); |
125 | 125 | } |
126 | 126 | if (isset($metadata['return']['type'])) { |
127 | - if ($qualified = Scope::resolve($metadata['return']['type'], $scope)) |
|
128 | - list($metadata['return']['type'], $metadata['return']['children']) = |
|
127 | + if ($qualified = Scope::resolve($metadata['return']['type'], $scope)) { |
|
128 | + list($metadata['return']['type'], $metadata['return']['children']) = |
|
129 | 129 | static::getTypeAndModel(new ReflectionClass($qualified), $scope); |
130 | + } |
|
130 | 131 | } else { |
131 | 132 | //assume return type is array |
132 | 133 | $metadata['return']['type'] = 'array'; |
@@ -147,8 +148,9 @@ discard block |
||
147 | 148 | $m[$dataName] = array(); |
148 | 149 | } |
149 | 150 | $p = &$m[$dataName]; |
150 | - if (empty($m['label'])) |
|
151 | - $m['label'] = Text::title($m['name']); |
|
151 | + if (empty($m['label'])) { |
|
152 | + $m['label'] = Text::title($m['name']); |
|
153 | + } |
|
152 | 154 | if (is_null($type) && isset($m['type'])) { |
153 | 155 | $type = $m['type']; |
154 | 156 | } |
@@ -307,8 +309,9 @@ discard block |
||
307 | 309 | } |
308 | 310 | $lastPathParam = end($pathParams); |
309 | 311 | foreach ($pathParams as $position) { |
310 | - if (!empty($url)) |
|
311 | - $url .= '/'; |
|
312 | + if (!empty($url)) { |
|
313 | + $url .= '/'; |
|
314 | + } |
|
312 | 315 | $url .= '{' . |
313 | 316 | static::typeChar(isset($call['metadata']['param'][$position]['type']) |
314 | 317 | ? $call['metadata']['param'][$position]['type'] |
@@ -356,8 +359,9 @@ discard block |
||
356 | 359 | } else { |
357 | 360 | static::$routes["v$version"][$path][$httpMethod] = $call; |
358 | 361 | //create an alias with index if the method name is index |
359 | - if ($call['methodName'] == 'index') |
|
360 | - static::$routes["v$version"][ltrim("$path/index", '/')][$httpMethod] = $call; |
|
362 | + if ($call['methodName'] == 'index') { |
|
363 | + static::$routes["v$version"][ltrim("$path/index", '/')][$httpMethod] = $call; |
|
364 | + } |
|
361 | 365 | } |
362 | 366 | } |
363 | 367 | |
@@ -407,8 +411,9 @@ discard block |
||
407 | 411 | } |
408 | 412 | //================== dynamic routes ============================= |
409 | 413 | //add newline char if trailing slash is found |
410 | - if (substr($path, -1) == '/') |
|
411 | - $path .= PHP_EOL; |
|
414 | + if (substr($path, -1) == '/') { |
|
415 | + $path .= PHP_EOL; |
|
416 | + } |
|
412 | 417 | //if double slash is found fill in newline char; |
413 | 418 | $path = str_replace('//', '/' . PHP_EOL . '/', $path); |
414 | 419 | ksort($p); |
@@ -474,8 +479,9 @@ discard block |
||
474 | 479 | } |
475 | 480 | foreach ($excludedPaths as $exclude) { |
476 | 481 | if (empty($exclude)) { |
477 | - if ($fullPath == $exclude || $fullPath == 'index') |
|
478 | - continue 2; |
|
482 | + if ($fullPath == $exclude || $fullPath == 'index') { |
|
483 | + continue 2; |
|
484 | + } |
|
479 | 485 | } elseif (Text::beginsWith($fullPath, $exclude)) { |
480 | 486 | continue 2; |
481 | 487 | } |
@@ -495,13 +501,15 @@ discard block |
||
495 | 501 | |
496 | 502 | public static function verifyAccess($route) |
497 | 503 | { |
498 | - if ($route['accessLevel'] < 2) |
|
499 | - return true; |
|
504 | + if ($route['accessLevel'] < 2) { |
|
505 | + return true; |
|
506 | + } |
|
500 | 507 | /** @var Restler $r */ |
501 | 508 | $r = Scope::get('Restler'); |
502 | 509 | $authenticated = $r->_authenticated; |
503 | - if (!$authenticated && $route['accessLevel'] > 1) |
|
504 | - return false; |
|
510 | + if (!$authenticated && $route['accessLevel'] > 1) { |
|
511 | + return false; |
|
512 | + } |
|
505 | 513 | if ( |
506 | 514 | $authenticated && |
507 | 515 | Defaults::$accessControlFunction && |
@@ -568,8 +576,9 @@ discard block |
||
568 | 576 | $modifier = "_modify_{$r->methodName}_api"; |
569 | 577 | if (method_exists($r->className, $modifier)) { |
570 | 578 | $stage = end(Scope::get('Restler')->getEvents()); |
571 | - if (empty($stage)) |
|
572 | - $stage = 'setup'; |
|
579 | + if (empty($stage)) { |
|
580 | + $stage = 'setup'; |
|
581 | + } |
|
573 | 582 | $r = Scope::get($r->className)->$modifier($r, $stage) ? : $r; |
574 | 583 | } |
575 | 584 | return $r; |
@@ -758,10 +767,18 @@ discard block |
||
758 | 767 | |
759 | 768 | public static function type($var) |
760 | 769 | { |
761 | - if (is_object($var)) return get_class($var); |
|
762 | - if (is_array($var)) return 'array'; |
|
763 | - if (is_bool($var)) return 'boolean'; |
|
764 | - if (is_numeric($var)) return is_float($var) ? 'float' : 'int'; |
|
770 | + if (is_object($var)) { |
|
771 | + return get_class($var); |
|
772 | + } |
|
773 | + if (is_array($var)) { |
|
774 | + return 'array'; |
|
775 | + } |
|
776 | + if (is_bool($var)) { |
|
777 | + return 'boolean'; |
|
778 | + } |
|
779 | + if (is_numeric($var)) { |
|
780 | + return is_float($var) ? 'float' : 'int'; |
|
781 | + } |
|
765 | 782 | return 'string'; |
766 | 783 | } |
767 | 784 | |
@@ -782,8 +799,9 @@ discard block |
||
782 | 799 | if ($reading && ',' == $token) { |
783 | 800 | //===== STOP =====// |
784 | 801 | $reading = false; |
785 | - if (!empty($namespace)) |
|
786 | - $imports[$alias] = trim($namespace, '\\'); |
|
802 | + if (!empty($namespace)) { |
|
803 | + $imports[$alias] = trim($namespace, '\\'); |
|
804 | + } |
|
787 | 805 | //===== START =====// |
788 | 806 | $reading = true; |
789 | 807 | $namespace = ''; |
@@ -791,8 +809,9 @@ discard block |
||
791 | 809 | } else { |
792 | 810 | //===== STOP =====// |
793 | 811 | $reading = false; |
794 | - if (!empty($namespace)) |
|
795 | - $imports[$alias] = trim($namespace, '\\'); |
|
812 | + if (!empty($namespace)) { |
|
813 | + $imports[$alias] = trim($namespace, '\\'); |
|
814 | + } |
|
796 | 815 | } |
797 | 816 | } elseif (T_USE == $token[0]) { |
798 | 817 | //===== START =====// |
@@ -379,7 +379,7 @@ |
||
379 | 379 | if (!$p) { |
380 | 380 | throw new RestException( |
381 | 381 | 404, |
382 | - $version == 1 ? '' : "Version $version is not supported" |
|
382 | + $version == 1 ? '' : "version $version is not supported" |
|
383 | 383 | ); |
384 | 384 | } |
385 | 385 | $status = 404; |
@@ -9,9 +9,9 @@ |
||
9 | 9 | */ |
10 | 10 | interface iProvideMultiVersionApi |
11 | 11 | { |
12 | - /** |
|
13 | - * Maximum api version supported by the api class |
|
14 | - * @return int |
|
15 | - */ |
|
16 | - public static function __getMaximumSupportedVersion(); |
|
12 | + /** |
|
13 | + * Maximum api version supported by the api class |
|
14 | + * @return int |
|
15 | + */ |
|
16 | + public static function __getMaximumSupportedVersion(); |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -16,21 +16,21 @@ |
||
16 | 16 | * |
17 | 17 | */ |
18 | 18 | interface iCompose { |
19 | - /** |
|
20 | - * Result of an api call is passed to this method |
|
21 | - * to create a standard structure for the data |
|
22 | - * |
|
23 | - * @param mixed $result can be a primitive or array or object |
|
24 | - */ |
|
25 | - public function response($result); |
|
19 | + /** |
|
20 | + * Result of an api call is passed to this method |
|
21 | + * to create a standard structure for the data |
|
22 | + * |
|
23 | + * @param mixed $result can be a primitive or array or object |
|
24 | + */ |
|
25 | + public function response($result); |
|
26 | 26 | |
27 | - /** |
|
28 | - * When the api call results in RestException this method |
|
29 | - * will be called to return the error message |
|
30 | - * |
|
31 | - * @param RestException $exception exception that has reasons for failure |
|
32 | - * |
|
33 | - * @return |
|
34 | - */ |
|
35 | - public function message(RestException $exception); |
|
27 | + /** |
|
28 | + * When the api call results in RestException this method |
|
29 | + * will be called to return the error message |
|
30 | + * |
|
31 | + * @param RestException $exception exception that has reasons for failure |
|
32 | + * |
|
33 | + * @return |
|
34 | + */ |
|
35 | + public function message(RestException $exception); |
|
36 | 36 | } |
37 | 37 | \ No newline at end of file |