1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
#include "string.h"
#include "ctype.h"
#include "php.h"
#include "main/php_main.h"
#include "Zend/zend_API.h"
#include "Zend/zend_variables.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "Zend/zend_interfaces.h"
#include "zend_object_handlers.h"
#include "ext/pcre/php_pcre.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_var.h"
#include "ct_helper.h"
#include "php_r3.h"
#include "r3_mux.h"
#include "r3_functions.h"
#include "php_expandable_mux.h"
#include "r3_controller.h"
#include "annotation/scanner.h"
#include "annotation/annot.h"
zend_class_entry *ce_r3_controller;
const zend_function_entry controller_methods[] = {
PHP_ME(Controller, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Controller, expand, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Controller, getActionMethods, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Controller, getActionRoutes, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Controller, before, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Controller, after, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Controller, toJson, NULL, ZEND_ACC_PUBLIC)
// PHP_ME(Controller, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_FE_END
};
zend_bool phannot_fetch_argument_value(zval **arg, zval** value TSRMLS_DC) {
zval **expr;
if (zend_hash_find(Z_ARRVAL_PP(arg), "expr", sizeof("expr"), (void**)&expr) == FAILURE ) {
return FAILURE;
}
return zend_hash_find(Z_ARRVAL_PP(expr), "value", sizeof("value"), (void**) value);
}
zend_bool phannot_fetch_argument_type(zval **arg, zval **type TSRMLS_DC) {
zval **expr;
if (zend_hash_find(Z_ARRVAL_PP(arg), "expr", sizeof("expr"), (void**)&expr) == FAILURE ) {
return FAILURE;
}
return zend_hash_find(Z_ARRVAL_PP(expr), "type", sizeof("type"), (void**)type);
}
int strpos(const char *haystack, char *needle)
{
char *p = strstr(haystack, needle);
if (p)
return p - haystack;
return -1;
}
void r3_init_controller(TSRMLS_D) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "R3\\Controller", controller_methods);
ce_r3_controller = zend_register_internal_class(&ce TSRMLS_CC);
zend_class_implements(ce_r3_controller TSRMLS_CC, 1, ce_r3_expandable_mux);
}
PHP_METHOD(Controller, __construct) {
}
/**
* Returns [ method, annotations ]
*/
PHP_METHOD(Controller, getActionMethods)
{
// get function table hash
HashTable *function_table = &Z_OBJCE_P(this_ptr)->function_table;
HashPosition pos;
array_init(return_value);
zend_function *mptr;
zend_hash_internal_pointer_reset_ex(function_table, &pos);
const char *fn;
size_t fn_len;
int p;
while (zend_hash_get_current_data_ex(function_table, (void **) &mptr, &pos) == SUCCESS) {
fn = mptr->common.function_name;
fn_len = strlen(mptr->common.function_name);
p = strpos(fn, "Action");
if ( p == -1 || (size_t)p != (fn_len - strlen("Action")) ) {
zend_hash_move_forward_ex(function_table, &pos);
continue;
}
// append the structure [method name, annotations]
zval *new_item;
zval *z_method_annotations;
zval *z_indexed_annotations;
zval *z_comment;
zval *z_file;
zval *z_line_start;
MAKE_STD_ZVAL(new_item);
array_init(new_item);
add_next_index_stringl(new_item, fn, fn_len, 1);
/*
* @var
*
* if there no annotation, we pass an empty array.
*
* The method annotation structure
*
*/
MAKE_STD_ZVAL(z_method_annotations);
array_init(z_method_annotations);
// simplified annotation information is saved to this variable.
MAKE_STD_ZVAL(z_indexed_annotations);
array_init(z_indexed_annotations);
if ( mptr->type == ZEND_USER_FUNCTION && mptr->op_array.doc_comment ) {
ALLOC_ZVAL(z_comment);
ZVAL_STRING(z_comment, mptr->op_array.doc_comment, 1);
ALLOC_ZVAL(z_file);
ZVAL_STRING(z_file, mptr->op_array.filename, 1);
ALLOC_ZVAL(z_line_start);
ZVAL_LONG(z_line_start, mptr->op_array.line_start);
/*
zval *z_line_end;
ALLOC_ZVAL(z_line_end);
ZVAL_LONG(z_line_start, mptr->op_array.line_end);
*/
zval **z_ann = NULL, **z_ann_name = NULL, **z_ann_arguments = NULL, **z_ann_argument = NULL, **z_ann_argument_value = NULL;
// TODO: make phannot_parse_annotations reads comment variable in char* type, so we don't need to create extra zval(s)
if (phannot_parse_annotations(z_method_annotations, z_comment, z_file, z_line_start TSRMLS_CC) == SUCCESS) {
/*
* z_method_annotations is an indexed array, which contains a structure like this:
*
* [
* { name => ... , type => ... , arguments => ... , file => , line => },
* { name => ... , type => ... , arguments => ... , file => , line => },
* { name => ... , type => ... , arguments => ... , file => , line => },
* ]
*
* the actuall structure:
*
* array(2) {
* [0]=>
* array(5) {
* ["type"]=>
* int(300)
* ["name"]=>
* string(5) "Route"
* ["arguments"]=>
* array(1) {
* [0]=>
* array(1) {
* ["expr"]=>
* array(2) {
* ["type"]=>
* int(303)
* ["value"]=>
* string(7) "/delete"
* }
* }
* }
* ["file"]=> string(48) "...."
* ["line"]=> int(54)
* },
* .....
* }
*/
HashPosition annp;
for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(z_method_annotations), &annp);
zend_hash_get_current_data_ex(Z_ARRVAL_P(z_method_annotations), (void**)&z_ann, &annp) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(z_method_annotations), &annp)
) {
if (zend_hash_find(Z_ARRVAL_P(*z_ann), "name", sizeof("name"), (void**)&z_ann_name) == FAILURE ) {
continue;
}
if (zend_hash_find(Z_ARRVAL_P(*z_ann), "arguments", sizeof("arguments"), (void**)&z_ann_arguments) == FAILURE ) {
continue;
}
// We currenly only support "@Method()" and "@Route"
if ( strncmp( Z_STRVAL_PP(z_ann_name), "Method", sizeof("Method") - 1 ) != 0
&& strncmp( Z_STRVAL_PP(z_ann_name), "Route", sizeof("Route") - 1 ) != 0 )
{
continue;
}
// read the first argument (we only support for one argument currently, and should support complex syntax later.)
if ( zend_hash_index_find(Z_ARRVAL_PP(z_ann_arguments), 0, (void**) &z_ann_argument) == SUCCESS ) {
if ( phannot_fetch_argument_value(z_ann_argument, (zval**) &z_ann_argument_value TSRMLS_CC) == SUCCESS ) {
Z_ADDREF_PP(z_ann_argument_value);
add_assoc_zval(z_indexed_annotations, Z_STRVAL_PP(z_ann_name), *z_ann_argument_value);
}
}
}
}
}
Z_ADDREF_P(z_indexed_annotations);
add_next_index_zval(new_item, z_indexed_annotations);
add_next_index_zval(return_value, new_item);
zend_hash_move_forward_ex(function_table, &pos);
}
return;
}
char * translate_method_name_to_path(const char *method_name)
{
char *p = strstr(method_name, "Action");
if ( p == NULL ) {
return NULL;
}
if ( strncmp(method_name, "indexAction", strlen("indexAction") ) == 0 ) {
// returns empty string as its path
return strndup("",sizeof("")-1);
}
char * new_path;
// XXX: this might overflow..
new_path = ecalloc( 128 , sizeof(char) );
int len = p - method_name;
char * c = (char*) method_name;
int x = 0;
new_path[x++] = '/';
int new_path_len = 1;
while( len-- ) {
if ( isupper(*c) ) {
new_path[x++] = '/';
new_path[x++] = tolower(*c);
new_path_len += 2;
} else {
new_path[x++] = *c;
new_path_len ++;
}
c++;
}
return new_path;
}
// return path => path pairs
// structure: [[ path, method name ], [ ... ], [ ... ], ... ]
PHP_METHOD(Controller, getActionRoutes)
{
zend_function *fe;
if ( zend_hash_quick_find( &ce_r3_controller->function_table, "getactionmethods", sizeof("getactionmethods"), zend_inline_hash_func(ZEND_STRS("getactionmethods")), (void **) &fe) == FAILURE ) {
php_error(E_ERROR, "getActionMethods method not found");
}
// call export method
zval *rv = NULL;
zend_call_method_with_0_params( &this_ptr, ce_r3_controller, &fe, "getactionmethods", &rv );
HashTable *func_list = Z_ARRVAL_P(rv);
HashPosition pointer;
zval **z_method_name;
zval **z_annotations;
zval **z_doc_method;
zval **z_doc_uri;
zval **item;
array_init(return_value);
for(zend_hash_internal_pointer_reset_ex(func_list, &pointer);
zend_hash_get_current_data_ex(func_list, (void**) &item, &pointer) == SUCCESS;
zend_hash_move_forward_ex(func_list, &pointer))
{
zend_hash_index_find(Z_ARRVAL_PP(item), 0, (void**)&z_method_name);
const char *method_name = Z_STRVAL_PP(z_method_name);
int method_name_len = Z_STRLEN_PP(z_method_name);
char *path = NULL;
zval *z_route_options;
MAKE_STD_ZVAL(z_route_options);
array_init(z_route_options);
if ( zend_hash_index_find(Z_ARRVAL_PP(item), 1, (void**)&z_annotations) == SUCCESS ) {
if (zend_hash_find(Z_ARRVAL_PP(z_annotations), "Route", sizeof("Route"), (void**)&z_doc_uri) == SUCCESS) {
path = estrndup(Z_STRVAL_PP(z_doc_uri), Z_STRLEN_PP(z_doc_uri));
}
if (zend_hash_find(Z_ARRVAL_PP(z_annotations), "Method", sizeof("Method"), (void**)&z_doc_method) == SUCCESS) {
Z_ADDREF_PP(z_doc_method);
add_assoc_zval(z_route_options, "method", *z_doc_method);
}
}
if (!path) {
path = translate_method_name_to_path(method_name);
}
// return structure [ path, method name, http method ]
zval * new_item;
MAKE_STD_ZVAL(new_item);
array_init_size(new_item, 3);
add_next_index_string(new_item, path, 1);
add_next_index_stringl(new_item, method_name, method_name_len, 0);
Z_ADDREF_P(z_route_options);
add_next_index_zval(new_item, z_route_options);
// append to the result array
add_next_index_zval(return_value, new_item);
}
return;
}
PHP_METHOD(Controller, expand)
{
zval *new_mux;
MAKE_STD_ZVAL(new_mux);
object_init_ex(new_mux, ce_r3_mux);
CALL_METHOD(Mux, __construct, new_mux, new_mux);
Z_ADDREF_P(new_mux); // add reference
zval *path_array = NULL;
zend_call_method_with_0_params( &this_ptr, ce_r3_controller, NULL, "getactionroutes", &path_array );
if ( Z_TYPE_P(path_array) != IS_ARRAY ) {
php_error(E_ERROR, "getActionRoutes does not return an array.");
RETURN_FALSE;
}
const char *class_name = NULL;
zend_uint class_name_len;
int dup = zend_get_object_classname(this_ptr, &class_name, &class_name_len TSRMLS_CC);
HashPosition pointer;
zval **path_entry = NULL;
for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(path_array), &pointer);
zend_hash_get_current_data_ex(Z_ARRVAL_P(path_array), (void**) &path_entry, &pointer) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(path_array), &pointer)
) {
zval **z_path;
zval **z_method;
zval **z_options = NULL;
if ( zend_hash_index_find(Z_ARRVAL_PP(path_entry), 0, (void**) &z_path) == FAILURE ) {
continue;
}
if ( zend_hash_index_find(Z_ARRVAL_PP(path_entry), 1, (void**) &z_method) == FAILURE ) {
continue;
}
zend_hash_index_find(Z_ARRVAL_PP(path_entry), 2, (void**) &z_options);
zval *z_callback;
MAKE_STD_ZVAL(z_callback);
array_init_size(z_callback, 2);
Z_ADDREF_P(z_callback);
Z_ADDREF_PP(z_method);
add_next_index_stringl(z_callback, class_name, class_name_len, 1);
add_next_index_zval(z_callback, *z_method);
zval *rv = NULL;
zend_call_method_with_3_params(&new_mux, ce_r3_mux, NULL, "add", strlen("add"), &rv, 3, *z_path, z_callback, *z_options TSRMLS_CC);
}
zval *rv = NULL;
zend_call_method_with_0_params(&new_mux, ce_r3_mux, NULL, "sort", &rv );
*return_value = *new_mux;
zval_copy_ctor(return_value);
}
PHP_METHOD(Controller, before) {
}
PHP_METHOD(Controller, after) {
}
PHP_METHOD(Controller, toJson)
{
zval *z_data;
long options = 0;
long depth = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &z_data, &options, &depth) == FAILURE) {
RETURN_FALSE;
}
zval *z_options;
zval *z_depth;
MAKE_STD_ZVAL(z_options);
MAKE_STD_ZVAL(z_depth);
ZVAL_LONG(z_depth, 0);
ZVAL_LONG(z_options, 0);
zval *rv = NULL;
// zend_call_method_with_3_params(NULL, NULL, NULL, "json_encode", sizeof("json_encode"), &rv, 3, z_data, z_options, z_depth TSRMLS_CC );
zend_call_method_with_2_params(NULL, NULL, NULL, "json_encode", &rv, z_data, z_options);
*return_value = *rv;
zval_copy_ctor(return_value);
}
|