-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaf_application.c
769 lines (681 loc) · 28 KB
/
yaf_application.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <laruence@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h" /* for zend_alter_ini_entry */
#include "Zend/zend_interfaces.h" /* for zend_call_method_with_* */
#include "php_yaf.h"
#include "yaf_namespace.h"
#include "yaf_application.h"
#include "yaf_dispatcher.h"
#include "yaf_router.h"
#include "yaf_config.h"
#include "yaf_loader.h"
#include "yaf_request.h"
#include "yaf_bootstrap.h"
#include "yaf_exception.h"
zend_class_entry * yaf_application_ce;
/** {{{ ARG_INFO
* */
ZEND_BEGIN_ARG_INFO_EX(yaf_application_void_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_construct_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, config)
ZEND_ARG_INFO(0, environ)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_app_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_execute_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, entry)
ZEND_ARG_INFO(0, ...)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_getconfig_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_getmodule_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_getdispatch_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_bootstrap_arginfo, 0, 0, 0)
ZEND_ARG_INFO(0, bootstrap)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_environ_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_run_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_application_setappdir_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, directory)
ZEND_END_ARG_INFO()
/* }}} */
int yaf_application_is_module_name(zend_string *name) /* {{{ */ {
zval *modules, *pzval;
yaf_application_t *app;
app = zend_read_static_property(yaf_application_ce, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP), 1);
if (UNEXPECTED(Z_TYPE_P(app) != IS_OBJECT)) {
return 0;
}
modules = zend_read_property(yaf_application_ce, app, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_MODULES), 1, NULL);
if (UNEXPECTED(Z_TYPE_P(modules) != IS_ARRAY)) {
return 0;
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(modules), pzval) {
if (UNEXPECTED(Z_TYPE_P(pzval) != IS_STRING)) {
continue;
}
if (zend_string_equals_ci(Z_STR_P(pzval), name)) {
return 1;
}
} ZEND_HASH_FOREACH_END();
return 0;
}
/* }}} */
int yaf_application_is_module_name_str(char *name, size_t len) /* {{{ */ {
zend_string *str = zend_string_init(name, len, 0);
int ret = yaf_application_is_module_name(str);
zend_string_release(str);
return ret;
}
/* }}} */
//application params init. phoenix
static int yaf_application_parse_option(zval *options) /* {{{ */ {
HashTable *conf;
zval *pzval, *psval, *app;
conf = HASH_OF(options);
if (UNEXPECTED((app = zend_hash_str_find(conf, ZEND_STRL("application"))) == NULL)) {
/* For back compatibilty */
if ((app = zend_hash_str_find(conf, ZEND_STRL("yaf"))) == NULL) {
yaf_trigger_error(YAF_ERR_TYPE_ERROR, "%s", "Expected an array of application configure");
return FAILURE;
}
}
if (UNEXPECTED(Z_TYPE_P(app) != IS_ARRAY)) {
yaf_trigger_error(YAF_ERR_TYPE_ERROR, "%s", "Expected an array of application configure");
return FAILURE;
}
if (UNEXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("directory"))) == NULL || Z_TYPE_P(pzval) != IS_STRING || Z_STRLEN_P(pzval) == 0)) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "%s", "Expected a directory entry in application configures");
return FAILURE;
}
if (UNEXPECTED(*(Z_STRVAL_P(pzval) + Z_STRLEN_P(pzval) - 1) == DEFAULT_SLASH)) {
YAF_G(directory) = zend_string_init(Z_STRVAL_P(pzval), Z_STRLEN_P(pzval) - 1, 0);
} else {
YAF_G(directory) = zend_string_copy(Z_STR_P(pzval));
}
if (UNEXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("ext"))) != NULL && Z_TYPE_P(pzval) == IS_STRING)) {
YAF_G(ext) = zend_string_copy(Z_STR_P(pzval));
}
if (UNEXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("bootstrap"))) != NULL && Z_TYPE_P(pzval) == IS_STRING)) {
YAF_G(bootstrap) = zend_string_copy(Z_STR_P(pzval));
}
if (EXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app), ZEND_STRL("library"))) != NULL)) {
if (IS_STRING == Z_TYPE_P(pzval)) {
if (*(Z_STRVAL_P(pzval) + Z_STRLEN_P(pzval)) == DEFAULT_SLASH) {
YAF_G(local_library) = zend_string_init(Z_STRVAL_P(pzval), Z_STRLEN_P(pzval) - 1, 0);
} else {
YAF_G(local_library) = zend_string_copy(Z_STR_P(pzval));
}
} else if (IS_ARRAY == Z_TYPE_P(pzval)) {
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("directory"))) != NULL && Z_TYPE_P(psval) == IS_STRING) {
if (*(Z_STRVAL_P(psval) + Z_STRLEN_P(psval)) == DEFAULT_SLASH) {
YAF_G(local_library) = zend_string_init(Z_STRVAL_P(psval), Z_STRLEN_P(psval) - 1, 0);
} else {
YAF_G(local_library) = zend_string_copy(Z_STR_P(psval));
}
}
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("namespace"))) != NULL && Z_TYPE_P(psval) == IS_STRING) {
uint i, len;
char *src = Z_STRVAL_P(psval);
if (Z_STRLEN_P(psval)) {
char *target = emalloc(Z_STRLEN_P(psval) + 1);
len = 0;
for(i=0; i<Z_STRLEN_P(psval); i++) {
if (src[i] == ',') {
target[len++] = DEFAULT_DIR_SEPARATOR;
} else if (src[i] != ' ') {
target[len++] = src[i];
}
}
target[len] = '\0';
yaf_loader_register_namespace_single(target, len);
efree(target);
}
}
}
}
if (UNEXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("view"))) != NULL && Z_TYPE_P(pzval) == IS_ARRAY)) {
if (UNEXPECTED((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("ext"))) != NULL && Z_TYPE_P(psval) == IS_STRING)) {
zend_string_release(YAF_G(view_ext));
YAF_G(view_ext) = zend_string_copy(Z_STR_P(psval));
}
}
if (UNEXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("baseUri"))) != NULL && Z_TYPE_P(pzval) == IS_STRING)) {
YAF_G(base_uri) = zend_string_copy(Z_STR_P(pzval));
}
if (UNEXPECTED((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("dispatcher"))) != NULL && Z_TYPE_P(pzval) == IS_ARRAY)) {
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("defaultModule"))) != NULL && Z_TYPE_P(psval) == IS_STRING) {
zend_string_release(YAF_G(default_module));
YAF_G(default_module) = zend_string_dup(Z_STR_P(psval), 0);
zend_str_tolower(ZSTR_VAL(YAF_G(default_module)), ZSTR_LEN(YAF_G(default_module)));
*ZSTR_VAL(YAF_G(default_module)) = toupper(*ZSTR_VAL(YAF_G(default_module)));
}
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("defaultController"))) != NULL && Z_TYPE_P(psval) == IS_STRING) {
zend_string_release(YAF_G(default_controller));
YAF_G(default_controller) = zend_string_dup(Z_STR_P(psval), 0);
zend_str_tolower(ZSTR_VAL(YAF_G(default_controller)), ZSTR_LEN(YAF_G(default_controller)));
*ZSTR_VAL(YAF_G(default_controller)) = toupper(*ZSTR_VAL(YAF_G(default_controller)));
}
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("defaultAction"))) != NULL && Z_TYPE_P(psval) == IS_STRING) {
zend_string_release(YAF_G(default_action));
YAF_G(default_action) = zend_string_tolower(Z_STR_P(psval));
}
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval), ZEND_STRL("throwException"))) != NULL) {
YAF_G(throw_exception) = zend_is_true(psval);
}
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval), ZEND_STRL("catchException"))) != NULL) {
YAF_G(catch_exception) = zend_is_true(psval);
}
if ((psval = zend_hash_str_find(Z_ARRVAL_P(pzval),
ZEND_STRL("defaultRoute"))) != NULL && Z_TYPE_P(psval) == IS_ARRAY) {
/* increase the refcount? */
YAF_G(default_route) = psval;
}
}
do {
char *ptrptr;
zval module;
array_init(&YAF_G(modules));
if ((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("modules"))) != NULL && Z_TYPE_P(pzval) == IS_STRING && Z_STRLEN_P(pzval)) {
char *seg, *modules;
modules = estrndup(Z_STRVAL_P(pzval), Z_STRLEN_P(pzval));
seg = php_strtok_r(modules, ",", &ptrptr);
while (seg) {
if (seg && strlen(seg)) {
ZVAL_STRINGL(&module, seg, strlen(seg));
*(Z_STRVAL(module)) = toupper(*Z_STRVAL(module));
zend_hash_next_index_insert(Z_ARRVAL(YAF_G(modules)), &module);
}
seg = php_strtok_r(NULL, ",", &ptrptr);
}
efree(modules);
} else {
ZVAL_STR_COPY(&module, YAF_G(default_module));
zend_hash_next_index_insert(Z_ARRVAL(YAF_G(modules)), &module);
}
} while (0);
if ((pzval = zend_hash_str_find(Z_ARRVAL_P(app),
ZEND_STRL("system"))) != NULL && Z_TYPE_P(pzval) == IS_ARRAY) {
zval *value;
char name[128];
zend_string *key;
size_t len;
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(pzval), key, value) {
zend_string *str, *val;
len = snprintf(name, sizeof(name), "%s.%s", "yaf", ZSTR_VAL(key));
if (len > sizeof(name) -1) {
len = sizeof(name) - 1;
}
str = zend_string_init(name, len, 0);
val = zval_get_string(value);
zend_alter_ini_entry(str, val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
zend_string_release(str);
zend_string_release(val);
} ZEND_HASH_FOREACH_END();
}
return SUCCESS;
}
/* }}} */
/** {{{ proto Yaf_Application::__construct(mixed $config, string $environ = YAF_G(environ_name))
*/
/*
* 一切构造从new Yaf_application()开始(在public/index.php中有new Yaf_application(),new首先调用构造函数)
*
* 所有类的实例化都从Yaf_application()的构造函数开始,做的事情:
* 1. 实例化 config;
* 2. 实例化 Request;
* 3. 实例化 Dispatcher;
* 4. 实例化 Loader
* 在Yaf_Application的构造函数中,调用其他类的构造函数时,又会继续创建它依赖的其它对象,
* 最终所有框架类都完成实例化,形成图1-1所示的关系图,然后就可以开始处理用户的请求了。
* phoenix
*/
PHP_METHOD(yaf_application, __construct) {
zval *config;
zval *section = NULL, zsection = {{0}};
yaf_config_t zconfig = {{0}};
yaf_request_t zrequest = {{0}};
yaf_dispatcher_t zdispatcher = {{0}};
yaf_application_t *app, *self;
yaf_loader_t *loader, zloader = {{0}};
#if PHP_YAF_DEBUG
php_error_docref(NULL, E_STRICT, "Yaf is running in debug mode");
#endif
//获取参数,把配置文件路径或者配置数组传递进来.
//section配置文件中的节点名字,默认product.
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|z", &config, §ion) == FAILURE) {
return;
}
//phoenix add
// #define ZEND_STRL(str) (str), (sizeof(str)-1)
//ZEND_STRL(_app)-->(_app),(sizeof(_app)-1)
//app = zend_read_static_property(yaf_applicatioin_ce,(_app),(sizeof(_app)-1),1);
//ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, zend_bool silent)
//获取yaf_application::_app变量值,默认为0
//phoenix end
app = zend_read_static_property(yaf_application_ce, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP), 1);
if (!ZVAL_IS_NULL(app)) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "Only one application can be initialized");
RETURN_FALSE;
}
//获取当前的对象,也就是php里面的$this
self = getThis();
if (!section || Z_TYPE_P(section) != IS_STRING || !Z_STRLEN_P(section)) {
ZVAL_STRING(&zsection, YAF_G(environ_name));
//实例化config,用于读取配置信息.phoenix add
(void)yaf_config_instance(&zconfig, config, &zsection);
zval_ptr_dtor(&zsection);
} else {
//获取config对象.phoenix add
(void)yaf_config_instance(&zconfig, config, section);
}
//yaf_application_parse_option:用于解析application的配置信息,将配置参数保存在全局变量zconfig中. phoenix add
if (UNEXPECTED(Z_TYPE(zconfig) != IS_OBJECT
|| yaf_application_parse_option(zend_read_property(yaf_config_ce,
&zconfig, ZEND_STRL(YAF_CONFIG_PROPERT_NAME), 1, NULL)) == FAILURE)) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "Initialization of application config failed");
zval_ptr_dtor(&zconfig);
RETURN_FALSE;
}
//2. 实例化Request,保存请求信息. phoenix add
(void)yaf_request_instance(&zrequest, YAF_G(base_uri));
if (UNEXPECTED(Z_TYPE(zrequest) != IS_OBJECT)) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "Initialization of request failed");
RETURN_FALSE;
}
//3.实例化Dispatcher,这个是yaf的核心控制类.主要在里面调用插件,路由,分发等 phoenix add
(void)yaf_dispatcher_instance(&zdispatcher);
if (UNEXPECTED(Z_TYPE(zdispatcher) != IS_OBJECT)) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "Instantiation of application dispatcher failed");
RETURN_FALSE;
}
//把request对象保存在zdispatcher对象属性中
yaf_dispatcher_set_request(&zdispatcher, &zrequest);
zval_ptr_dtor(&zrequest);
//把zconfig对象实例设置成Application的属性(保存在yaf_application对象属性中)
zend_update_property(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_CONFIG), &zconfig);
//把zdispatcher对象实例设置成Application的属性(保存在yaf_application对象属性中)
zend_update_property(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_DISPATCHER), &zdispatcher);
zval_ptr_dtor(&zdispatcher);
zval_ptr_dtor(&zconfig);
//4.创建loader,用于自动加载类
//是否指定本地类库地址.
if (YAF_G(local_library)) {
zend_string *global_library = strlen(YAF_G(global_library))?
zend_string_init(YAF_G(global_library), strlen(YAF_G(global_library)), 0) : NULL;
//获取一个loader加载类,并同时向内核注册一个自动加载器autoload,这里注册自动加载其也是用内核提供的spl_autoload_register
//之后在代码中new class()都会调用扩展中注册的自动加载器.
loader = yaf_loader_instance(&zloader, YAF_G(local_library), global_library);
if (global_library) {
zend_string_release(global_library);
}
} else {
zend_string *local_library, *global_library;
//获取一个默认的本地类库地址.
local_library = strpprintf(0, "%s%c%s", ZSTR_VAL(YAF_G(directory)), DEFAULT_SLASH, YAF_LIBRARY_DIRECTORY_NAME);
global_library = strlen(YAF_G(global_library))?
zend_string_init(YAF_G(global_library), strlen(YAF_G(global_library)), 0) : NULL;
//同上
loader = yaf_loader_ivar——这里的nstance(&zloader, local_library, global_library);
zend_string_release(local_library);
if (global_library) {
zend_string_release(global_library);
}
}
if (UNEXPECTED(Z_TYPE_P(loader) != IS_OBJECT)) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "Initialization of application auto loader failed");
RETURN_FALSE;
}
//赋值对象属性不
zend_update_property_bool(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_RUN), 0);
zend_update_property_string(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ENV), YAF_G(environ_name));
//这个modules一定会存在,默认是Index
if (Z_TYPE(YAF_G(modules)) == IS_ARRAY) {
zend_update_property(yaf_application_ce,
self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_MODULES), &YAF_G(modules));
} else {
zend_update_property_null(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_MODULES));
}
//yaf_application::_app赋值成当前self对象.
zend_update_static_property(yaf_application_ce, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP), self);
}
/* }}} */
/** {{{ proto public Yaf_Application::__desctruct(void)
*/
PHP_METHOD(yaf_application, __destruct) {
zend_update_static_property_null(yaf_application_ce, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP));
}
/* }}} */
/** {{{ proto private Yaf_Application::__sleep(void)
*/
PHP_METHOD(yaf_application, __sleep) {
}
/* }}} */
/** {{{ proto private Yaf_Application::__wakeup(void)
*/
PHP_METHOD(yaf_application, __wakeup) {
}
/* }}} */
/** {{{ proto private Yaf_Application::__clone(void)
*/
PHP_METHOD(yaf_application, __clone) {
}
/* }}} */
/** {{{ proto public Yaf_Application::run(void)
*/
/*
* 几乎所有MVC的Web框架处理一个请求都会走过3个流程:
* 0.request--->1.路由(根据request_uri确定处理请求的controller和action)
* --->2.处理请求(调用action定义的函数来处理用户请求)
* --->3.返回数据(action返回的数据输出给客户端)
*/
PHP_METHOD(yaf_application, run) {
zval *running;
yaf_dispatcher_t *dispatcher;
yaf_response_t *response, rresponse;
yaf_application_t *self = getThis();
//确保application的状态是非running的.phoenix
running = zend_read_property(yaf_application_ce, self,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_RUN), 1, NULL);
if (UNEXPECTED(IS_TRUE == Z_TYPE_P(running))) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED, "An application instance already run");
RETURN_TRUE;
}
//把application的状态置为running.phoenix
ZVAL_BOOL(running, 1);
/* zend_update_property(yaf_application_ce, self,
* ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_RUN), running); */
//调用dispatcher 来处理这次请求.yaf_dispatcher_dispatch是处理请求的中心函数,它调用路由器完成路由,然后根据路由结果,
//找到对应的action执行,最后把结果发送给客户端.phoenix
dispatcher = zend_read_property(yaf_application_ce, self,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_DISPATCHER), 1, NULL);
ZVAL_NULL(&rresponse);
if ((response = yaf_dispatcher_dispatch(dispatcher, &rresponse))) {
RETURN_ZVAL(response, 1, 1);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Application::execute(callback $func)
* We can not call to zif_call_user_func on windows, since it was not declared with dllexport
*/
PHP_METHOD(yaf_application, execute) {
zval retval;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f*", &fci, &fci_cache, &fci.params, &fci.param_count) == FAILURE) {
return;
}
fci.retval = &retval;
if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
ZVAL_COPY_VALUE(return_value, &retval);
} else {
RETURN_FALSE;
}
}
/* }}} */
/** {{{ proto public Yaf_Application::app(void)
*/
PHP_METHOD(yaf_application, app) {
yaf_application_t *app = zend_read_static_property(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP), 1);
RETURN_ZVAL(app, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::getConfig(void)
*/
PHP_METHOD(yaf_application, getConfig) {
yaf_config_t *config = zend_read_property(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_CONFIG), 1, NULL);
RETURN_ZVAL(config, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::getDispatcher(void)
*/
PHP_METHOD(yaf_application, getDispatcher) {
yaf_dispatcher_t *dispatcher = zend_read_property(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_DISPATCHER), 1, NULL);
RETURN_ZVAL(dispatcher, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::getModules(void)
*/
PHP_METHOD(yaf_application, getModules) {
zval *modules = zend_read_property(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_MODULES), 1, NULL);
RETURN_ZVAL(modules, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::environ(void)
*/
PHP_METHOD(yaf_application, environ) {
zval *env = zend_read_property(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ENV), 1, NULL);
ZEND_ASSERT(Z_TYPE_P(env) == IS_STRING);
RETURN_STR(zend_string_copy(Z_STR_P(env)));
}
/* }}} */
/** {{{ proto public Yaf_Application::bootstrap(void)
*/
PHP_METHOD(yaf_application, bootstrap) {
zend_string *bootstrap_path;
uint retval = 1;
zend_class_entry *ce;
yaf_application_t *self = getThis();
//判断Bootstrap类是否存在,默认第一次肯定不存在,因为还没有加载注册进来 phoenix add
if (!(ce = zend_hash_str_find_ptr(EG(class_table),
YAF_DEFAULT_BOOTSTRAP_LOWER, sizeof(YAF_DEFAULT_BOOTSTRAP_LOWER) - 1))) {
//判断是否指定了bootstrap文件路径.
if (YAF_G(bootstrap)) {
bootstrap_path = zend_string_copy(YAF_G(bootstrap));
} else {
//没有指定,则使用默认路径
bootstrap_path = strpprintf(0, "%s%c%s.%s",
ZSTR_VAL(YAF_G(directory)), DEFAULT_SLASH, YAF_DEFAULT_BOOTSTRAP, ZSTR_VAL(YAF_G(ext)));
}
//导入bootstrap文件,内核如果发现是类,则会自动注册上.
if (!yaf_loader_import(bootstrap_path, 0)) {
php_error_docref(NULL, E_WARNING, "Couldn't find bootstrap file %s", ZSTR_VAL(bootstrap_path));
retval = 0;
} else if (UNEXPECTED((ce = zend_hash_str_find_ptr(EG(class_table),
YAF_DEFAULT_BOOTSTRAP_LOWER, sizeof(YAF_DEFAULT_BOOTSTRAP_LOWER) - 1)) == NULL)) {//获取bootstrap类
php_error_docref(NULL, E_WARNING, "Couldn't find class %s in %s", YAF_DEFAULT_BOOTSTRAP, ZSTR_VAL(bootstrap_path));
retval = 0;
} else if (UNEXPECTED(!instanceof_function(ce, yaf_bootstrap_ce))) {//判断类是否继承Yaf_Bootstrap_Abstract
php_error_docref(NULL, E_WARNING,
"Expect a %s instance, %s give", ZSTR_VAL(yaf_bootstrap_ce->name), ZSTR_VAL(ce->name));
retval = 0;
}
zend_string_release(bootstrap_path);
}
if (UNEXPECTED(!retval)) {
RETURN_FALSE;
} else {
zend_string *func;
zval bootstrap;
yaf_dispatcher_t *dispatcher;
object_init_ex(&bootstrap, ce);
dispatcher = zend_read_property(yaf_application_ce,
self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_DISPATCHER), 1, NULL);
ZEND_HASH_FOREACH_STR_KEY(&(ce->function_table), func) {
/* cann't use ZEND_STRL in strncasecmp, it cause a compile failed in VS2009 */
if (strncasecmp(ZSTR_VAL(func), YAF_BOOTSTRAP_INITFUNC_PREFIX, sizeof(YAF_BOOTSTRAP_INITFUNC_PREFIX)-1)) {
continue;
}
zend_call_method(&bootstrap, ce, NULL, ZSTR_VAL(func), ZSTR_LEN(func), NULL, 1, dispatcher, NULL);
/** an uncaught exception threw in function call */
if (UNEXPECTED(EG(exception))) {
zval_ptr_dtor(&bootstrap);
RETURN_FALSE;
}
} ZEND_HASH_FOREACH_END();
zval_ptr_dtor(&bootstrap);
}
RETURN_ZVAL(self, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::getLastErrorNo(void)
*/
PHP_METHOD(yaf_application, getLastErrorNo) {
zval *errcode = zend_read_property(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRNO), 1, NULL);
ZEND_ASSERT(Z_TYPE_P(errcode) == IS_LONG);
RETURN_LONG(Z_LVAL_P(errcode));
}
/* }}} */
/** {{{ proto public Yaf_Application::getLastErrorMsg(void)
*/
PHP_METHOD(yaf_application, getLastErrorMsg) {
zval *errmsg = zend_read_property(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRMSG), 1, NULL);
ZEND_ASSERT(Z_TYPE_P(errmsg) == IS_STRING);
RETURN_STR(zend_string_copy(Z_STR_P(errmsg)));
}
/* }}} */
/** {{{ proto public Yaf_Application::clearLastError(void)
*/
PHP_METHOD(yaf_application, clearLastError) {
zend_update_property_long(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRNO), 0);
zend_update_property_string(yaf_application_ce,
getThis(), ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRMSG), "");
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::setAppDirectory(string $directory)
*/
PHP_METHOD(yaf_application, setAppDirectory) {
zend_string *directory;
yaf_application_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &directory) == FAILURE) {
return;
}
if (ZSTR_LEN(directory) == 0 || !IS_ABSOLUTE_PATH(ZSTR_VAL(directory), ZSTR_LEN(directory))) {
RETURN_FALSE;
}
zend_string_release(YAF_G(directory));
YAF_G(directory) = zend_string_copy(directory);
RETURN_ZVAL(self, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Application::getAppDirectory(void)
*/
PHP_METHOD(yaf_application, getAppDirectory) {
RETURN_STR(zend_string_copy(YAF_G(directory)));
}
/* }}} */
/** {{{ yaf_application_methods
*/
// phoenix add start
/**
typedef struct _zend_function_entry {
const char *fname;
void (*handler)(INTERNAL_FUNCTION_PARAMETERS);
const struct _zend_internal_arg_info *arg_info;
uint32_t num_args;
uint32_t flags;
} zend_function_entry;
为什么yaf中用了这么多这么多的宏啊啊啊啊啊.
*/
//phoenix add end
zend_function_entry yaf_application_methods[] = {
PHP_ME(yaf_application, __construct, yaf_application_construct_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(yaf_application, run, yaf_application_run_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, execute, yaf_application_execute_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, app, yaf_application_app_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
YAF_ME(yaf_application_environ, "environ", yaf_application_environ_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, bootstrap, yaf_application_bootstrap_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getConfig, yaf_application_getconfig_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getModules, yaf_application_getmodule_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getDispatcher, yaf_application_getdispatch_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, setAppDirectory, yaf_application_setappdir_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getAppDirectory, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getLastErrorNo, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, getLastErrorMsg, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, clearLastError, yaf_application_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_application, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(yaf_application, __clone, NULL, ZEND_ACC_PRIVATE | ZEND_ACC_CLONE)
PHP_ME(yaf_application, __sleep, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_application, __wakeup, NULL, ZEND_ACC_PRIVATE)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ YAF_STARTUP_FUNCTION
*/
//phoenix add
/*
#define YAF_INIT_CLASS_ENTRY(ce, name, name_ns, methods) \
if(YAF_G(use_namespace)) { \
INIT_CLASS_ENTRY(ce, name_ns, methods); \
} else { \
INIT_CLASS_ENTRY(ce, name, methods); \
}
*/
//phoenix end
YAF_STARTUP_FUNCTION(application) {
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Application", "Yaf\\Application", yaf_application_methods);
yaf_application_ce = zend_register_internal_class_ex(&ce, NULL);
yaf_application_ce->ce_flags |= ZEND_ACC_FINAL;
zend_declare_property_null(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_CONFIG), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_DISPATCHER), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP), ZEND_ACC_STATIC | ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_MODULES), ZEND_ACC_PROTECTED);
zend_declare_property_bool(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_RUN), 0, ZEND_ACC_PROTECTED);
zend_declare_property_string(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ENV), YAF_G(environ_name), ZEND_ACC_PROTECTED);
zend_declare_property_long(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRNO), 0, ZEND_ACC_PROTECTED);
zend_declare_property_string(yaf_application_ce,
ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRMSG), "", ZEND_ACC_PROTECTED);
return SUCCESS;
}
/* }}} */
/*
* 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
*/