-
Notifications
You must be signed in to change notification settings - Fork 30
/
yaf_dispatcher.c
executable file
·1539 lines (1356 loc) · 58.8 KB
/
yaf_dispatcher.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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| 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]> |
+----------------------------------------------------------------------+
*/
/* $Id: yaf_dispatcher.c 328978 2013-01-04 10:41:50Z laruence $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "main/SAPI.h"
#include "Zend/zend_API.h"
#include "Zend/zend_interfaces.h"
#include "php_yaf.h"
#include "yaf_namespace.h"
#include "yaf_dispatcher.h"
#include "yaf_controller.h"
#include "yaf_action.h"
#include "yaf_application.h"
#include "yaf_view.h"
#include "yaf_response.h"
#include "yaf_loader.h"
#include "yaf_router.h"
#include "yaf_request.h"
#include "yaf_config.h"
#include "yaf_plugin.h"
#include "yaf_exception.h"
zend_class_entry * yaf_dispatcher_ce;
/** {{{ ARG_INFO
*/
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_void_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_dispatch_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, request)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_seterrhdler_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(0, error_types)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_flush_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_regplugin_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, plugin)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setrequest_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, request)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_throwex_arginfo, 0, 0, 0)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_catchex_arginfo, 0, 0, 0)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_autorender_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_returnresp_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_initview_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, templates_dir)
ZEND_ARG_ARRAY_INFO(0, options, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setview_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, view)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setctrl_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, controller)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setmodule_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, module)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setaction_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, action)
ZEND_END_ARG_INFO()
/* }}} */
/** {{{ yaf_dispatcher_t * yaf_dispatcher_instance(zval *this_ptr TSRMLS_DC)
* 类的__construct函数,单例模式
*/
yaf_dispatcher_t * yaf_dispatcher_instance(yaf_dispatcher_t *this_ptr TSRMLS_DC) {
zval *plugins;
yaf_router_t *router;
yaf_dispatcher_t *instance;
instance = zend_read_static_property(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_INSTANCE), 1 TSRMLS_CC);
if (IS_OBJECT == Z_TYPE_P(instance)
&& instanceof_function(Z_OBJCE_P(instance), yaf_dispatcher_ce TSRMLS_CC)) {
return instance;
}
if (this_ptr) {
instance = this_ptr;
return this_ptr;
} else {
instance = NULL;
MAKE_STD_ZVAL(instance);
object_init_ex(instance, yaf_dispatcher_ce);
}
/** unecessary yet
MAKE_STD_ZVAL(args);
array_init(args);
yaf_update_property(instance, YAF_DISPATCHER_PROPERTY_NAME_ARGS, args);
*/
MAKE_STD_ZVAL(plugins);
array_init(plugins);
zend_update_property(yaf_dispatcher_ce, instance, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_PLUGINS), plugins TSRMLS_CC);
zval_ptr_dtor(&plugins);
router = yaf_router_instance(NULL TSRMLS_CC);
zend_update_property(yaf_dispatcher_ce, instance, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ROUTER), router TSRMLS_CC);
zend_update_property_string(yaf_dispatcher_ce, instance, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), YAF_G(default_module) TSRMLS_CC);
zend_update_property_string(yaf_dispatcher_ce, instance, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), YAF_G(default_controller) TSRMLS_CC);
zend_update_property_string(yaf_dispatcher_ce, instance, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), YAF_G(default_action) TSRMLS_CC);
zend_update_static_property(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_INSTANCE), instance TSRMLS_CC);
zval_ptr_dtor(&router);
return instance;
}
/* }}} */
/** {{{ static void yaf_dispatcher_get_call_parameters(zend_class_entry *request_ce, yaf_request_t *request, zend_function *fptr, zval ****params, uint *count TSRMLS_DC)
* 获得当前method需要获得的参数
*/
static void yaf_dispatcher_get_call_parameters(zend_class_entry *request_ce, yaf_request_t *request, zend_function *fptr, zval ****params, uint *count TSRMLS_DC) {
zval *args, **arg;
zend_arg_info *arg_info;
uint current;
HashTable *params_ht;
/* $args = Yaf_Request_Abstract::$params */
args = zend_read_property(request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_PARAMS), 1 TSRMLS_CC);
params_ht = Z_ARRVAL_P(args);
arg_info = fptr->common.arg_info;
*params = safe_emalloc(sizeof(zval **), fptr->common.num_args, 0);
for (current = 0;current < fptr->common.num_args; current++, arg_info++) {
if (zend_hash_find(params_ht, arg_info->name, arg_info->name_len + 1, (void **)&arg) == SUCCESS) {
(*params)[current] = arg;
(*count)++;
} else {
char *key;
uint keylen;
ulong idx, llen;
arg = NULL;
llen = arg_info->name_len + 1;
/* since we need search ignoring case, can't use zend_hash_find */
for(zend_hash_internal_pointer_reset(params_ht);
zend_hash_has_more_elements(params_ht) == SUCCESS;
zend_hash_move_forward(params_ht)) {
if (zend_hash_get_current_key_ex(params_ht, &key, &keylen, &idx, 0, NULL) == HASH_KEY_IS_STRING) {
if (keylen == llen && !strncasecmp(key, arg_info->name, keylen)) {
if (zend_hash_get_current_data(params_ht, (void**)&arg) == SUCCESS) {
/* return when we find first match, there is a trap
* when multi different parameters in different case presenting in params_ht
* only the first take affect
*/
(*params)[current] = arg;
(*count)++;
break;
}
}
}
}
if (NULL == arg) {
break;
}
}
}
}
/* }}} */
/** {{{ static yaf_view_t * yaf_dispatcher_init_view(yaf_dispatcher_t *dispatcher, zval *tpl_dir, zval *options TSRMLS_DC)
* 在dispatcher中实例化视图引擎
*/
static yaf_view_t * yaf_dispatcher_init_view(yaf_dispatcher_t *dispatcher, zval *tpl_dir, zval *options TSRMLS_DC) {
/* $view = $this->_view */
yaf_view_t *view = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_VIEW), 1 TSRMLS_CC);
if (view && IS_OBJECT == Z_TYPE_P(view)
&& instanceof_function(Z_OBJCE_P(view), yaf_view_interface_ce TSRMLS_CC)) {
/* 已经初始化视图引擎则返回试图引擎实例化后的对象 */
return view;
}
/* 实例化视图引擎 */
view = yaf_view_instance(NULL, tpl_dir, options TSRMLS_CC);
if (!view) {
return NULL;
}
/* $this->_view = $view */
zend_update_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_VIEW), view TSRMLS_CC);
zval_ptr_dtor(&view);
return view;
}
/* }}} */
/** {{{ static inline void yaf_dispatcher_fix_default(yaf_dispatcher_t *dispatcher, yaf_request_t *request TSRMLS_DC)
* 检测路由的module、controller、action名称是否合法,不合法的用默认代替,合法的进行需要的响应的转换工作
*/
static inline void yaf_dispatcher_fix_default(yaf_dispatcher_t *dispatcher, yaf_request_t *request TSRMLS_DC) {
zval *module, *controller, *action;
/* $module = Yaf_Request::$module */
module = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
/* $action = Yaf_Request::$action */
action = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), 1 TSRMLS_CC);
/* $controller = Yaf_Request::$controller */
controller = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), 1 TSRMLS_CC);
/* 1.设置路由的module */
if (!module || Z_TYPE_P(module) != IS_STRING || !Z_STRLEN_P(module)) { /* module名称非法 */
/* $default_module = $this->_default_module */
zval *default_module = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
/* Yaf_Request::$module = $default_module */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), default_module TSRMLS_CC);
} else { /* module名称合法 */
/* tolower */
char *p = zend_str_tolower_dup(Z_STRVAL_P(module), Z_STRLEN_P(module));
/* toupper */
*p = toupper(*p);
/* Yaf_Request::$module = $p */
zend_update_property_stringl(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), p, Z_STRLEN_P(module) TSRMLS_CC);
efree(p);
}
/* 2.设置路由的controller */
if (!controller || Z_TYPE_P(controller) != IS_STRING || !Z_STRLEN_P(controller)) { /* controller名称非法 */
/* $default_controller = $this->_default_controller */
zval *default_controller = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), 1 TSRMLS_CC);
/* Yaf_Request::$controller = $default_controller */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), default_controller TSRMLS_CC);
} else { /* controller名称合法 */
char *q, *p = zend_str_tolower_dup(Z_STRVAL_P(controller), Z_STRLEN_P(controller));
/**
* upper controller name
* eg: Index_sub -> Index_Sub
*/
q = p;
*q = toupper(*q);
while (*q != '\0') {
if (*q == '_'
#ifdef YAF_HAVE_NAMESPACE
|| *q == '\\'
#endif
) {
if (*(q+1) != '\0') {
*(q+1) = toupper(*(q+1));
q++;
}
}
q++;
}
/* Yaf_Request::$controller = $p */
zend_update_property_stringl(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), p, Z_STRLEN_P(controller) TSRMLS_CC);
efree(p);
}
/* 3.设置路由的action */
if (!action || Z_TYPE_P(action) != IS_STRING || !Z_STRLEN_P(action)) { //action名称非法
/* $default_action = $this->_default_action */
zval *default_action = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), 1 TSRMLS_CC);
/* Yaf_Request::$action = $default_action */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), default_action TSRMLS_CC);
} else { //action名称合法
/* Yaf_Request::$action = strtolower($action) */
char *p = zend_str_tolower_dup(Z_STRVAL_P(action), Z_STRLEN_P(action));
zend_update_property_stringl(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), p, Z_STRLEN_P(action) TSRMLS_CC);
efree(p);
}
}
/* }}} */
/** {{{int yaf_dispatcher_set_request(yaf_dispatcher_t *dispatcher, yaf_request_t *request TSRMLS_DC)
*/
int yaf_dispatcher_set_request(yaf_dispatcher_t *dispatcher, yaf_request_t *request TSRMLS_DC) {
if (request) {
/* $this->_request = $request */
zend_update_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), request TSRMLS_CC);
return 1;
}
return 0;
}
/* }}} */
/** {{{ zend_class_entry * yaf_dispatcher_get_controller(char *app_dir, char *module, char *controller, int len, int def_module TSRMLS_DC)
* 拼装得到controller的文件路径,然后注册类
*/
zend_class_entry * yaf_dispatcher_get_controller(char *app_dir, char *module, char *controller, int len, int def_module TSRMLS_DC) {
char *directory = NULL;
int directory_len = 0;
/* 分为默认module和不是默认module的情况来拼装controllers文件夹的路径 */
if (def_module) {
/* /home/www/sample.com/application/controllers */
directory_len = spprintf(&directory, 0, "%s%c%s", app_dir, DEFAULT_SLASH, YAF_CONTROLLER_DIRECTORY_NAME);
} else {
/* /home/www/sample.com/application/modules/module1/controllers */
directory_len = spprintf(&directory, 0, "%s%c%s%c%s%c%s", app_dir, DEFAULT_SLASH,
YAF_MODULE_DIRECTORY_NAME, DEFAULT_SLASH, module, DEFAULT_SLASH, YAF_CONTROLLER_DIRECTORY_NAME);
}
if (directory_len) {
char *class = NULL;
char *class_lowercase = NULL;
int class_len = 0;
zend_class_entry **ce = NULL;
/* 根据controller名以及框架的前后缀还有类名分隔符来拼装类名 */
if (YAF_G(name_suffix)) {
class_len = spprintf(&class, 0, "%s%s%s", controller, YAF_G(name_separator), "Controller");
} else {
class_len = spprintf(&class, 0, "%s%s%s", "Controller", YAF_G(name_separator), controller);
}
/**
* $class_lowercase = strtolower($class)
* 在Zend中类名和其成员函数是小写的
*/
class_lowercase = zend_str_tolower_dup(class, class_len);
/* EG(class_table)是一个全局变量, 所有的zend_class_entry定义都注册到它里面 */
if (zend_hash_find(EG(class_table), class_lowercase, class_len + 1, (void *)&ce) != SUCCESS) { /* 类还没有注册,执行注册操作 */
if (!yaf_internal_autoload(controller, len, &directory TSRMLS_CC)) { /* 接收文件名和文件所在文件夹路径,拼装出完整文件的路径,然后对文件是否存在的判断,包含,解析以及执行过程 */
yaf_trigger_error(YAF_ERR_NOTFOUND_CONTROLLER TSRMLS_CC, "Failed opening controller script %s: %s", directory, strerror(errno));
efree(class);
efree(class_lowercase);
efree(directory);
return NULL;
} else if (zend_hash_find(EG(class_table), class_lowercase, class_len + 1, (void **) &ce) != SUCCESS) { /* 注册成功后,EG(class_table)中还是不存在 */
yaf_trigger_error(YAF_ERR_AUTOLOAD_FAILED TSRMLS_CC, "Could not find class %s in controller script %s", class, directory);
efree(class);
efree(class_lowercase);
efree(directory);
return 0;
} else if (!instanceof_function(*ce, yaf_controller_ce TSRMLS_CC)) { /* 注册成功了,但是注册的类不是继承自Yaf_Controller_Abstract */
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Controller must be an instance of %s", yaf_controller_ce->name);
efree(class);
efree(class_lowercase);
efree(directory);
return 0;
}
}
efree(class);
efree(class_lowercase);
efree(directory);
return *ce;
}
return NULL;
}
/* }}} */
/** {{{ zend_class_entry * yaf_dispatcher_get_action(char *app_dir, yaf_controller_t *controller, char *module, int def_module, char *action, int len TSRMLS_DC)
* 拼装得到action的文件路径,然后进行class和method的注册
*/
zend_class_entry * yaf_dispatcher_get_action(char *app_dir, yaf_controller_t *controller, char *module, int def_module, char *action, int len TSRMLS_DC) {
zval **ppaction, *actions_map;
/* $actions_map = Yaf_Controller_Abstract::$actions */
actions_map = zend_read_property(Z_OBJCE_P(controller), controller, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_ACTIONS), 1 TSRMLS_CC);
if (IS_ARRAY == Z_TYPE_P(actions_map)) { //数组
if (zend_hash_find(Z_ARRVAL_P(actions_map), action, len + 1, (void **)&ppaction) == SUCCESS) { //action查找成功
char *action_path;
uint action_path_len;
/* 拼装action路径 */
action_path_len = spprintf(&action_path, 0, "%s%c%s", app_dir, DEFAULT_SLASH, Z_STRVAL_PP(ppaction));
/* 加载文件并编译执行 */
if (yaf_loader_import(action_path, action_path_len, 0 TSRMLS_CC)) {
zend_class_entry **ce;
char *class, *class_lowercase;
uint class_len;
char *action_upper = estrndup(action, len);
*(action_upper) = toupper(*action_upper);
if (YAF_G(name_suffix)) { //Action后缀
class_len = spprintf(&class, 0, "%s%s%s", action_upper, YAF_G(name_separator), "Action");
} else { //Action前缀
class_len = spprintf(&class, 0, "%s%s%s", "Action", YAF_G(name_separator), action_upper);
}
class_lowercase = zend_str_tolower_dup(class, class_len);
/* 检验controller类注册 */
if (zend_hash_find(EG(class_table), class_lowercase, class_len + 1, (void **) &ce) == SUCCESS) { //查找成功,类已经注册,执行完毕,释放资源
efree(action_path);
efree(action_upper);
efree(class_lowercase);
if (instanceof_function(*ce, yaf_action_ce TSRMLS_CC)) {
efree(class);
return *ce;
} else {
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Action %s must extends from %s", class, yaf_action_ce->name);
efree(class);
}
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION TSRMLS_CC, "Could not find action %s in %s", class, action_path);
}
efree(action_path);
efree(action_upper);
efree(class);
efree(class_lowercase);
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION TSRMLS_CC, "Failed opening action script %s: %s", action_path, strerror(errno));
efree(action_path);
}
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION TSRMLS_CC, "There is no method %s%s in %s::$%s",
action, "Action", Z_OBJCE_P(controller)->name, YAF_CONTROLLER_PROPERTY_NAME_ACTIONS);
}
} else //不是数组,正常情况下为数组值
/* {{{ This only effects internally */
if (YAF_G(st_compatible)) { //开启兼容模式
char *directory, *class, *class_lowercase, *p;
uint class_len;
zend_class_entry **ce;
char *action_upper = estrndup(action, len);
/**
* upper Action Name
* eg: Index_sub -> Index_Sub
*/
p = action_upper;
*(p) = toupper(*p);
while (*p != '\0') {
if (*p == '_'
#ifdef YAF_HAVE_NAMESPACE
|| *p == '\\'
#endif
) {
if (*(p+1) != '\0') {
*(p+1) = toupper(*(p+1));
p++;
}
}
p++;
}
/* 拼装module路径 */
if (def_module) {
spprintf(&directory, 0, "%s%c%s", app_dir, DEFAULT_SLASH, "actions");
} else {
spprintf(&directory, 0, "%s%c%s%c%s%c%s", app_dir, DEFAULT_SLASH,
"modules", DEFAULT_SLASH, module, DEFAULT_SLASH, "actions");
}
/* 通过前后缀拼装action完整名称 */
if (YAF_G(name_suffix)) {
class_len = spprintf(&class, 0, "%s%s%s", action_upper, YAF_G(name_separator), "Action");
} else {
class_len = spprintf(&class, 0, "%s%s%s", "Action", YAF_G(name_separator), action_upper);
}
/* 转换小写 */
class_lowercase = zend_str_tolower_dup(class, class_len);
if (zend_hash_find(EG(class_table), class_lowercase, class_len + 1, (void *)&ce) != SUCCESS) { //注册类中查找类名
if (!yaf_internal_autoload(action_upper, len, &directory TSRMLS_CC)) { //加载承载action的文件失败
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION TSRMLS_CC, "Failed opening action script %s: %s", directory, strerror(errno));
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return NULL;
} else if (zend_hash_find(EG(class_table), class_lowercase, class_len + 1, (void **) &ce) != SUCCESS) { //成功加载,但是还是在注册哈希表中没找到
yaf_trigger_error(YAF_ERR_AUTOLOAD_FAILED TSRMLS_CC, "Could find class %s in action script %s", class, directory);
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return NULL;
} else if (!instanceof_function(*ce, yaf_action_ce TSRMLS_CC)) { /* 不是继承自Yaf_Action_Abstract */
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Action must be an instance of %s", yaf_action_ce->name);
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return NULL;
}
}
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return *ce;
} else
/* }}} */
{
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION TSRMLS_CC, "There is no method %s%s in %s", action, "Action", Z_OBJCE_P(controller)->name);
}
return NULL;
}
/* }}} */
/** {{{ int yaf_dispatcher_handle(yaf_dispatcher_t *dispatcher, yaf_request_t *request, yaf_response_t *response, yaf_view_t *view TSRMLS_DC)
* 进行controller文件的加载、然后执行action,最后根据配置来进行模板渲染以及输出
*/
int yaf_dispatcher_handle(yaf_dispatcher_t *dispatcher, yaf_request_t *request, yaf_response_t *response, yaf_view_t *view TSRMLS_DC) {
zend_class_entry *request_ce;
char *app_dir = YAF_G(directory);
request_ce = Z_OBJCE_P(request);
/* Yaf_Request::dispatched = 1 */
yaf_request_set_dispatched(request, 1 TSRMLS_CC);
if (!app_dir) { /* 没设置application.directory */
yaf_trigger_error(YAF_ERR_STARTUP_FAILED TSRMLS_CC, "%s requires %s(which set the application.directory) to be initialized first",
yaf_dispatcher_ce->name, yaf_application_ce->name);
return 0;
} else {
int is_def_module = 0;
/* int is_def_ctr = 0; */
zval *module, *controller, *dmodule, *dcontroller, *instantly_flush;
zend_class_entry *ce;
yaf_controller_t *executor;
zend_function *fptr;
/**
* $module = Yaf_Request::$module
* $controller = Yaf_Request::$controller
*/
module = zend_read_property(request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
controller = zend_read_property(request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), 1 TSRMLS_CC);
/**
* $dmodule = $this->_default_module
* $dcontroller = $this->_default_controller
*/
dmodule = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
dcontroller = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), 1 TSRMLS_CC);
/* 从字符串角度和已经注册了的module来检测当前的module是否合法 */
if (Z_TYPE_P(module) != IS_STRING
|| !Z_STRLEN_P(module)) {
yaf_trigger_error(YAF_ERR_DISPATCH_FAILED TSRMLS_CC, "Unexcepted a empty module name");
return 0;
} else if (!yaf_application_is_module_name(Z_STRVAL_P(module), Z_STRLEN_P(module) TSRMLS_CC)) {
yaf_trigger_error(YAF_ERR_NOTFOUND_MODULE TSRMLS_CC, "There is no module %s", Z_STRVAL_P(module));
return 0;
}
/* 以合法字符串方式来检验controller */
if (Z_TYPE_P(controller) != IS_STRING
|| !Z_STRLEN_P(controller)) {
yaf_trigger_error(YAF_ERR_DISPATCH_FAILED TSRMLS_CC, "Unexcepted a empty controller name");
return 0;
}
/* 检测传进来的module名是否为默认的module名称 */
if(strncasecmp(Z_STRVAL_P(dmodule), Z_STRVAL_P(module), Z_STRLEN_P(module)) == 0) {
is_def_module = 1;
}
/* if (strncasecmp(Z_STRVAL_P(dcontroller), Z_STRVAL_P(controller), Z_STRLEN_P(controller)) == 0) {
is_def_ctr = 1;
} */
/* 注册controller */
ce = yaf_dispatcher_get_controller(app_dir, Z_STRVAL_P(module), Z_STRVAL_P(controller), Z_STRLEN_P(controller), is_def_module TSRMLS_CC);
if (!ce) {
return 0;
} else { /* controller注册成功 */
zval *action, *render, *ret = NULL;
char *action_lower, *func_name, *view_dir;
uint func_name_len;
yaf_controller_t *icontroller;
/* $icontroller = new Controller() */
MAKE_STD_ZVAL(icontroller);
object_init_ex(icontroller, ce);
/* cause controller's constructor is a final method, so it must be a internal function
do {
zend_function *constructor = NULL;
constructor = Z_OBJ_HT_P(exec_ctr)->get_constructor(exec_ctr TSRMLS_CC);
if (constructor != NULL) {
if (zend_call_method_with_2_params(&exec_ctr, *ce
, &constructor, NULL, &ret, request, response) == NULL) {
yaf_trigger_error(YAF_ERR_CALL_FAILED, "function call for %s::__construct failed", (*ce)->name);
return 0;
}
}
} while(0);
*/
/* Yaf_Controller_Abstract::__construct */
yaf_controller_construct(ce, icontroller, request, response, view, NULL TSRMLS_CC);
if (EG(exception)) { //异常处理
zval_ptr_dtor(&icontroller);
return 0;
}
/* view template directory for application, please notice that view engine's directory has high priority */
/* 拼装模板文件夹的路径 */
if (is_def_module) {
/* /home/www/sample.com/application/views */
//spprintf(&view_dir, 0, "%s/%s", app_dir ,"views");
spprintf(&view_dir, 0, "%s%c%s", app_dir , DEFAULT_SLASH, "views");
} else {
/* /home/www/sample.com/application/ modules / module1 /views */
//spprintf(&view_dir, 0, "%s/%s/%s/%s", app_dir, "modules", Z_STRVAL_P(module), "views");
spprintf(&view_dir, 0, "%s%c%s%c%s%c%s", app_dir, DEFAULT_SLASH, "modules", DEFAULT_SLASH, Z_STRVAL_P(module), DEFAULT_SLASH, "views");
}
if (YAF_G(view_directory)) {
efree(YAF_G(view_directory));
}
YAF_G(view_directory) = view_dir;
/* $icontroller->_name = Yaf_Request::$controller */
zend_update_property(ce, icontroller, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_NAME), controller TSRMLS_CC);
/* $action = Yaf_Request::$action */
action = zend_read_property(request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), 1 TSRMLS_CC);
/* $action_lower = strtolower($action) */
action_lower = zend_str_tolower_dup(Z_STRVAL_P(action), Z_STRLEN_P(action));
/* because the action might call the forward to override the old action */
Z_ADDREF_P(action);
/* action名称也是全小写 */
func_name_len = spprintf(&func_name, 0, "%s%s", action_lower, "action");
efree(action_lower);
if (zend_hash_find(&((ce)->function_table), func_name, func_name_len + 1, (void **)&fptr) == SUCCESS) {
/* 在注册的controller的函数表中查找成功 */
uint count = 0;
zval ***call_args = NULL;
ret = NULL;
executor = icontroller;
if (fptr->common.num_args) {
zval *method_name;
/* 获取当前method的参数和参数个数 */
yaf_dispatcher_get_call_parameters(request_ce, request, fptr, &call_args, &count TSRMLS_CC);
MAKE_STD_ZVAL(method_name);
ZVAL_STRINGL(method_name, func_name, func_name_len, 0);
/* 调用method并传参 */
call_user_function_ex(&(ce)->function_table, &icontroller, method_name, &ret, count, call_args, 1, NULL TSRMLS_CC);
efree(method_name);
efree(call_args);
} else {
/* 没有传递参数,直接调用method */
zend_call_method(&icontroller, ce, NULL, func_name, func_name_len, &ret, 0, NULL, NULL TSRMLS_CC);
}
efree(func_name);
if (!ret) {
zval_ptr_dtor(&action);
zval_ptr_dtor(&icontroller);
return 0;
}
/* Why:返回值为false的情况下返回1呢 */
if ((Z_TYPE_P(ret) == IS_BOOL
&& !Z_BVAL_P(ret))) {
/* no auto-render */
zval_ptr_dtor(&ret);
zval_ptr_dtor(&action);
zval_ptr_dtor(&icontroller);
return 1;
}
zval_ptr_dtor(&ret);
} else if ((ce = yaf_dispatcher_get_action(app_dir, icontroller,
Z_STRVAL_P(module), is_def_module, Z_STRVAL_P(action), Z_STRLEN_P(action) TSRMLS_CC)) //在已经注册的里面查找失败后重新进行加载
&& (zend_hash_find(&(ce)->function_table, YAF_ACTION_EXECUTOR_NAME,
sizeof(YAF_ACTION_EXECUTOR_NAME), (void **)&fptr) == SUCCESS)) {
zval ***call_args;
yaf_action_t *iaction;
uint count = 0;
efree(func_name);
/* $iaction = new Yaf_Action_Abstract() */
MAKE_STD_ZVAL(iaction);
object_init_ex(iaction, ce);
/* Yaf_Action_Abstract::__construct() */
yaf_controller_construct(ce, iaction, request, response, view, NULL TSRMLS_CC);
executor = iaction;
/* $iaction->_name = $controller */
zend_update_property(ce, iaction, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_NAME), controller TSRMLS_CC);
/* $iaction->_controller = $icontroller */
zend_update_property(ce, iaction, ZEND_STRL(YAF_ACTION_PROPERTY_NAME_CTRL), icontroller TSRMLS_CC);
zval_ptr_dtor(&icontroller);
if (fptr->common.num_args) { //当前action有参数传递进来
zval *method_name = NULL;
yaf_dispatcher_get_call_parameters(request_ce, request, fptr, &call_args, &count TSRMLS_CC);
MAKE_STD_ZVAL(method_name);
ZVAL_STRINGL(method_name, YAF_ACTION_EXECUTOR_NAME, sizeof(YAF_ACTION_EXECUTOR_NAME) - 1, 0);
call_user_function_ex(&(ce)->function_table, &iaction, method_name, &ret, count, call_args, 1, NULL TSRMLS_CC);
efree(method_name);
efree(call_args);
} else { //无参数传递
zend_call_method_with_0_params(&iaction, ce, NULL, "execute", &ret);
}
if (!ret) {
zval_ptr_dtor(&action);
zval_ptr_dtor(&iaction);
return 0;
}
if (( Z_TYPE_P(ret) == IS_BOOL
&& !Z_BVAL_P(ret))) {
/* no auto-render */
zval_ptr_dtor(&ret);
zval_ptr_dtor(&action);
zval_ptr_dtor(&iaction);
return 1;
}
zval_ptr_dtor(&ret);
} else {
efree(func_name);
zval_ptr_dtor(&icontroller);
return 0;
}
if (executor) {
/* controller's property can override the Dispatcher's */
int auto_render = 1;
/* $render = Yaf_Action_Abstract::$yafAutoRender */
render = zend_read_property(ce, executor, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_RENDER), 1 TSRMLS_CC);
/* $instantly_flush = $this->_instantly_flush */
instantly_flush = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_FLUSH), 1 TSRMLS_CC);
if (render == EG(uninitialized_zval_ptr)) {
/* $render = $this->_auto_render */
render = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), 1 TSRMLS_CC);
/* $auto_render = (bool)$render */
auto_render = Z_BVAL_P(render);
} else if (Z_TYPE_P(render) <= IS_BOOL && !Z_BVAL_P(render)) {
auto_render = 0;
}
if (auto_render) { //开启了自动渲染
ret = NULL;
if (!Z_BVAL_P(instantly_flush)) { //没开启,有输出的时候通过Yaf_Response_Abstract来返回给用户
/* 调用Yaf_Controller_Abstract::render()进行渲染模板,然后得到渲染后的结果 */
zend_call_method_with_1_params(&executor, ce, NULL, "render", &ret, action);
zval_ptr_dtor(&executor);
if (!ret) {
zval_ptr_dtor(&action);
return 0;
} else if (IS_BOOL == Z_TYPE_P(ret) && !Z_BVAL_P(ret)) {
zval_ptr_dtor(&ret);
zval_ptr_dtor(&action);
return 0;
}
/* 渲染返回结果正常,将它放入Yaf_Response_Abstract::_body的值 */
if (Z_TYPE_P(ret) == IS_STRING && Z_STRLEN_P(ret)) {
yaf_response_alter_body(response, NULL, 0, Z_STRVAL_P(ret), Z_STRLEN_P(ret), YAF_RESPONSE_APPEND TSRMLS_CC);
}
zval_ptr_dtor(&ret);
} else { //开启了直接输出给用户
/* 直接调用Yaf_Controller_Abstract::display渲染视图模板, 并直接输出渲染结果 */
zend_call_method_with_1_params(&executor, ce, NULL, "display", &ret, action);
zval_ptr_dtor(&executor);
if (!ret) {
zval_ptr_dtor(&action);
return 0;
}
if ((Z_TYPE_P(ret) == IS_BOOL && !Z_BVAL_P(ret))) {
zval_ptr_dtor(&ret);
zval_ptr_dtor(&action);
return 0;
}
zval_ptr_dtor(&ret);
}
} else {
zval_ptr_dtor(&executor);
}
}
zval_ptr_dtor(&action);
}
return 1;
}
return 0;
}
/* }}} */
/** {{{ void yaf_dispatcher_exception_handler(yaf_dispatcher_t *dispatcher, yaf_request_t *request, yaf_response_t *response TSRMLS_DC)
* 在本文件中没用,在 yaf_exception.h中调用了
* 调用错误处理的controller和action进行错误处理
*/
void yaf_dispatcher_exception_handler(yaf_dispatcher_t *dispatcher, yaf_request_t *request, yaf_response_t *response TSRMLS_DC) {
zval *module, *controller, *action, *exception;
yaf_view_t *view;
if (YAF_G(in_exception) || !EG(exception)) {
return;
}
YAF_G(in_exception) = 1;
MAKE_STD_ZVAL(controller);
MAKE_STD_ZVAL(action);
/* $module = Yaf_Request_Abstract::$module */
module = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
/* Yaf_Request_Abstract::$moduel不合法,使用默认的moduel了来替换 */
if (!module || Z_TYPE_P(module) != IS_STRING || !Z_STRLEN_P(module)) {
module = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), module TSRMLS_CC);
}
/* $controller = "Error" */
ZVAL_STRING(controller, YAF_ERROR_CONTROLLER, 1);
/* $action = "error" */
ZVAL_STRING(action, YAF_ERROR_ACTION, 1);
exception = EG(exception);
EG(exception) = NULL;
/* Yaf_Request_Abstract::$controller = $controller */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), controller TSRMLS_CC);
/* Yaf_Request_Abstract::$action = $action */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), action TSRMLS_CC);
/* Yaf_Request_Abstract::$_exception = $exception */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_EXCEPTION), exception TSRMLS_CC);
Z_DELREF_P(controller);
zval_ptr_dtor(&action);
/** use $request->getException() instand of */
if (yaf_request_set_params_single(request, ZEND_STRL("exception"), exception TSRMLS_CC)) {
zval_ptr_dtor(&exception);
} else {
/* failover to uncaught exception */
EG(exception) = exception;
return;
}
yaf_request_set_dispatched(request, 0 TSRMLS_CC); /* Yaf_Request::dispatched = 0 */
view = yaf_dispatcher_init_view(dispatcher, NULL, NULL TSRMLS_CC); //实例化视图引擎类
if (!view) {
return;
}
if (!yaf_dispatcher_handle(dispatcher, request, response, view TSRMLS_CC)) { //进行controller文件的加载、然后执行action,最后根据配置来进行模板渲染以及输出
if (EG(exception)
&& instanceof_function(Z_OBJCE_P(EG(exception)),
yaf_buildin_exceptions[YAF_EXCEPTION_OFFSET(YAF_ERR_NOTFOUND_CONTROLLER)] TSRMLS_CC)) {
zval *m = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1 TSRMLS_CC);
/* failover to default module error catcher */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), m TSRMLS_CC);
zval_ptr_dtor(&EG(exception));
EG(exception) = NULL;
(void)yaf_dispatcher_handle(dispatcher, request, response, view TSRMLS_CC);
}
}
(void)yaf_response_send(response TSRMLS_CC);
YAF_EXCEPTION_ERASE_EXCEPTION();
}
/* }}} */
/** {{{ int yaf_dispatcher_route(yaf_dispatcher_t *dispatcher, yaf_request_t *request TSRMLS_DC)
* 路由执行
*/
int yaf_dispatcher_route(yaf_dispatcher_t *dispatcher, yaf_request_t *request TSRMLS_DC) {
zend_class_entry *router_ce;
/* $router = $this->_router */
yaf_router_t *router = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ROUTER), 1 TSRMLS_CC);
if (IS_OBJECT == Z_TYPE_P(router)) {
if ((router_ce = Z_OBJCE_P(router)) == yaf_router_ce) {
/* use built-in router */
yaf_router_route(router, request TSRMLS_CC);
} else {
/* user custom router */
zval *ret = zend_call_method_with_1_params(&router, router_ce, NULL, "route", &ret, request);
if (Z_TYPE_P(ret) == IS_BOOL && Z_BVAL_P(ret) == 0) {
yaf_trigger_error(YAF_ERR_ROUTE_FAILED TSRMLS_CC, "Routing request faild");
return 0;
}
}
return 1;
}
return 0;
}
/* }}} */
/** {{{ yaf_response_t * yaf_dispatcher_dispatch(yaf_dispatcher_t *dispatcher TSRMLS_DC)
*/
yaf_response_t * yaf_dispatcher_dispatch(yaf_dispatcher_t *dispatcher TSRMLS_DC) {
zval *return_response, *plugins, *view;
yaf_response_t *response;
yaf_request_t *request;
uint nesting = YAF_G(forward_limit); //forward最大嵌套深度
/* 进行一些列初始化,初始化成员变量并且返回类自身的实例 */
response = yaf_response_instance(NULL, sapi_module.name TSRMLS_CC);
/* Yaf_Request $request = $this->_request */
request = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), 1 TSRMLS_CC);
/* Yaf_Plugin $plugins = $this->_plugins */
plugins = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_PLUGINS), 1 TSRMLS_CC);
/* $request也就是$this->_request的值应该为一个对象k */
if (!request || IS_OBJECT != Z_TYPE_P(request)) {
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expect a %s instance", yaf_request_ce->name);
zval_ptr_dtor(&response);
return NULL;
}
/* route request */
if (!yaf_request_is_routed(request TSRMLS_CC)) { /* 还没有开始执行路由 */
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_ROUTESTARTUP, request, response); //routerstartup调用并执行插件
YAF_EXCEPTION_HANDLE(dispatcher, request, response); //异常处理
if (!yaf_dispatcher_route(dispatcher, request TSRMLS_CC)) { //执行路由
yaf_trigger_error(YAF_ERR_ROUTE_FAILED TSRMLS_CC, "Routing request failed");
YAF_EXCEPTION_HANDLE_NORET(dispatcher, request, response);
zval_ptr_dtor(&response);
return NULL;
}
/* 进行module/controller/action名的检验以及响应的转换 */
yaf_dispatcher_fix_default(dispatcher, request TSRMLS_CC);
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_ROUTESHUTDOWN, request, response); //routershutdown调用并执行插件
YAF_EXCEPTION_HANDLE(dispatcher, request, response); //异常处理
/* Yaf_Request::$routed = 1 */
(void)yaf_request_set_routed(request, 1 TSRMLS_CC);
} else {
/* 进行module/controller/action名的检验以及响应的转换 */
yaf_dispatcher_fix_default(dispatcher, request TSRMLS_CC);
}
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_LOOPSTARTUP, request, response); //dispatchloopstartup调用并执行插件
YAF_EXCEPTION_HANDLE(dispatcher, request, response); //异常处理
view = yaf_dispatcher_init_view(dispatcher, NULL, NULL TSRMLS_CC); //在dispatcher中实例化视图引擎
if (!view) {
return NULL;
}
/* 进行路由的分发,其中包含了forward操作 */
do {
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_PREDISPATCH, request, response); //predispatch调用并执行插件
if (!yaf_dispatcher_handle(dispatcher, request, response, view TSRMLS_CC)) { //进行controller文件的加载、然后执行action,最后根据配置来进行模板渲染以及输出
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
zval_ptr_dtor(&response);
return NULL;
}
/* 进行module/controller/action名的检验以及响应的转换 */
yaf_dispatcher_fix_default(dispatcher, request TSRMLS_CC);
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_POSTDISPATCH, request, response); //postdispatch调用并执行插件
YAF_EXCEPTION_HANDLE(dispatcher, request, response); //捕获异常
} while (--nesting > 0 && !yaf_request_is_dispatched(request TSRMLS_CC));
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_LOOPSHUTDOWN, request, response); //dispatchloopshutdown调用并执行插件
YAF_EXCEPTION_HANDLE(dispatcher, request, response); //捕获异常
if (0 == nesting && !yaf_request_is_dispatched(request TSRMLS_CC)) {
yaf_trigger_error(YAF_ERR_DISPATCH_FAILED TSRMLS_CC, "The max dispatch nesting %ld was reached", YAF_G(forward_limit));
YAF_EXCEPTION_HANDLE_NORET(dispatcher, request, response);
zval_ptr_dtor(&response);
return NULL;
}
/* $return_response = $this->_return_response */
return_response = zend_read_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RETURN), 1 TSRMLS_CC);