forked from laruence/yaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaf.c
440 lines (385 loc) · 13.1 KB
/
yaf.c
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
438
439
440
/*
+----------------------------------------------------------------------+
| Yet Another Framework |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "main/SAPI.h"
#include "Zend/zend_alloc.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "php_yaf.h"
#include "yaf_logo.h"
#include "yaf_loader.h"
#include "yaf_exception.h"
#include "yaf_application.h"
#include "yaf_dispatcher.h"
#include "yaf_config.h"
#include "yaf_view.h"
#include "yaf_controller.h"
#include "yaf_action.h"
#include "yaf_request.h"
#include "yaf_response.h"
#include "yaf_router.h"
#include "yaf_bootstrap.h"
#include "yaf_plugin.h"
#include "yaf_registry.h"
#include "yaf_session.h"
ZEND_DECLARE_MODULE_GLOBALS(yaf);
/* {{{ yaf_functions[]
*/
zend_function_entry yaf_functions[] = {
{NULL, NULL, NULL}
};
/* }}} */
void yaf_iterator_dtor(zend_object_iterator *iter) /* {{{ */ {
zval_ptr_dtor(&iter->data);
zval_ptr_dtor(&((yaf_iterator*)iter)->current);
/* zend_iterator_dtor(iter); */
}
/* }}} */
static int yaf_iterator_valid(zend_object_iterator *iter) /* {{{ */ {
return zend_hash_has_more_elements_ex(Z_ARRVAL(iter->data), &(((yaf_iterator*)iter)->pos));
}
/* }}} */
static void yaf_iterator_rewind(zend_object_iterator *iter) /* {{{ */ {
zend_hash_internal_pointer_reset_ex(Z_ARRVAL(iter->data), &(((yaf_iterator*)iter)->pos));
}
/* }}} */
static void yaf_iterator_move_forward(zend_object_iterator *iter) /* {{{ */ {
zend_hash_move_forward_ex(Z_ARRVAL(iter->data), &(((yaf_iterator*)iter)->pos));
}
/* }}} */
static zval *yaf_iterator_get_current_data(zend_object_iterator *iter) /* {{{ */ {
return zend_hash_get_current_data_ex(Z_ARRVAL(iter->data), &(((yaf_iterator*)iter)->pos));
}
/* }}} */
static void yaf_iterator_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */ {
zend_ulong idx;
zend_string *str;
switch (zend_hash_get_current_key_ex(Z_ARRVAL(iter->data), &str, &idx, &(((yaf_iterator*)iter)->pos))) {
case HASH_KEY_IS_STRING:
ZVAL_STR_COPY(key, str);
break;
case HASH_KEY_IS_LONG:
ZVAL_LONG(key, idx);
break;
default:
ZVAL_NULL(key);
break;
}
}
/* }}} */
zend_object_iterator_funcs yaf_iterator_funcs = /* {{{ */ {
yaf_iterator_dtor,
yaf_iterator_valid,
yaf_iterator_get_current_data,
yaf_iterator_get_current_key,
yaf_iterator_move_forward,
yaf_iterator_rewind,
NULL
};
/* }}} */
int yaf_call_user_method(zend_object *obj, zend_function* fbc, zval *ret, int num_arg, zval *a1, zval *a2) /* {{{ */ {
uint32_t call_info;
zend_execute_data *call;
if (UNEXPECTED(fbc->common.fn_flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE))) {
php_error_docref(NULL, E_WARNING, "cannot call %s method %s::%s()",
(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) == ZEND_ACC_PROTECTED?
"protected" : "private", ZSTR_VAL(obj->ce->name), ZSTR_VAL(fbc->common.function_name));
return 0;
}
#if PHP_VERSION_ID < 70400
call_info = ZEND_CALL_TOP_FUNCTION;
call = zend_vm_stack_push_call_frame(call_info, fbc, num_arg, NULL, obj);
#else
call_info = ZEND_CALL_TOP_FUNCTION | ZEND_CALL_HAS_THIS;
call = zend_vm_stack_push_call_frame(call_info, fbc, num_arg, obj);
#endif
call->symbol_table = NULL;
if (num_arg) {
if (UNEXPECTED(a2 == ((zval*)-1))) {
unsigned int i;
for (i = 0; i < num_arg; i++) {
ZVAL_COPY(ZEND_CALL_ARG(call, i+1), &a1[i]);
}
} else if (num_arg == 2) {
ZVAL_COPY(ZEND_CALL_ARG(call, 1), a1);
ZVAL_COPY(ZEND_CALL_ARG(call, 2), a2);
} else if (num_arg == 1) {
ZVAL_COPY(ZEND_CALL_ARG(call, 1), a1);
}
}
/* At least we should in calls of Dispatchers */
ZEND_ASSERT(EG(current_execute_data));
if (EXPECTED(fbc->type == ZEND_USER_FUNCTION)) {
zend_init_execute_data(call, (zend_op_array*)fbc, ret);
/* const zend_op *current_opline_before_exception = EG(opline_before_exception); */
zend_execute_ex(call);
/* EG(opline_before_exception) = current_opline_before_exception; */
} else {
ZEND_ASSERT(fbc->type == ZEND_INTERNAL_FUNCTION);
call->prev_execute_data = EG(current_execute_data);
EG(current_execute_data) = call;
if (EXPECTED(zend_execute_internal == NULL)) {
fbc->internal_function.handler(call, ret);
} else {
zend_execute_internal(call, ret);
}
EG(current_execute_data) = call->prev_execute_data;
zend_vm_stack_free_args(call);
}
zend_vm_stack_free_call_frame(call);
if (UNEXPECTED(EG(exception))) {
/* We should return directly to user codes */
ZVAL_UNDEF(ret);
return 0;
}
return 1;
}
/* }}} */
zend_string *yaf_build_camel_name(const char *str, size_t len) /* {{{ */ {
unsigned int i;
zend_string *name = zend_string_alloc(len, 0);
unsigned char *p = ZSTR_VAL(name);
*p++ = toupper(*str);
for (i = 1; i < len; i++) {
unsigned char ch = str[i];
if (ch != '_') {
*p++ = tolower(ch);
} else {
*p++ = ch;
i++;
*p++ = toupper(str[i]);
}
}
*p = '\0';
return name;
}
/* }}} */
zend_string *yaf_build_lower_name(const char *str, size_t len) /* {{{ */ {
unsigned int i;
zend_string *name = zend_string_alloc(len, 0);
unsigned char *p = ZSTR_VAL(name);
for (i = 0; i < len; i++) {
*p++ = tolower(str[i]);
}
*p = '\0';
return name;
}
/* }}} */
zend_string *yaf_canonical_name(int type, zend_string *name) /* {{{ */ {
const char *p = ZSTR_VAL(name);
const char *e = ZSTR_VAL(name) + ZSTR_LEN(name);
if (type) {
/* Module, Controller */
if ((*p < 'A' || *p > 'Z') && *p != '_') {
goto sanitize;
}
while (p++ != e) {
if (*p >= 'A' && *p <= 'Z') {
goto sanitize;
}
}
return zend_string_copy(name);
sanitize:
return yaf_build_camel_name(ZSTR_VAL(name), ZSTR_LEN(name));
} else {
return zend_string_tolower(name);
}
}
/* }}} */
/** {{{ PHP_INI_MH(OnUpdateSeparator)
*/
PHP_INI_MH(OnUpdateSeparator) {
YAF_G(name_separator) = ZSTR_VAL(new_value);
YAF_G(name_separator_len) = ZSTR_LEN(new_value);
return SUCCESS;
}
/* }}} */
/** {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("yaf.library", "", PHP_INI_ALL, OnUpdateString, global_library, zend_yaf_globals, yaf_globals)
STD_PHP_INI_BOOLEAN("yaf.action_prefer", "0", PHP_INI_ALL, OnUpdateBool, action_prefer, zend_yaf_globals, yaf_globals)
STD_PHP_INI_BOOLEAN("yaf.lowcase_path", "0", PHP_INI_ALL, OnUpdateBool, lowcase_path, zend_yaf_globals, yaf_globals)
STD_PHP_INI_BOOLEAN("yaf.use_spl_autoload", "0", PHP_INI_ALL, OnUpdateBool, use_spl_autoload, zend_yaf_globals, yaf_globals)
STD_PHP_INI_ENTRY("yaf.forward_limit", "5", PHP_INI_ALL, OnUpdateLongGEZero, forward_limit, zend_yaf_globals, yaf_globals)
STD_PHP_INI_BOOLEAN("yaf.name_suffix", "1", PHP_INI_ALL, OnUpdateBool, name_suffix, zend_yaf_globals, yaf_globals)
PHP_INI_ENTRY("yaf.name_separator", "", PHP_INI_ALL, OnUpdateSeparator)
STD_PHP_INI_ENTRY("yaf.environ", "product", PHP_INI_SYSTEM, OnUpdateString, environ_name, zend_yaf_globals, yaf_globals)
STD_PHP_INI_BOOLEAN("yaf.use_namespace", "0", PHP_INI_SYSTEM, OnUpdateBool, use_namespace, zend_yaf_globals, yaf_globals)
PHP_INI_END();
/* }}} */
/** {{{ PHP_GINIT_FUNCTION
*/
PHP_GINIT_FUNCTION(yaf)
{
memset(yaf_globals, 0, sizeof(*yaf_globals));
}
/* }}} */
/** {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(yaf)
{
REGISTER_INI_ENTRIES();
if (YAF_G(use_namespace)) {
REGISTER_STRINGL_CONSTANT("YAF\\VERSION", PHP_YAF_VERSION, sizeof(PHP_YAF_VERSION) - 1, CONST_PERSISTENT | CONST_CS);
REGISTER_STRINGL_CONSTANT("YAF\\ENVIRON", YAF_G(environ_name), strlen(YAF_G(environ_name)), CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\STARTUP_FAILED", YAF_ERR_STARTUP_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\ROUTE_FAILED", YAF_ERR_ROUTE_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\DISPATCH_FAILED", YAF_ERR_DISPATCH_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\AUTOLOAD_FAILED", YAF_ERR_AUTOLOAD_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\MODULE", YAF_ERR_NOTFOUND_MODULE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\CONTROLLER",YAF_ERR_NOTFOUND_CONTROLLER, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\ACTION", YAF_ERR_NOTFOUND_ACTION, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\VIEW", YAF_ERR_NOTFOUND_VIEW, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\CALL_FAILED", YAF_ERR_CALL_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\TYPE_ERROR", YAF_ERR_TYPE_ERROR, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF\\ERR\\ACCESS", YAF_ERR_ACCESS_ERROR, CONST_PERSISTENT | CONST_CS);
} else {
REGISTER_STRINGL_CONSTANT("YAF_VERSION", PHP_YAF_VERSION, sizeof(PHP_YAF_VERSION) - 1, CONST_PERSISTENT | CONST_CS);
REGISTER_STRINGL_CONSTANT("YAF_ENVIRON", YAF_G(environ_name),strlen(YAF_G(environ_name)), CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_STARTUP_FAILED", YAF_ERR_STARTUP_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_ROUTE_FAILED", YAF_ERR_ROUTE_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_DISPATCH_FAILED", YAF_ERR_DISPATCH_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_AUTOLOAD_FAILED", YAF_ERR_AUTOLOAD_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_MODULE", YAF_ERR_NOTFOUND_MODULE, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_CONTROLLER", YAF_ERR_NOTFOUND_CONTROLLER, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_ACTION", YAF_ERR_NOTFOUND_ACTION, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_VIEW", YAF_ERR_NOTFOUND_VIEW, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_CALL_FAILED", YAF_ERR_CALL_FAILED, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_TYPE_ERROR", YAF_ERR_TYPE_ERROR, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("YAF_ERR_ACCESS_ERROR", YAF_ERR_ACCESS_ERROR, CONST_PERSISTENT | CONST_CS);
}
/* startup components */
YAF_STARTUP(application);
YAF_STARTUP(bootstrap);
YAF_STARTUP(dispatcher);
YAF_STARTUP(loader);
YAF_STARTUP(request);
YAF_STARTUP(response);
YAF_STARTUP(controller);
YAF_STARTUP(action);
YAF_STARTUP(config);
YAF_STARTUP(view);
YAF_STARTUP(router);
YAF_STARTUP(plugin);
YAF_STARTUP(registry);
YAF_STARTUP(session);
YAF_STARTUP(exception);
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(yaf)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(yaf)
{
YAF_G(in_exception) = 0;
YAF_G(throw_exception) = 1;
YAF_G(catch_exception) = 0;
ZVAL_UNDEF(&YAF_G(registry));
ZVAL_UNDEF(&YAF_G(session));
ZVAL_UNDEF(&YAF_G(loader));
ZVAL_UNDEF(&YAF_G(app));
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(yaf)
{
zval_ptr_dtor(&YAF_G(registry));
zval_ptr_dtor(&YAF_G(session));
zval_ptr_dtor(&YAF_G(loader));
zval_ptr_dtor(&YAF_G(app));
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(yaf)
{
php_info_print_table_start();
if (PG(expose_php) && !sapi_module.phpinfo_as_text) {
php_info_print_table_header(2, "yaf support", YAF_LOGO_IMG"enabled");
} else {
php_info_print_table_header(2, "yaf support", "enabled");
}
php_info_print_table_row(2, "Version", PHP_YAF_VERSION);
php_info_print_table_row(2, "Supports", YAF_SUPPORT_URL);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/** {{{ DL support
*/
#ifdef COMPILE_DL_YAF
ZEND_GET_MODULE(yaf)
#endif
/* }}} */
/** {{{ module depends
*/
#if ZEND_MODULE_API_NO >= 20050922
zend_module_dep yaf_deps[] = {
ZEND_MOD_REQUIRED("spl")
ZEND_MOD_REQUIRED("pcre")
ZEND_MOD_OPTIONAL("session")
{NULL, NULL, NULL}
};
#endif
/* }}} */
/** {{{ yaf_module_entry
*/
zend_module_entry yaf_module_entry = {
#if ZEND_MODULE_API_NO >= 20050922
STANDARD_MODULE_HEADER_EX, NULL,
yaf_deps,
#else
STANDARD_MODULE_HEADER,
#endif
"yaf",
yaf_functions,
PHP_MINIT(yaf),
PHP_MSHUTDOWN(yaf),
PHP_RINIT(yaf),
PHP_RSHUTDOWN(yaf),
PHP_MINFO(yaf),
PHP_YAF_VERSION,
PHP_MODULE_GLOBALS(yaf),
PHP_GINIT(yaf),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/